0% found this document useful (0 votes)
70 views

OS Notes

The document describes algorithms for implementing four process scheduling mechanisms: FCFS, SJF, Priority, and Round Robin. For each mechanism, it provides pseudocode outlining the key steps: accepting process details, calculating waiting times and turnaround times, and determining average waiting and turnaround times. It also includes C program code implementing each algorithm.

Uploaded by

Komal Tariq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

OS Notes

The document describes algorithms for implementing four process scheduling mechanisms: FCFS, SJF, Priority, and Round Robin. For each mechanism, it provides pseudocode outlining the key steps: accepting process details, calculating waiting times and turnaround times, and determining average waiting and turnaround times. It also includes C program code implementing each algorithm.

Uploaded by

Komal Tariq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

a)FCFS (First Come First Serve)

Aim: Write a C program to implement the various process scheduling


mechanisms such as FCFS
scheduling.
Algorithm:
1: Start the process
2: Accept the number of processes in the ready Queue
3: For each process in the ready Q, assign the process id and accept
the CPU burst time
4: Set the waiting of the first process as ‘0’ and its burst time as its
turn around time
5: for each process in the Ready Q calculate
a. Waiting time for process(n)= waiting time of process (n-1) + Burst
time of process(n-1)
b. Turnaround time for Process(n)= waiting time of Process(n)+ Burst
time for process(n)
6: Calculate
a. Average waiting time = Total waiting Time / Number of process
b. Average Turnaround time = Total Turnaround Time / Number of
process
7: Stop the process
Program:
#include <stdio.h>
int main()
{
int pid[15];
int bt[15];
int n;
printf("Enter the number of processes: ");
scanf("%d",&n);

printf("Enter process id of all the processes: ");


for(int i=0;i<n;i++)
{
scanf("%d",&pid[i]);
}

printf("Enter burst time of all the processes: ");


for(int i=0;i<n;i++)
{
scanf("%d",&bt[i]);
}

int i, wt[n];
wt[0]=0;

//for calculating waiting time of each process


for(i=1; i<n; i++)
{
wt[i]= bt[i-1]+ wt[i-1];
}

printf("Process ID Burst Time Waiting Time TurnAround


Time\n");
float twt=0.0;
float tat= 0.0;
for(i=0; i<n; i++)
{
printf("%d\t\t", pid[i]);
printf("%d\t\t", bt[i]);
printf("%d\t\t", wt[i]);

//calculating and printing turnaround time of each process


printf("%d\t\t", bt[i]+wt[i]);
printf("\n");

//for calculating total waiting time


twt += wt[i];

//for calculating total turnaround time


tat += (wt[i]+bt[i]);
}
float att,awt;
//for calculating average waiting time
awt = twt/n;

//for calculating average turnaround time


att = tat/n;
printf("Avg. waiting time= %f\n",awt);
printf("Avg. turnaround time= %f",att);
}
Output:

b) SJF (Shortest Job First)


Aim: Write a C program to implement the various process scheduling
mechanisms such as SJF
Scheduling.
Algorithm:
1: Start the process
2: Accept the number of processes in the ready Queue
3: For each process in the ready Q, assign the process id and accept
the CPU burst time
4: Start the Ready Q according the shortest Burst time by sorting
according to lowest to highest burst time.
5: Set the waiting time of the first process as ‘0’ and its turnaround
time as its burst time.
6: For each process in the ready queue, calculate
(a) Waiting time for process(n)= waiting time of process (n-1) + Burst
time of process(n-1)
(b) Turn around time for Process(n)= waiting time of Process(n)+
Burst time for process(n)
7: Calculate
(c) Average waiting time = Total waiting Time / Number of process
• Average Turnaround time = Total Turnaround Time / Number of
process
8: Stop the process
Program:
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,totalT=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);

printf("\nEnter Burst Time:\n");


for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}

//sorting of burst times


for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;

//finding the waiting time of all the processes


for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
//individual WT by adding BT of all previous completed
processes
wt[i]+=bt[j];

//total waiting time


total+=wt[i];
}

//average waiting time


avg_wt=(float)total/n;

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround


Time");
for(i=0;i<n;i++)
{
//turnaround time of individual processes
tat[i]=bt[i]+wt[i];

//total turnaround time


totalT+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}

//average turnaround time


avg_tat=(float)totalT/n;
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f",avg_tat);
}
Output:

