Sie sind auf Seite 1von 24

Basic Commandline Operating System to demonstrate

CPU Scheduling Algorithms

MADE BY:
SHANTAM SINHA
NAVYA NIDHI SHARMA
RAHUL BASAK
PINAKIN CHATURVEDI

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.

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

First Come First Served (FCFS) Scheduling


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

Gantt Chart &waiting time for FCFS

(NON PREEMPTIVE)SHORTEST JOB FIRST


SCHEDULING.

PREEMPTIVE SJF

ROUND ROBIN SCHEDULING ALGORITHM

FLOW DIAGRAM
After execution the remaining burst time of process >
time slice then added to the tail of ready queue
NEW
First requested
process

Added at the
tail

Running
I/O or event
completion
Waiting

According to
time slice

I/O or event
wait

Release

Exit

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.

If priority process will come, then added to the ready


queue as per priority
NEW
Highest priority
process

Added as per
assigned
priority

Running
I/O or event
completion
Waiting

Release
I/O or event
wait

Exit

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 have
its 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

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 Tim

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

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 an
integrated 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.

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":
{

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];

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

//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++)
{

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

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

//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;
total = 0;

//average waiting time

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)
{

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");

for(i=0;i<n;i++)
Console.WriteLine("P["+ i+1 + "]
" + tat[i]);

" + wt[i] + "

" + bt[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;
}
}
}
}}}

Output

About OS

List of available commands

Scheduling Algorithms

FCFS

SJF

Priority Based

Round Robin

Das könnte Ihnen auch gefallen