fos report : command line os for demonstration of cpu scheduling algorithms

24
 Basic Commandline Operating System to demonstrate CPU Scheduling Algorithms MADE BY: SHANTAM SINHA NAVYA NIDHI SHARMA RAHUL BASAK PINAKIN CHATURVEDI

Upload: shantam-sinha

Post on 07-Jan-2016

238 views

Category:

Documents


0 download

DESCRIPTION

FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

TRANSCRIPT

Page 1: FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

7/17/2019 FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

http://slidepdf.com/reader/full/fos-report-command-line-os-for-demonstration-of-cpu-scheduling-algorithms 1/24

 

Basic Commandline Operating System to demonstrate

CPU Scheduling Algorithms

MADE BY:SHANTAM SINHA

NAVYA NIDHI SHARMARAHUL BASAK

PINAKIN CHATURVEDI

Page 2: FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

7/17/2019 FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

http://slidepdf.com/reader/full/fos-report-command-line-os-for-demonstration-of-cpu-scheduling-algorithms 2/24

Background

•  In computer science, scheduling is the method by which threads, processes

or data flows are given access to system resources (e.g. processor time).

•  This is done to load balance a system effectively.

•  The need for a scheduling algorithm arises from the requirement for

modern systems to perform multitasking.

Scheduling: Deciding which threads are given access to resources from

moment to moment.

Page 3: FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

7/17/2019 FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

http://slidepdf.com/reader/full/fos-report-command-line-os-for-demonstration-of-cpu-scheduling-algorithms 3/24

 

Assumptions about Scheduling

Many implicit assumptions for CPU scheduling are:

•  One program per user

•  One thread per program

•  Programs are independent

•  These are unrealistic but simplify the problem

Does “fair” mean fairness among users or programs? 

•  If I run one compilation job and you run five, do you get five times as

much CPU?

•  Often times, yes!

•  Goal: dole out CPU time to optimize some desired parameters

of the system.

Types of scheduling

•  FIRST COME, FIRST SERVED

•  SHORTEST JOB FIRST ( Pre-emptive & Non Pre-emptive)

• 

Round-robin scheduling

•  Fixed-priority pre-emptive scheduling

•  Multilevel Queue Scheduling

Page 4: FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

7/17/2019 FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

http://slidepdf.com/reader/full/fos-report-command-line-os-for-demonstration-of-cpu-scheduling-algorithms 4/24

First Come First Served (FCFS) Scheduling

Processes get the CPU in the order they request it and run until they release it

Page 5: FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

7/17/2019 FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

http://slidepdf.com/reader/full/fos-report-command-line-os-for-demonstration-of-cpu-scheduling-algorithms 5/24

 

Gantt Chart &waiting time for FCFS

Page 6: FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

7/17/2019 FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

http://slidepdf.com/reader/full/fos-report-command-line-os-for-demonstration-of-cpu-scheduling-algorithms 6/24

(NON PREEMPTIVE)SHORTEST JOB FIRST

SCHEDULING.

PREEMPTIVE SJF

Page 7: FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

7/17/2019 FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

http://slidepdf.com/reader/full/fos-report-command-line-os-for-demonstration-of-cpu-scheduling-algorithms 7/24

ROUND ROBIN SCHEDULING ALGORITHM

FLOW DIAGRAM

NEW

Waiting

Running

Exit

After execution the remaining burst time of process >

time slice then added to the tail of ready queue

Added at the

tail

I/O or event

completion

First requested

process

Release

I/O or event

wait

According to

time slice

Page 8: FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

7/17/2019 FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

http://slidepdf.com/reader/full/fos-report-command-line-os-for-demonstration-of-cpu-scheduling-algorithms 8/24

Fixed-priority pre-emptive scheduling

•  Each process is assigned a priority. Process with highest priority is to be

executed first and so on. Processes with same priority are executed on first

come first serve basis. Priority can be decided based on memory

requirements, time requirements or any other resource requirement.

MULTILEVEL QUEUE SCHEDULING

•  This is used for situations in which processes are easily divided into different

groups. Multiple queues are maintained for processes. Each queue can haveits own scheduling algorithms. Priorities are assigned to each queue.

•  Ready queue is partitioned into separate queues: foreground (interactive)

and background (batch)

•  Each queue has its own scheduling algorithm: foreground – RR and

background – FCFS

NEW

Waiting

Running

Exit

If priority process will come, then added to the ready

queue as per priority

Added as per

assigned

priority

I/O or event

completion

Highest priority

process

Release

I/O or event

wait

Page 9: FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

7/17/2019 FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

