Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Thursday, 16 November 2017

Preemptive Scheduling

          C Program To implement Preemptive Scheduling


#include<stdio.h>
int n,a[10],b[10],pr[10],p[10],c[10],t[10],w[10],a1[100],p1[100],c1[100],pr1[100],s[10]={0};
void disp()
{
int i;
printf(" \npid\t a\t b\t pr\t w\t c\t t\t\n ");
for(i=0;i<n;i++)
{
printf("\n%d\t%d\t%d\t%d\t%d\t%d\t%d",p[i],a[i],b[i],pr[i],w[i],c[i],t[i]);
}
}
main()
{int i,j,h,x,y,z,max,t,k=0;
printf(" eneter no of procesese");
scanf("%d",&n);
h=n;
for(i=0;i<n;i++)
{
printf("enter arrival and burst and priority");
p[i]=i;
scanf("%d%d%d",&a[i],&b[i],&pr[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[1+j])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
t=b[j];
b[j]=b[j+1];
b[j+1]=t;
t=pr[j];
pr[j]=pr[j+1];
pr[j+1]=t;
t=p[j];
p[j]=p[j+1];
p[j+1]=t;
}
}
}
disp();
x=a[0];
max=0;
for(i=1;i<n;i++)
{
if(a[i]<=x)
{
if(pr[max]<pr[i]) max=i;
}
printf("namaste1");
}
xy:
x= b[max]+x;//crr
s[max]=2;
for(i=0;i<n;i++)
{ if(s[i]!=2)
if(b[i]!=0)
{
if(a[i]<=x)
{
if(p[y]<pr[i]) y=i;
}
printf("namaste2");
}
}
s[max]=0;
printf("namaste3");
if((a[y]-a[max])<=b[max])
{
z=(a[y]-a[max]);
pr1[k]=pr[max];
p1[k]=p[max];
a1[k]=a[max];
c1[k]=x+z;
b[max]=b[max]-(a[y]-a[max]);
if(b[max]==0)
{
h--;
}
}
else
{
printf("namaste 4");
z=(a[y]-a[max]);
pr1[k]=pr[max];
p1[k]=p[max];
a1[k]=a[max];
c1[k]=x+b[max];
b[max]=0;
h--;
printf("\n%d\t%d\t%d",p1[k],a1[k],c1[k],pr1[k]);
k++;
p1[k]=-1;
a1[k]=-1;
}
printf("\n%d\t%d\t%d",p1[k],a1[k],c1[k],pr1[k]);
k++;
x=c1[k-1];
max= y;
if(h!=0)
goto xy; else
{
printf ("\n program done\n pid \t arrival\t priority \t completio");
for(i=0;i<n;i++)
{
printf("\n%d\t%d\t%d\t%d",p1[i],a1[i],pr[i],c1[i]);
} }
}
x

No comments:

Post a Comment