c)Priority
Aim: Write a C program to implement the various process scheduling
mechanisms such as
Priority Scheduling.
Algorithm:
1: Start the process
2: Accept the number of processes in the ready Queue
3: For each process in the ready Q, assign the process id and accept
the CPU burst time 4: Sort
the ready queue according to the priority number.
5: Set the waiting of the first process as ‘0’ and its burst time as its
turn around time
6: For each process in the Ready Q calculate
(e) Waiting time for process(n)= waiting time of process (n-1) + Burst
time of process(n-1)
(f) Turn around time for Process(n)= waiting time of Process(n)+
Burst time for process(n)
7: Calculate
(g) Average waiting time = Total waiting Time / Number of process
(h) Average Turnaround time = Total Turnaround Time / Number of
process Step 8: Stop the
Process
Program:
#include<stdio.h>
int main()
{
int
bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_t
at;
printf("Enter Total Number of Process:");
scanf("%d",&n);
printf("\nEnter Burst Time and Priority\n");
for(i=0;i<n;i++)
{
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
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;
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
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];
}
avg_wt=total/n; //average waiting time
total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround
Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=total/n; //average turnaround time
printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);
return 0;
}
Output:

c) Round Robin
Aim: Write a C program to implement the various process scheduling
mechanisms such as Round
Robin Scheduling.
Algorithm
1: Start the process
2: Accept the number of processes in the ready Queue and time
quantum (or) time slice
3: For each process in the ready Q, assign the process id and accept
the CPU burst time
4: Calculate the no. of time slices for each process where
No. of time slice for process(n) = burst time process(n)/time slice
5: If the burst time is less than the time slice then the no. of time slices
=1.
6: Consider the ready queue is a circular Q, calculate
(a) Waiting time for process(n) = waiting time of process(n-1)+ burst
time of process(n-1 ) +
the time difference in getting the CPU from process(n-1)
(b) Turn around time for process(n) = waiting time of process(n) +
burst time of process(n)+ the
time difference in getting CPU from process(n).
7: Calculate
(a) Average waiting time = Total waiting Time / Number of process
(b) Average Turnaround time = Total Turnaround Time / Number of
process Step
8: Stop the process

Program:
#include<stdio.h>

int main()
{
//Input no of processed
int n;
printf("Enter Total Number of Processes:");
scanf("%d", &n);
int wait_time = 0, ta_time = 0, arr_time[n], burst_time[n],
temp_burst_time[n];
int x = n;

//Input details of processes


for(int i = 0; i < n; i++)
{
printf("Enter Details of Process %d \n", i + 1);
printf("Arrival Time: ");
scanf("%d", &arr_time[i]);
printf("Burst Time: ");
scanf("%d", &burst_time[i]);
temp_burst_time[i] = burst_time[i];
}

//Input time slot


int time_slot;
printf("Enter Time Slot:");
scanf("%d", &time_slot);
//Total indicates total time
//counter indicates which process is executed
int total = 0, counter = 0,i;
printf("Process ID Burst Time Turnaround Time Waiting
Time\n");
for(total=0, i = 0; x!=0; )
{
// define the conditions
if(temp_burst_time[i] <= time_slot && temp_burst_time[i] > 0)
{
total = total + temp_burst_time[i];
temp_burst_time[i] = 0;
counter=1;
}
else if(temp_burst_time[i] > 0)
{
temp_burst_time[i] = temp_burst_time[i] - time_slot;
total += time_slot;
}
if(temp_burst_time[i]==0 && counter==1)
{
x--; //decrement the process no.
printf("\nProcess No %d \t\t %d\t\t\t\t %d\t\t\t %d", i+1,
burst_time[i],
total-arr_time[i], total-arr_time[i]-burst_time[i]);
wait_time = wait_time+total-arr_time[i]-burst_time[i];
ta_time += total -arr_time[i];
counter =0;
}
if(i==n-1)
{
i=0;
}
else if(arr_time[i+1]<=total)
{
i++;
}
else
{
i=0;
}
}
float average_wait_time = wait_time * 1.0 / n;
float average_turnaround_time = ta_time * 1.0 / n;
printf("\nAverage Waiting Time:%f", average_wait_time);
printf("\nAvg Turnaround Time:%f", average_turnaround_time);
return 0;
}
Output:

You might also like