0% found this document useful (0 votes)
290 views3 pages

Information Science & Engineering / / BEC Bagalkot, (Karnataka) INDIA.

The document describes a C program that implements the shortest job first (SJF) scheduling algorithm to calculate the average waiting time and average turnaround time of processes. The program accepts the arrival time and burst time of each process as input, sorts the processes by burst time, schedules the processes non-preemptively based on SJF, and calculates the waiting time, turnaround time, average waiting time, and average turnaround time for each process. Sample input/output of running the program on 4 processes is also shown.

Uploaded by

Sharanangadi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
290 views3 pages

Information Science & Engineering / / BEC Bagalkot, (Karnataka) INDIA.

The document describes a C program that implements the shortest job first (SJF) scheduling algorithm to calculate the average waiting time and average turnaround time of processes. The program accepts the arrival time and burst time of each process as input, sorts the processes by burst time, schedules the processes non-preemptively based on SJF, and calculates the waiting time, turnaround time, average waiting time, and average turnaround time for each process. Sample input/output of running the program on 4 processes is also shown.

Uploaded by

Sharanangadi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

/* SHARANABASU G ANGADI */

/* Information Science & Engineering */


/* BEC Bagalkot , (Karnataka) INDIA. */
Write a C/C++ program to compute average waiting time and average
turnaround time for SJF algorithm. The program should accept arrival time and
burst time as input.

$vim 3.c

#include<stdio.h>
typedef struct proc
{ int at,bt,status;
int proc,est;
}P;
P p[15];
int arr[15];
void swap(P *p,P *q)
{ P temp;
temp.at=p->at;
temp.bt=p->bt;
temp.status=p->status;
temp.proc=p->proc;
temp.est=p->est;

p->at=q->at;
p->bt=q->bt;
p->status=q->status;
p->proc=q->proc;
p->est=q->est;

q->at=temp.at;
q->bt=temp.bt;
q->status=temp.status;
q->proc=temp.proc;
q->est=temp.est;
}

void sort_process_by_bt(int n)
{ int i,j;
for(i=0;i<n-1;i++)
{ for(j=i+1;j<n;j++)
{ if(p[i].bt>p[j].bt)
{
swap(&p[i],&p[j]);
}
}
}
}

void cal_proc(int n)
{ int ct=0,i,j,k=0;
up:
for(j=0;j<n;j++)
for(i=0;i<n;i++)
{ if(p[i].status==0 && p[i].at<=ct)
{ p[i].status=1;
p[i].est=ct;
ct+=p[i].bt;
arr[k++]=p[i].proc;
goto up;
}
}
}

int main()
{ int n,i,wt[15],tat[15];
float awt,atat;
printf("\n\n\t\tEnter the no of Process (max 15) : ");
scanf("%d",&n);
printf("\n\n\t\tEnter the arrivel time of process \n");
for(i=0;i<n;i++)
{ printf("\t\tprocess P%d : ",i);
scanf("%d",&p[i].at);
}

printf("\n\n\t\tEnter the Burst time of process \n");


for(i=0;i<n;i++)
{ printf("\t\tprocess P%d : ",i);
scanf("%d",&p[i].bt);
p[i].status=0;
p[i].proc=i;
}
sort_process_by_bt(n);
cal_proc(n);
for(i=0;i<n;i++)
{ wt[i]=p[i].est-p[i].at;
awt+=wt[i];
tat[i]=(p[i].est-p[i].at)+p[i].bt;
atat+=tat[i];
}
awt/=n*1.0;
atat/=n*1.0;
printf("\n\tProc A_T B_T W_T TA_T\t");
printf("\n\t______________________________________\n\n");
for(i=0;i<n;i++)
printf("\t%d\t%d\t%d\t%d\t%d\n",p[i].proc,p[i].at,p[i].bt,wt[i],tat[i]);
printf("\n\t_______________________________________\n");
printf("\n\n\t\tAverage Waiting time = %f\n",awt);
printf("\n\n\t\tAverage Turn around time = %f\n",atat);
return 0;
}
/
*===========================Out_put===================================
===*/
$cc 3.c
$./a.out
Enter the no of Process (max 15) : 4

Enter the arrivel time of process


process P0 : 4
process P1 : 0
process P2 : 7
process P3 : 5

Enter the Burst time of process


process P0 : 5
process P1 : 4
process P2 : 3
process P3 : 6

Proc A_T B_T W_T TA_T


______________________________________
2 7 3 2 5
1 0 4 0 4
0 4 5 0 5
3 5 6 7 13
_______________________________________

Average Waiting time = 2.250000

Average Turn around time = 6.750000


/
*====================================================================
===*/

You might also like