Write the simulation program for non-preemptive scheduling algorithm using SJF. The arrival time and first CPU bursts of different jobs should be input to the system. The output should give the Gantt chart and Turnaround time for each process and average time.
Program:
#include <stdio.h>
#include <stdlib.h>
void main(){
setbuf(stdout,NULL);
int f,max[10][10]={0,0},al[10][10]={0,0},need[10][10]={0,0},av[10]={0},i,j,m,n,cp[10]={0},sf[10];
printf("\nEnter the number of processes:");
scanf("%d",&m);
printf("\nEnter the number of resources:");
scanf("%d",&n);
printf("\nEnter Max matrix:");
for(i=0;i<n;i++){
for(j=0;j<m;j++){
scanf("%d",&max[i][j]);
}
}
printf("Enter the allocation matrix:");
for(i=0;i<n;i++){
for(j=0;j<m;j++){
scanf("%d",&al[i][j]);
}
}
printf("Enter available matrix:");
for(i=0;i<n;i++){
scanf("%d",&av[i]);
}
for(i=0;i<n;i++){
for(j=0;j<m;j++){
need[i][j]=max[i][j]-al[i][j];
}
}
for(i=0;i<n;i++){
if(cp[i]==0){
f=1;
for(j=0;j<m;j++){
if(need[i][j] > av[i]){
f=0;
}
}
if(f){
for(j=0;j<m;j++){
av[i]+=al[i][j];
cp[i]=1;
printf("%d|",i);
}
}
}
}
for(i=0;i<m;i++){
if(cp[i]==0){
f=1;
for(j=0;j<m;j++){
if(need[i][j]>av[i]){
f=0;
}
}
if(f==0){
printf("\nDead Lock is occurred.");
exit(1);
}
}
}
printf("\nDead lock is not occurred");
}
Tags:
c program
disk scheduling
non-preemptive scheduling
programming
scheduling algorithm
shortest job first
simulation program
sjf algorithm
unix program