http://slidepdf.com/reader/full/fos-report-command-line-os-for-demonstration-of-cpu-scheduling-algorithms 9/24

COMPARISON AND CONCLUSION

The treatment of shortest process in SJF scheduling tends to result in increased

waiting time for long processes. And the long process will never get served, though

it produces minimum average waiting time and average turnaround time. It is

recommended that any kind of simulation for any CPU scheduling algorithm has

limited accuracy. The only way to evaluate a scheduling algorithm is to code it and

to put it in the operating system, only then the proper working capability of the

algorithm can be measured in real time systems.

Scheduling

Algorithm 

CPU Overhead  Throughput  Turnaround Time  Response Ti

First In First Out  Low Low High Low

Shortest Job First  Medium High Medium Medium

Priority based

scheduling 

Medium Low High High

Round-robin

scheduling 

High Medium Medium High

Page 10: FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

7/17/2019 FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

http://slidepdf.com/reader/full/fos-report-command-line-os-for-demonstration-of-cpu-scheduling-algorithms 10/24

About COSMOS

Cosmos (C# Open Source Managed Operating System) is an operating system

development kit which uses Visual Studio as its development environment. Despite

C# in the name, any .NET based language can be used including VB.NET, FORTRAN,

Delphi Prism, IronPython, F# and more. Cosmos itself and the kernel routines are

primarily written in C#, and thus the Cosmos name. Besides that, NOSMOS (.NET

Open Source Managed Operating System) sounds stupid.

Cosmos is not an operating system in the traditional sense, but instead it is an

"Operating System Kit", or as I like to say "Operating System Legos". Cosmos lets you

create operating systems just as Visual Studio and C# normally let you create

applications. Most users can write and boot their own operating system in just a few

minutes, all using Visual Studio. Milestone 5 includes new features such as anintegrated project type in Visual Studio, and an integrated debugger. You can debug

your operating system directly from Visual Studio using breakpoints.

Cosmos is available in two distributions, the developer kit (dev kit), and the user kit.

The dev kit is designed for users who want to work on Cosmos itself. The user kit is

designed for those who are interested in building their own operating system and

doing some Cosmos work. The dev kit might be thought of as the Cosmos SDK. Most

users should start off with the user kit as it is not so overwhelming like the dev kit.

This article focuses on the user kit.

Page 11: FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

7/17/2019 FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

http://slidepdf.com/reader/full/fos-report-command-line-os-for-demonstration-of-cpu-scheduling-algorithms 11/24

 

Code

using System;

using System.Collections.Generic;

using System.Text;

using Sys = Cosmos.System;

namespace iON

