Following code produces Gantt chart for the SJF-preemtive schedulign.....works for 4 process.
In a Array process one is represented by 0 and so on
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct process
{
int at;
int bt;
int rt;
int c;
int wt;
int trt;
};
int min(int *a,int j)
{int min;
int i=0;
int flag=0;
int pos;
for(i=0;i<=j;i++)
{
if(a[i]==0)
continue;
min=a[i];
pos=i;
flag=1;
break;
}
if(flag==0)
return -1;
else
for(i=0;i<=j;i++)
{
if(a[i]==0)
continue;
if(a[i]<min)
{
min=a[i];
pos=i;
}
}
return pos;
}
void display(struct process *p)
{
int i=1;
while(1)
{
printf("Arrival time%d ",p[i].at);
printf("Burst Time%d ",p[i].bt);
printf("Response Time%d ",p[i].rt);
printf("comlete Time%d ",p[i].c);
printf("waiting Time%d ",(p[i].rt-p[i].at));
printf("\n\n");
i++;
if(i==5)
break;
}
}
main()
{
struct process p[100];
int b[100];
int a[4];//since number of process taken are four
puts("Enetr the process arival time and burst time");
int i=0;
int j=0;
while(1)
{
i++;
scanf("%d",&(p[i].at));
scanf("%d",&(p[i].bt));
a[j]=p[i].bt;
j++;
if(i==4)
break;
}
int x=0;
int y=0;
j=0;
while(x!=-1)
{
a[x]=a[x]-1;
b[y]=x;
y++;
if(j!=3)
j++;
x=min(a,j);
}
printf("\n\n");
for(i=0;i<y;i++)
printf("%d\n",b[i]);
getch();
}
In a Array process one is represented by 0 and so on
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct process
{
int at;
int bt;
int rt;
int c;
int wt;
int trt;
};
int min(int *a,int j)
{int min;
int i=0;
int flag=0;
int pos;
for(i=0;i<=j;i++)
{
if(a[i]==0)
continue;
min=a[i];
pos=i;
flag=1;
break;
}
if(flag==0)
return -1;
else
for(i=0;i<=j;i++)
{
if(a[i]==0)
continue;
if(a[i]<min)
{
min=a[i];
pos=i;
}
}
return pos;
}
void display(struct process *p)
{
int i=1;
while(1)
{
printf("Arrival time%d ",p[i].at);
printf("Burst Time%d ",p[i].bt);
printf("Response Time%d ",p[i].rt);
printf("comlete Time%d ",p[i].c);
printf("waiting Time%d ",(p[i].rt-p[i].at));
printf("\n\n");
i++;
if(i==5)
break;
}
}
main()
{
struct process p[100];
int b[100];
int a[4];//since number of process taken are four
puts("Enetr the process arival time and burst time");
int i=0;
int j=0;
while(1)
{
i++;
scanf("%d",&(p[i].at));
scanf("%d",&(p[i].bt));
a[j]=p[i].bt;
j++;
if(i==4)
break;
}
int x=0;
int y=0;
j=0;
while(x!=-1)
{
a[x]=a[x]-1;
b[y]=x;
y++;
if(j!=3)
j++;
x=min(a,j);
}
printf("\n\n");
for(i=0;i<y;i++)
printf("%d\n",b[i]);
getch();
}
No comments:
Post a Comment