0% found this document useful (0 votes)
7 views9 pages

Os

The document contains multiple C programs that implement different CPU scheduling algorithms including Fork, FCFS (First-Come, First-Served), SJF (Shortest Job First), Priority Scheduling, and Round Robin. Each program prompts the user for process details such as burst time, arrival time, and priority, and then calculates and displays metrics like waiting time and turnaround time. The programs demonstrate how each scheduling algorithm processes tasks and manages CPU time allocation.

Uploaded by

teck2addaa
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)
7 views9 pages

Os

The document contains multiple C programs that implement different CPU scheduling algorithms including Fork, FCFS (First-Come, First-Served), SJF (Shortest Job First), Priority Scheduling, and Round Robin. Each program prompts the user for process details such as burst time, arrival time, and priority, and then calculates and displays metrics like waiting time and turnaround time. The programs demonstrate how each scheduling algorithm processes tasks and manages CPU time allocation.

Uploaded by

teck2addaa
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/ 9

PID

#include<stdio.h>

#include<unistd.h>

#include<sys/types.h>

int main()

pid_t pid=fork();

if(pid<0)

printf("fork failed\n");

else if(pid==0)

printf("The child process PID is %d parent id is %d\n",getpid(),getppid());

else

printf("The parent process PID is %d child id is %d\n",getpid(),pid);

}
FCFS

#include<stdio.h>

int main()

int n,i,bt[10],at[10],tat[10],wt[10],ct[10];

float awt=0,atat=0;

printf("Enter the no of processes\n");

scanf("%d",&n);

printf("Enter the burst time\n");

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

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

printf("Enter the arrival time\n");

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

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

ct[0]=0;

printf("Process\t Burst time\t Arrival time\t Waiting time\t Turnaroundtime\n");

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

wt[i]=0;

tat[i]=0;

ct[i+1]=ct[i]+bt[i];

wt[i]=ct[i]-at[i];

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

awt=awt+wt[i];

atat=atat+tat[i];

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

awt=awt/n;

atat=atat/n;

printf("Average waiting time is %.2f\n",awt);

printf("Average turnaroundtime time is %.2f\n",atat);

}
SJF

#include<stdio.h>

int main()

int at[10],bt[10],pr[10];

int n,i,j,temp,time=0,count,over=0,sum_wait=0,sum_turnaround=0,start;

float avgwait,avgturn;

printf("Enter the number of processes\n");

scanf("%d",&n);

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

printf("Enter the arrival time and execution time for process %d\n",i+1);

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

pr[i]=i+1;

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

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

if(at[i]>at[j])

temp=at[i];

at[i]=at[j];

at[j]=temp;

temp=bt[i];

bt[i]=bt[j];

bt[j]=temp;

temp=pr[i];

pr[i]=pr[j];

pr[j]=temp;

}
printf("\n\nProcess\t|Arrival time\t|Burst time\t|Start time\t|End time\t|waiting time\t|Turnaround time\n\n");

while(over<n)

count=0;

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

if(at[i]<=time)

count++;

else

break;

if(count>1)

for(i=over; i<over+count-1; i++)

for(j=i+1; j<over+count; j++)

if(bt[i]>bt[j])

temp=at[i];

at[i]=at[j];

at[j]=temp;

temp=bt[i];

bt[i]=bt[j];

bt[j]=temp;

temp=pr[i];

pr[i]=pr[j];

pr[j]=temp;

start=time;

time+=bt[over];
printf("p[%d]\t|\t%d\t|\t%d\t|\t%d\t|\t%d\t|\t%d\t|\t%d\n",pr[over],

at[over],bt[over],start,time,time-at[over]-bt[over],time-at[over]);

sum_wait+=time-at[over]-bt[over];

sum_turnaround+=time-at[over];

over++;

avgwait=(float)sum_wait/(float)n;

avgturn=(float)sum_turnaround/(float)n;

printf("Average waiting time is %f\n",avgwait);

printf("Average turnaround time is %f\n",avgturn);

return 0;

}
Priority

#include<stdio.h>

#include<string.h>

int main()

int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10];

int totwt=0,totta=0;

float awt,ata;

char pn[10][10],t[10];

//clrscr();

printf("Enter the number of process:");

scanf("%d",&n);

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

printf("Enter process name,arrivaltime,execution time & priority:");

//flushall();

scanf("%s%d%d%d",pn[i],&at[i],&et[i],&p[i]);

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

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

if(p[i]<p[j])

temp=p[i];

p[i]=p[j];

p[j]=temp;

temp=at[i];

at[i]=at[j];

at[j]=temp;

temp=et[i];

et[i]=et[j];

et[j]=temp;

strcpy(t,pn[i]);

strcpy(pn[i],pn[j]);
strcpy(pn[j],t);

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

if(i==0)

st[i]=at[i];

wt[i]=st[i]-at[i];

ft[i]=st[i]+et[i];

ta[i]=ft[i]-at[i];

else

st[i]=ft[i-1];

wt[i]=st[i]-at[i];

ft[i]=st[i]+et[i];

ta[i]=ft[i]-at[i];

totwt+=wt[i];

totta+=ta[i];

awt=(float)totwt/n;

ata=(float)totta/n;

printf("\nPname\tarrivaltime\texecutiontime\tpriority\twaitingtime\ttatime");

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

printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],p[i],wt[i],ta[i]);

printf("\nAverage waiting time is:%.2fms",awt);

printf("\nAverage turnaroundtime is:%.2fms\n",ata);

return 0;

}
Round Robin

#include<stdio.h>

int main()

int cnt,j,n,t,remain,flag=0,tq;

int wt=0,tat=0,at[10],bt[10],rt[10];

printf("Enter Total Process:\t ");

scanf("%d",&n);

remain=n;

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

printf("Enter Arrival Time and Burst Time for Process Process Number %d :",cnt+1);

scanf("%d",&at[cnt]);

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

rt[cnt]=bt[cnt];

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

scanf("%d",&tq);

printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");

for(t=0,cnt=0;remain!=0;)

if(rt[cnt]<=tq && rt[cnt]>0)

t+=rt[cnt];

rt[cnt]=0;

flag=1;

else if(rt[cnt]>0)

rt[cnt]-=tq;

t+=tq;

}
if(rt[cnt]==0 && flag==1)

remain--;

printf("P[%d]\t|\t%d\t|\t%d\n",cnt+1,t-at[cnt],t-at[cnt]-bt[cnt]);

wt+=t-at[cnt]-bt[cnt];

tat+=t-at[cnt];

flag=0;

if(cnt==n-1)

cnt=0;

else if(at[cnt+1]<=t)

cnt++;

else

cnt=0;

printf("\nAverage Waiting Time= %f\n",wt*1.0/n);

printf("Avg Turnaround Time = %f\n",tat*1.0/n);

return 0;

You might also like