{

public class Kernel : Sys.Kernel

{

protected override void BeforeRun()

{

Console.WriteLine("Welcome to iON Operating System.\nTo see the list of available

commands, type 'list'.\n\n");

}

protected override void Run()

{

string ch, temp;

int flag;

while (true)

{

flag = 0;

Console.Write(":~$ ");

string command = Console.ReadLine();

switch (command)

{

case "shutdown":

{

Page 12: FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

7/17/2019 FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

http://slidepdf.com/reader/full/fos-report-command-line-os-for-demonstration-of-cpu-scheduling-algorithms 12/24

  CosmosOS.ACPI.Shutdown();

CosmosOS.ACPI.Disable();

Cosmos.Core.Global.CPU.Halt();

break;

}

case "list":

{

Console.WriteLine("schedule - Demonstrate CPU Scheduling algorithms");

Console.WriteLine("about - Info. about the OS");

Console.WriteLine("list - List the available commands");

Console.WriteLine("shutdown - Shut down the OS");

Console.WriteLine("reboot - Reboot the OS");

break;

}

case "about":

{

Console.WriteLine("iON OS v1.0 - A simple OS to demonstrate CPU Scheduling

Algorithms.");

break;

}

case "reboot":

{

CosmosOS.ACPI.Reboot();

break;

}

case "schedule":

{

Int32 n, i, j, total=0, tempvar, pos, tq, count, sq=0, swt=0, stat=0;

float avwt = 0, avtat = 0;

Int32[] bt = new Int32[20];

Int32[] p = new Int32[20];

Page 13: FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

7/17/2019 FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

http://slidepdf.com/reader/full/fos-report-command-line-os-for-demonstration-of-cpu-scheduling-algorithms 13/24

  Int32[] wt = new Int32[20];

Int32[] st = new Int32[20];

Int32[] tat = new Int32[20];

Int32[] pr = new Int32[20];

while (flag == 0)

{

Console.Clear();

Console.WriteLine("\nPROCESS SCHEDULING\n\nChoose from the following

options : ");

Console.WriteLine("1. First Come First Serve (FCFS)");

Console.WriteLine("2. Shortest Job First (SJF)");

Console.WriteLine("3. Priority Based");

Console.WriteLine("4. Round Robin");

Console.WriteLine("5. Exit");

Console.WriteLine("\n\n\nEnter your choice : ");

ch = Console.ReadLine();

switch (ch)

{

case "1":

{

Console.Write("Enter total number of processes : ");

temp = Console.ReadLine();

n = Int32.Parse(temp);

Console.Write("\nEnter Process Burst Time\n");

for (i = 0; i < n; i++)

{

Console.Write("P[" + i + 1 + "]:");

temp = Console.ReadLine();

bt[i] = Int32.Parse(temp);

}

wt[0] = 0; //waiting time for first process is 0

Page 14: FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

7/17/2019 FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

http://slidepdf.com/reader/full/fos-report-command-line-os-for-demonstration-of-cpu-scheduling-algorithms 14/24

  //calculating waiting time

for (i = 1; i < n; i++)

{

wt[i] = 0;

for (j = 0; j < i; j++)

wt[i] += bt[j];

}

Console.WriteLine("\nProcess\t\tBT\tWT\tTT");

//calculating turnaround time

for (i = 0; i < n; i++)

{

tat[i] = bt[i] + wt[i];

avwt += wt[i];

avtat += tat[i];

Console.WriteLine("\nP[" + i + 1 + "]" + "\t\t" + bt[i] + "\t\t" + wt[i] +

"\t\t" + tat[i]);

}

avwt /= i;

avtat /= i;

Console.WriteLine("\n\nAverage Waiting Time:" + avwt);

Console.WriteLine("\nAverage Turnaround Time:" + avtat);

break;

}

case "2":

{

Console.WriteLine("Enter the total number of processes: ");

temp = Console.ReadLine();

n = Int32.Parse(temp);

Console.Write("\nEnter Process Burst Time\n");

for (i = 0; i < n; i++)

{

Page 15: FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

7/17/2019 FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

http://slidepdf.com/reader/full/fos-report-command-line-os-for-demonstration-of-cpu-scheduling-algorithms 15/24

  Console.Write("P[" + i + 1 + "]:");

temp = Console.ReadLine();

bt[i] = Int32.Parse(temp);

p[i]=i+1;

}

//sorting burst time in ascending order using selection sort

for(i=0;i<n;i++)

{

pos=i;

for(j=i+1;j<n;j++)

{

if(bt[j]<bt[pos])

pos=j;

}

tempvar=bt[i];

bt[i]=bt[pos];

bt[pos]=tempvar;

tempvar=p[i];

p[i]=p[pos];

p[pos]=tempvar;

}

wt[0]=0; //waiting time for first process will be zero

//calculate waiting time

for(i=1;i<n;i++)

{

wt[i]=0;

for(j=0;j<i;j++)

wt[i]+=bt[j];

total+=wt[i];

}

avwt=total/n; //average waiting time

Page 16: FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

7/17/2019 FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

http://slidepdf.com/reader/full/fos-report-command-line-os-for-demonstration-of-cpu-scheduling-algorithms 16/24

  total=0;

Console.WriteLine("\nProcess\t BT \tWT\tTT");

for(i=0;i<n;i++)

{

tat[i]=bt[i]+wt[i]; //calculate turnaround time

total+=tat[i];

Console.WriteLine("\nP[" + i + 1 + "]" + "\t\t" + bt[i] + "\t\t" + wt[i] +

"\t\t" + tat[i]);

}

avtat=total/n; //average turnaround time

Console.WriteLine("\n\nAverage Waiting Time:" + avwt);

Console.WriteLine("\nAverage Turnaround Time:" + avtat);

break;

}

case "3":

{

Console.WriteLine("Enter the total number of processes: ");

temp = Console.ReadLine();

n = Int32.Parse(temp);

Console.Write("\nEnter Burst Time and Priority: \n");

for (i = 0; i < n; i++)

{

Console.WriteLine("P[" + i + 1 + "]:");

Console.Write("Burst Time : ");

temp = Console.ReadLine();

bt[i] = Int32.Parse(temp);

Console.Write("Priority : ");

temp = Console.ReadLine();

pr[i] = Int32.Parse(temp);

p[i] = i + 1; //contains process number

}

Page 17: FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

7/17/2019 FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

http://slidepdf.com/reader/full/fos-report-command-line-os-for-demonstration-of-cpu-scheduling-algorithms 17/24

  //sorting burst time, priority and process number in ascending order

using selection sort

for (i = 0; i < n; i++)

{

pos = i;

for (j = i + 1; j < n; j++)

{

if (pr[j] < pr[pos])

pos = j;

}

tempvar = pr[i];

pr[i] = pr[pos];

pr[pos] = tempvar;

tempvar = bt[i];

bt[i] = bt[pos];

bt[pos] = tempvar;

tempvar = p[i];

p[i] = p[pos];

p[pos] = tempvar;

}

wt[0] = 0; //waiting time for first process is zero

//calculate waiting time

for (i = 1; i < n; i++)

{

wt[i] = 0;

for (j = 0; j < i; j++)

wt[i] += bt[j];

total += wt[i];

}

avwt = total/n; //average waiting time

total = 0;

Page 18: FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

7/17/2019 FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

http://slidepdf.com/reader/full/fos-report-command-line-os-for-demonstration-of-cpu-scheduling-algorithms 18/24

  Console.WriteLine("\nProcess\t BT \tWT\tTT");

for (i = 0; i < n; i++)

{

tat[i] = bt[i] + wt[i]; //calculate turnaround time

total += tat[i];

Console.WriteLine("\nP[" + i + 1 + "]" + "\t\t" + bt[i] + "\t\t" + wt[i] +

"\t\t" + tat[i]);

}

avtat = total / n; //average turnaround time

Console.WriteLine("\n\nAverage Waiting Time:" + avwt);

Console.WriteLine("\nAverage Turnaround Time:" + avtat);

break;

}

case "4":

{

Console.Write("Enter number of processes: ");

temp = Console.ReadLine();

n = Int32.Parse(temp);

Console.WriteLine("Enter burst time for processes: ");

for(i=0;i<n;i++)

{

Console.Write("P[" + i + 1 + "]: ");

temp = Console.ReadLine();

bt[i] = Int32.Parse(temp);

st[i]=bt[i];

}

Console.Write("Enter time quantum: ");

temp = Console.ReadLine();

tq = Int32.Parse(temp);

while(true)

{

Page 19: FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

7/17/2019 FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

http://slidepdf.com/reader/full/fos-report-command-line-os-for-demonstration-of-cpu-scheduling-algorithms 19/24

  for(i=0,count=0;i<n;i++)

{

tempvar=tq;

if(st[i]==0)

{

count++;

continue;

}

if(st[i]>tq)

st[i]=st[i]-tq;

else

if(st[i]>=0)

{

tempvar=st[i];

st[i]=0;

}

sq=sq+tempvar;

tat[i]=sq;

}

if(n==count)

break;

}

for(i=0;i<n;i++)

{

wt[i]=tat[i]-bt[i];

swt=swt+wt[i];

stat=stat+tat[i];

}

avwt=swt/n;

avtat=stat/n;

Console.WriteLine("Process BT WT TT");

Page 20: FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

7/17/2019 FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

http://slidepdf.com/reader/full/fos-report-command-line-os-for-demonstration-of-cpu-scheduling-algorithms 20/24

  for(i=0;i<n;i++)

Console.WriteLine("P["+ i+1 + "] " + bt[i] + "

" + wt[i] + " " + tat[i]);

Console.WriteLine("Average Waiting Time: " + avwt);

Console.WriteLine("Average Turnaround Time: " + avtat);

break;

}

case "5":

{

flag = 1;

break;

}

default:

{

Console.WriteLine("ERROR : Invalid choice! Enter again.");

break;

}

}

break;

}

break;

}

default:

{

Console.WriteLine("ERROR: Invalid command! Enter again.");

command = null;

break;

}

}

}

} } }

Page 21: FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

7/17/2019 FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

http://slidepdf.com/reader/full/fos-report-command-line-os-for-demonstration-of-cpu-scheduling-algorithms 21/24

Output

About OS

List of available commands

Page 22: FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

7/17/2019 FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

http://slidepdf.com/reader/full/fos-report-command-line-os-for-demonstration-of-cpu-scheduling-algorithms 22/24

 Scheduling Algorithms

FCFS

Page 23: FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

7/17/2019 FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

http://slidepdf.com/reader/full/fos-report-command-line-os-for-demonstration-of-cpu-scheduling-algorithms 23/24

 SJF

Priority Based

Page 24: FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

7/17/2019 FOS Report : Command Line OS for demonstration of CPU Scheduling Algorithms

http://slidepdf.com/reader/full/fos-report-command-line-os-for-demonstration-of-cpu-scheduling-algorithms 24/24

 Round Robin