0% found this document useful (0 votes)
72 views7 pages

Operating Systems (SWE3001)

This document contains C code to calculate the turnaround time and waiting time of processes using the FIFO and non-preemptive SJF scheduling algorithms. It first implements FIFO scheduling for processes with arrival time of 0, calculating and printing the waiting time, turnaround time, and average times. It then implements non-preemptive SJF scheduling, sorting processes by arrival time then selecting the process with the smallest remaining burst time at each time quantum. It calculates and prints the waiting time, turnaround time, and average times for each process and overall.

Uploaded by

Delvin company
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)
72 views7 pages

Operating Systems (SWE3001)

This document contains C code to calculate the turnaround time and waiting time of processes using the FIFO and non-preemptive SJF scheduling algorithms. It first implements FIFO scheduling for processes with arrival time of 0, calculating and printing the waiting time, turnaround time, and average times. It then implements non-preemptive SJF scheduling, sorting processes by arrival time then selecting the process with the smallest remaining burst time at each time quantum. It calculates and prints the waiting time, turnaround time, and average times for each process and overall.

Uploaded by

Delvin company
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/ 7

Name : Kiran Vellanki Faculty : Dr.M.

Premalatha
Reg No: 19MIS1089 Slot: L19+L20

Operating Systems(SWE3001)
Ex. No 4 - FIFO and Non Preemptive SJF with AT

#include<stdio.h>
int waitingtime(int proc[], int n,
int burst_time[], int wait_time[]) {
wait_time[0] = 0;
for (int i = 1; i < n ; i++ )
wait_time[i] = burst_time[i-1] + wait_time[i-1] ;
return 0;
}
int turnaroundtime( int proc[], int n,
int burst_time[], int wait_time[], int tat[]) {
int i;
for ( i = 0; i < n ; i++)
tat[i] = burst_time[i] + wait_time[i];
return 0;
}
int avgtime( int proc[], int n, int burst_time[]) {
int wait_time[n], tat[n], total_wt = 0, total_tat = 0;
int i;
waitingtime(proc, n, burst_time, wait_time);
turnaroundtime(proc, n, burst_time, wait_time, tat);
printf("\n----RESULT----\n\nProcess Burst-time(s) Waiting-time(s)
Turnaround-time(s)\n");
for ( i=0; i<n; i++) {
total_wt += wait_time[i];
total_tat += tat[i];
printf("[P%d] %d %d %d\n", i+1,
burst_time[i], wait_time[i], tat[i]);
}
printf("\nTotal waiting time : %d\n Total turnaround time : %d
\n",total_wt,total_tat);
printf("Average waiting time = %f\n", (float)total_wt / n);
printf("Average turn around time = %f\n", (float)total_tat /n);
return 0;
}
/////////////////

int main()
{
int bt[100],wt[100],tat[100],at[100],proc[100];
int total=0,total2=0,temp,a,i,num;
float av1=0,av2=0;
int p[10]={1,2,3,4,5,6,7,8,9,10},min,k=1,btime=0;
int j,tt[10],ta=0,sum=0;
float wavg=0,tavg=0,tsum=0,wsum=0;
while(1){
printf("1 : FIFO-NP at AT=0\n2 : SJF-NP\n0 : EXIT ");
//printf("*****INPUT*****\n");
printf("\nEnter your Choice : ");
scanf("%d",&a);
switch (a)
{
case 1:
/////////////////////
//process id's
printf("AT=0\nEnter number of process\n");
scanf("%d",&num);

printf("Enter burst time for processess\n");


for(i=0;i<num;i++)
{
printf("P%d: ",(i+1));
scanf("%d",&bt[i]);
}
avgtime(proc, num, bt);
printf("----------------------\n");
break;
///////////////
case 2:
printf( "Enter number of Process: ");
scanf ("%d",&num);
printf("Enter the arrival time of %d processess\n",num);
for(i=0;i<num;i++)
{
printf("P%d: ",(i+1));
scanf(" %d",&at[i]);
}
printf("Enter the burst time of %d processess \n",num);
printf("P%d: ",(i+1));
for(i=0;i<num;i++){

scanf(" %d",&bt[i]);
}
for(i=0;i<num;i++)
{
for(j=0;j<num;j++)
{
if(at[i]<at[j])
{
temp=p[j];
p[j]=p[i];
p[i]=temp;
temp=at[j];
at[j]=at[i];
at[i]=temp;
temp=bt[j];
bt[j]=bt[i];
bt[i]=temp;
}
}
}

for(j=0;j<num;j++)
{
btime=btime+bt[j];
min=bt[k];
for(i=k;i<num;i++)
{
if (btime>=at[i] && bt[i]<min)
{
temp=p[k];
p[k]=p[i];
p[i]=temp;
temp=at[k];
at[k]=at[i];
at[i]=temp;
temp=bt[k];
bt[k]=bt[i];
bt[i]=temp;
}
}
k++;
}
wt[0]=0;
for(i=1;i<num;i++)
{
sum=sum+bt[i-1];
wt[i]=sum-at[i];
wsum=wsum+wt[i];

wavg=(wsum/num);
for(i=0;i<num;i++)
{
ta=ta+bt[i];
tt[i]=ta-at[i];
tsum=tsum+tt[i];

}
tavg=(tsum/num);
printf("\n RESULT:-");
printf("\nProcess\t Burst\t Arrival\t Waiting\t Turn-around" );
for(i=0;i<num;i++)
{
printf("\n p%d\t %d\t %d\t\t
%d\t\t\t%d",p[i],bt[i],at[i],wt[i],tt[i]);
}
printf("\n\nAverage waiting time: %f",wavg);
printf("\nAverage turn around time: %f",tavg);
printf("\n----------------------\n");
break;
case 0:
printf("Exit\n\n");
//return 0;
exit(0);
break;
default:
printf("Enter proper value!\n");
break;
}
}
return 0;
}

You might also like