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

OS LAb

This document contains code for implementing three different CPU scheduling algorithms: First Come First Serve (FCFS), Shortest Job First (SJF) non-preemptive, and SJF preemptive. The FCFS algorithm calculates waiting times and turnaround times for processes without changing their order. The SJF non-preemptive algorithm sorts processes by burst time before calculating metrics. The SJF preemptive algorithm uses arrival times and iteratively reduces burst times to find minimum remaining process. All algorithms output average waiting and turnaround times.

Uploaded by

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

OS LAb

This document contains code for implementing three different CPU scheduling algorithms: First Come First Serve (FCFS), Shortest Job First (SJF) non-preemptive, and SJF preemptive. The FCFS algorithm calculates waiting times and turnaround times for processes without changing their order. The SJF non-preemptive algorithm sorts processes by burst time before calculating metrics. The SJF preemptive algorithm uses arrival times and iteratively reduces burst times to find minimum remaining process. All algorithms output average waiting and turnaround times.

Uploaded by

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

// FCFS Scheduling

#include<stdio.h>

int main()

int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;

printf("Enter total number of processes(maximum 20):");

scanf("%d",&n);

printf("\n Enter Process Burst Timen");

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

printf("P[%d]:",i+1);

scanf("%d",&bt[i]);

wt[0]=0;

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

wt[i]=0;

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

wt[i]+=bt[j];

printf("\n Process \t Burst Time \t Waiting Time \t Turnaround Time");

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

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

avwt+=wt[i];

avtat+=tat[i];

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

avwt/=i;

avtat/=i;
printf("\n Average Waiting Time:%d",avwt);

printf("\n Average Turnaround Time:%d",avtat);

return 0;

// SJF Scheduling Algorithm non preemptive


#include<stdio.h>

int main()

int bt[20],p[20],wt[20],tat[20],i,j,n,total=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;

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

wt[i]=0;

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

wt[i]+=bt[j];

total+=wt[i];

avg_wt=(float)total/n;

total=0;

printf("\n Process \t Burst Time \t Waiting Time \t Turnaround Time");

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

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

total+=tat[i];

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

avg_tat=(float)total/n;

printf("\nAverage Waiting Time=%f",avg_wt);

printf("\nAverage Turnaround Time=%f",avg_tat);

}
// SJF Scheduling Algorithm preemptive

#include <stdio.h>

int main()

int arrival_time[10], burst_time[10], temp[10];

int i, smallest, count = 0, time, limit;

double wait_time = 0, turnaround_time = 0, end;

float average_waiting_time, average_turnaround_time;

printf("\nEnter the Total Number of Processes:\t");

scanf("%d", &limit);

printf("\nEnter Details of %d Processesn", limit);

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

printf("\nEnter Arrival Time:\t");

scanf("%d", &arrival_time[i]);

printf("Enter Burst Time:\t");

scanf("%d", &burst_time[i]);

temp[i] = burst_time[i];

burst_time[9] = 9999;

for(time = 0; count != limit; time++)

smallest = 9;

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

if(arrival_time[i] <= time && burst_time[i] < burst_time[smallest] && burst_time[i] > 0)

smallest = i;
}

burst_time[smallest]--;

if(burst_time[smallest] == 0)

count++;

end = time + 1;

wait_time = wait_time + end - arrival_time[smallest] - temp[smallest];

turnaround_time = turnaround_time + end - arrival_time[smallest];

average_waiting_time = wait_time / limit;

average_turnaround_time = turnaround_time / limit;

printf("\nAverage Waiting Time:\t%lf\n", average_waiting_time);

printf("Average Turnaround Time:\t%lf\n", average_turnaround_time);

return 0;

You might also like