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

Operating System Lab Manual

This document contains 4 programming objects related to CPU scheduling algorithms: 1) FCFS scheduling algorithm 2) SJF scheduling algorithm 3) Priority scheduling algorithm 4) Round Robin scheduling algorithm Pseudocode and C code is provided for each algorithm to calculate waiting times and average waiting time.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Operating System Lab Manual

This document contains 4 programming objects related to CPU scheduling algorithms: 1) FCFS scheduling algorithm 2) SJF scheduling algorithm 3) Priority scheduling algorithm 4) Round Robin scheduling algorithm Pseudocode and C code is provided for each algorithm to calculate waiting times and average waiting time.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

OPERATING SYSTEM LAB

MANUAL

OBJECT 1: Write a program to implement FCFS CPU scheduling


algorithm
#include<stdio.h>
#include<conio.h>
#include<process.h>
int main()
{
char p[10][5];
int tot=0,pt[10],wt[10],i,n;
float avg=0;
printf("enter no of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter process%d name:\n",i+1);
scanf("%s",&p[i]);
printf("enter process time");
scanf("%d",&pt[i]);
}

wt[0]=0;
for(i=1;i<n;i++)

{
wt[i]=wt[i-1]+pt[i-1];
tot=tot+wt[i];
}
avg=(float)tot/n;
printf("p_name\t P_time\t w_time\n");
for(i=0;i<n;i++)
printf("%s\t%d\t%d\n",p[i],pt[i],wt[i]);
printf("total waiting time=%d\n avg waiting time=%f",tot,avg);
getch();
return 0;
}

OBJECT 2: Write a program to implement SJF CPU scheduling


algorithm.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
int main()
{
char p[10][5],temp[5];
int tot=0,wt[10],pt[10],i,j,n,temp1;
float avg=0;
printf("enter no of processes:");
scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("enter process%d name:\n",i+1);
scanf("%s",&p[i]);
printf("enter process time");
scanf("%d",&pt[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(pt[i]>pt[j])
{
temp1=pt[i];
pt[i]=pt[j];
pt[j]=temp1;
strcpy(temp,p[i]);
strcpy(p[i],p[j]);
strcpy(p[j],temp);
}
}
}
wt[0]=0;
for(i=1;i<n;i++)
{

wt[i]=wt[i-1]+pt[i-1];
tot=tot+wt[i];
}
avg=(float)tot/n;
printf("p_name\t P_time\t w_time\n");
for(i=0;i<n;i++)
printf("%s\t%d\t%d\n",p[i],pt[i],wt[i]);
printf("total waiting time=%d\n avg waiting time=%f",tot,avg);
getch();
return
}
OBJECT 3: Write a program to implement Priority CPU Scheduling
algorithm.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char p[10][5],temp[5];
int i,j,pt[10],wt[10],totwt=0,pr[10],temp1,n;
float avgwt;
printf("enter no of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{

printf("enter process%d name:",i+1);


scanf("%s",&p[i]);
printf("enter process time:");
scanf("%d",&pt[i]);
printf("enter priority:");
scanf("%d",&pr[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(pr[i]>pr[j])
{
temp1=pr[i];
pr[i]=pr[j];
pr[j]=temp1;
temp1=pt[i];
pt[i]=pt[j];
pt[j]=temp1;
strcpy(temp,p[i]);
strcpy(p[i],p[j]);
strcpy(p[j],temp);
}
}
}

wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+pt[i-1];
totwt=totwt+wt[i];
}
avgwt=(float)totwt/n;
printf("p_name\t p_time\t priority\t w_time\n");
for(i=0;i<n;i++)
{
printf(" %s\t %d\t %d\t %d\n" ,p[i],pt[i],pr[i],wt[i]);
}
printf("total waiting time=%d\n avg waiting time=%f",totwt,avgwt);
getch();
return 0;
}
OBJECT 4: Write a program to implement Round Robin CPU
scheduling algorithm.
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
int main()
{
char p[10][5];

int et[10],wt[10],timer=3,count,pt[10],rt,i,j,totwt=0,t,n=5,found=0,m;
float avgwt;
for(i=0;i<n;i++)
{
printf("enter the process name : ");
scanf("%s",&p[i]);
printf("enter the processing time : ");
scanf("%d",&pt[i]);
}
m=n;
wt[0]=0;
i=0;
do
{
if(pt[i]>timer)
{
rt=pt[i]-timer;
strcpy(p[n],p[i]);
pt[n]=rt;
et[i]=timer;
n++;
}
else
{
et[i]=pt[i];

}
i++;
wt[i]=wt[i-1]+et[i-1];
}while(i<n);

count=0;
for(i=0;i<m;i++)
{
for(j=i+1;j<=n;j++)
{
if(strcmp(p[i],p[j])==0)
{
count++;
found=j;
}
}
if(found!=0)
{
wt[i]=wt[found]-(count*timer);
count=0;
found=0;
}
}
for(i=0;i<m;i++)
{

totwt+=wt[i];
}
avgwt=(float)totwt/m;
for(i=0;i<m;i++)
{
printf("\n%s\t%d\t%d",p[i],pt[i],wt[i]);
}
printf("\ntotal waiting time %d\n",totwt);
printf("total avgtime %f",avgwt);
getch();
return 0;
}

You might also like