Sheduling Algorithms

These algorithms are used to shedule the CPU, depending on different parameters.All the programmes below are in "C" and use file handling.There will be sub headings related to the sheduling algorithms and below them the corresponding code snippets.The code once compiled runs automatically using the data from text file and displays the output.Tested for DevCPP and codeblocks in Windows.Just be careful to keep the input text file in the same folder as programmes and change the name of the input file in the programme to correspond to the text file.If you have doubts, comment, will clarify them, with in at max two days.


Input text file (same for all the algorithms):

3
A 5 30
B 10 20
C 15 10

1.FCFS

#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{FILE *fp;
    int i,n,TET,temp,j;
float AV_WT,AV_TA;
char pro_name[20][10],tempch[10];
int e[20],w[20],ta[20],a[20],sw;
fp=fopen("Input.txt","r");
printf("Enter the number of processes:\n");
fscanf(fp,"%d",&n);
printf("Enter the name of process, it's arrival time and it's execution time, side by side :\n");
for(i=0;i<n;i++)
fscanf(fp,"%s%d%d",&pro_name[i],&a[i],&e[i]);
fclose(fp);
//
for(i=0;i<n;i++)
{sw=0;
for(j=1;j<n-i;j++)
{if(a[j]<a[i])
{temp=a[j];
a[j]=a[i];
a[i]=temp;
//
strcpy(tempch,pro_name[j]);
strcpy(pro_name[j],pro_name[i]);
strcpy(pro_name[i],tempch);
//
temp=e[j];
e[j]=e[i];
e[i]=temp;
sw=sw+1;}
}
if(sw==0)
break;
}
//
printf("The processes you entered after sorting are:\nName\tArr_time\tExec_time\n");
for(i=0;i<n;i++)
printf("%s\t%d\t%d\n",pro_name[i],a[i],e[i]);
w[0]=0;
AV_WT=0;
ta[0]=e[0];
AV_TA=ta[0];
for(i=1;i<n;i++)
{
if(a[i]>(a[i-1]+e[i-1]+w[i-1]))
w[i]=0;
else
w[i]=a[i-1]+e[i-1]+w[i-1]-a[i];
AV_WT=AV_WT+w[i];
ta[i]=w[i]+e[i];
AV_TA=AV_TA+ta[i];
}
AV_WT/=n;
AV_TA/=n;
TET=w[i-1]+e[i-1]+a[i-1]-a[0];
printf("The processes you entered along with waiting times and turn around times are:\n");
printf("Process name   Exectime   Waitingtime   Turnaroundtime\n");
for(i=0;i<n;i++)
                printf("%s\t\t\t%d\t%d\t%d\n",pro_name[i],e[i],w[i],ta[i]);
printf("The total execution time is %d\n\nThe avarage waiting time is %f\n\nThe avarage turn around time is %f\n",TET,AV_WT,AV_TA);
getche();
return 0;
}

2.Preemptive (SRTF)

#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{FILE *fp;
    int i,n,fl,k,j,min,temp,MT=0,sw=0,flag,t;
float AV_WT,AV_TA;
char pro_name[20][10],tempch[10];
int e[20],w[20],ta[20],a[20],l[20],ex[20],pr[20];
fp=fopen("Input.txt","r");
printf("Enter the number of processes:\n");
fscanf(fp,"%d",&n);
for(i=0;i<n;i++)
l[i]=0;
printf("Enter the name of process, it's arrival time and it's execution time, side by side as per the order they arrived:\n");
for(i=0;i<n;i++)
fscanf(fp,"%s%d%d",&pro_name[i],&a[i],&e[i]);
fclose(fp);
for(i=0;i<n;i++)
{sw=0;
for(j=1;j<n-i;j++)
{if(a[j]<a[i])
{temp=a[j];
a[j]=a[i];
a[i]=temp;
strcpy(tempch,pro_name[j]);
strcpy(pro_name[j],pro_name[i]);
strcpy(pro_name[i],tempch);
temp=e[j];
e[j]=e[i];
e[i]=temp;
sw=sw+1;}
}
if(sw==0)
break;
}
printf("The processes you entered after sorting are:\nName\tArr_time\tExec_time\n");
for(i=0;i<n;i++)
printf("%s\t%d\t%d\n",pro_name[i],a[i],e[i]);
//
for(i=0;i<n;i++)
l[i]=0;
for(i=0;i<n;i++)
ex[i]=e[i];
AV_WT=0;
AV_TA=0;
for(i=0;i<n;i++)
if(a[i]<a[i+1])
break;
i=i+1;
fl=1;
while(fl)
{fl=0;
e[18]=10000;
min=18;
//To find the minimum element
for(j=0;j<i;j++)
if(l[j]!=1)
{if(e[j]<e[min])
min=j;}
flag=1;
while(i<=n)
{flag=0;
          if(MT+e[min]>a[i])
          {flag=1;i++;}
else
break;
if(flag==1)
{
           if(a[min]<MT)
{
              e[min]=e[min]-a[i-1]+MT;
MT=a[i-1];

}
else
{
   e[min]=e[min]-a[i-1]+a[min];
pr[min]=a[i-1]-a[min];
MT=a[i-1];
}
printf("\nThe process executed just is %s",pro_name[min]);
         e[18]=10000;
min=18;
         for(j=0;j<i;j++)
if(l[j]!=1)
{if(e[j]<e[min])
min=j;}
}}
if(flag==0)
{
           if(a[min]<MT)
{MT=MT+e[min];
}
else
{MT=a[min]+e[min];
}
}
printf("\nThe process completed just is %s",pro_name[min]);
l[min]=1;
w[min]=MT-a[min]-ex[min];
ta[min]=w[min]+ex[min];
AV_WT=AV_WT+w[min];
AV_TA=AV_TA+ta[min];
for(i=0;i<n;i++)
if(l[i]!=1)
if(a[i]>MT)
break;

for(j=0;j<n;j++)
if(l[j]==0)
{fl=1;break;}
}
AV_WT/=n;
AV_TA/=n;

printf("\nThe processes you entered along with waiting times and turn around times are:\n");
printf("Process name   Exectime   Waitingtime   Turnaroundtime\n");
for(i=0;i<n;i++)
                printf("%s\t\t\t%d\t%d\t%d\n",pro_name[i],e[i],w[i],ta[i]);
printf("The Final time after execution is %d\n\nThe avarage waiting time is %f\n\nThe avarage turn around time is %f\n",MT,AV_WT,AV_TA);
getche();
         return 0;}


3.Non-Preemptive ( SJNF)

#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{FILE *fp;
    int i,n,fl,k,j,min,temp,MT=0,sw=0;
float AV_WT,AV_TA;
char pro_name[20][10],tempch[10];
int e[20],w[20],ta[20],a[20],l[20];
fp=fopen("Input.txt","r");
printf("Enter the number of processes:\n");
fscanf(fp,"%d",&n);
for(i=0;i<n;i++)
l[i]=0;
printf("Enter the name of process, it's arrival time and it's execution time, side by side as per the order they arrived:\n");
for(i=0;i<n;i++)
fscanf(fp,"%s%d%d",&pro_name[i],&a[i],&e[i]);
fclose(fp);
for(i=0;i<n;i++)
{sw=0;
for(j=1;j<n-i;j++)
{if(a[j]<a[i])
{temp=a[j];
a[j]=a[i];
a[i]=temp;
strcpy(tempch,pro_name[j]);
strcpy(pro_name[j],pro_name[i]);
strcpy(pro_name[i],tempch);
temp=e[j];
e[j]=e[i];
e[i]=temp;
sw=sw+1;}
}
if(sw==0)
break;
}
printf("The processes you entered after sorting are:\nName\tArr_time\tExec_time\n");
for(i=0;i<n;i++)
printf("%s\t%d\t%d\n",pro_name[i],a[i],e[i]);
AV_WT=0;
AV_TA=0;
for(i=0;i<n;i++)
if(a[i]<a[i+1])
break;
i=i+1;
fl=1;
while(fl)
{fl=0;
e[18]=10000;
min=18;
for(j=0;j<i;j++)//To find the minimum element
if(l[j]!=1)
{if(e[j]<e[min])
min=j;}
l[min]=1;
if(a[min]<MT)
MT=MT+e[min];
else
MT=a[min]+e[min];
printf("\nThe process executed just is %s",pro_name[min]);
w[min]=MT-a[min]-e[min];
ta[min]=w[min]+e[min];
AV_WT=AV_WT+w[min];
AV_TA=AV_TA+ta[min];
for(i=0;i<n;i++)
if(l[i]!=1)
if(a[i]>MT)
break;
for(j=0;j<n;j++)
if(l[j]==0)
{fl=1;break;}
}
AV_WT/=n;
AV_TA/=n;
printf("\nThe processes you entered along with waiting times and turn around times are:\n");
printf("Process name   Exectime   Waitingtime   Turnaroundtime\n");
for(i=0;i<n;i++)
                printf("%s\t\t\t%d\t%d\t%d\n",pro_name[i],e[i],w[i],ta[i]);
printf("The Final time after execution is %d\n\nThe avarage waiting time is %f\n\nThe avarage turn around time is %f\n",MT,AV_WT,AV_TA);
getche();
         return 0;}

4.Round Robin Solution

#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{FILE *fp;
    int i,n,fl,k,j,min,temp,MT=0,sw=0,flag;
float AV_WT,AV_TA;
char pro_name[20][10],tempch[10];
int e[20],w[20],ta[20],a[20],l[20],ex[20],ts;
fp=fopen("Input.txt","r");
printf("Enter the number of processes:\n");
fscanf(fp,"%d",&n);
for(i=0;i<n;i++)
l[i]=0;
printf("Enter the name of process, it's arrival time and it's execution time, side by side as per the order they arrived:\n");
for(i=0;i<n;i++)
fscanf(fp,"%s%d%d",&pro_name[i],&a[i],&e[i]);
fclose(fp);
for(i=0;i<n;i++)
{sw=0;
for(j=1;j<n-i;j++)
{if(a[j]<a[i])
{temp=a[j];
a[j]=a[i];
a[i]=temp;
strcpy(tempch,pro_name[j]);
strcpy(pro_name[j],pro_name[i]);
strcpy(pro_name[i],tempch);
temp=e[j];
e[j]=e[i];
e[i]=temp;
sw=sw+1;}
}
if(sw==0)
break;
}
printf("The processes you entered after sorting are:\nName\tArr_time\tExec_time\n");
for(i=0;i<n;i++)
printf("%s\t%d\t%d\n",pro_name[i],a[i],e[i]);
for(i=0;i<n;i++)
l[i]=0;
for(i=0;i<n;i++)
ex[i]=e[i];
AV_WT=0;
AV_TA=0;
ts=2;
fl=1;
MT=0;
while(fl)
{fl=0;
for(i=0;i<n;i++)
{if(l[i]!=1)
{if(MT>a[i])
{
           if(e[i]>ts)
{
           MT=MT+ts;
e[i]=e[i]-ts;
printf("\nThe process executed just is %s",pro_name[i]);
}
else
{
    MT=MT+e[i];
    w[i]=MT-a[i]-ex[i];
ta[i]=w[i]+ex[i];
AV_WT=AV_WT+w[i];
AV_TA=AV_TA+ta[i];
l[i]=1;
printf("\nThe process executed just is %s",pro_name[i]);
}}
else
{
    if(e[i]>ts)
{
MT=a[i]+ts;
e[i]=e[i]-ts;
printf("\nThe process executed just is %s",pro_name[i]);}
else
{
    MT=a[i]+e[i];
w[i]=MT-a[i]-ex[i];
ta[i]=w[i]+ex[i];
AV_WT=AV_WT+w[i];
AV_TA=AV_TA+ta[i];
l[i]=1;
printf("\nThe process executed just is %s",pro_name[i]);
}}}
}
for(j=0;j<n;j++)
if(l[j]==0)
fl=1;
}
//Round Robin end
AV_WT/=n;
AV_TA/=n;
printf("\nThe processes you entered along with waiting times and turn around times are:\n");
printf("Process name   Exectime   Waitingtime   Turnaroundtime\n");
for(i=0;i<n;i++)
                printf("%s\t\t\t%d\t%d\t%d\n",pro_name[i],e[i],w[i],ta[i]);
printf("The Final time after execution is %d\n\nThe avarage waiting time is %f\n\nThe avarage turn around time is %f\n",MT,AV_WT,AV_TA);
getche();
         return 0;}

5.Round Robin With Aging

#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{FILE *fp;
    int i,n,fl,k,j,min,temp,MT=0,sw=0,flag,fl1;
float AV_WT,AV_TA;
char pro_name[20][10],tempch[10];
int e[20],w[20],ta[20],a[20],l[20],ex[20],ts,tsa,pre,ag;
fp=fopen("Input.txt","r");
printf("Enter the number of processes:\n");
fscanf(fp,"%d",&n);
for(i=0;i<n;i++)
l[i]=0;
printf("Enter the name of process, it's arrival time and it's execution time, side by side as per the order they arrived:\n");
for(i=0;i<n;i++)
fscanf(fp,"%s%d%d",&pro_name[i],&a[i],&e[i]);
fclose(fp);
for(i=0;i<n;i++)
{sw=0;
for(j=1;j<n-i;j++)
{if(a[j]<a[i])
{temp=a[j];
a[j]=a[i];
a[i]=temp;
strcpy(tempch,pro_name[j]);
strcpy(pro_name[j],pro_name[i]);
strcpy(pro_name[i],tempch);
temp=e[j];
e[j]=e[i];
e[i]=temp;
sw=sw+1;}
}
if(sw==0)
break;
}
printf("The processes you entered after sorting are:\nName\tArr_time\tExec_time\n");
for(i=0;i<n;i++)
printf("%s\t%d\t%d\n",pro_name[i],a[i],e[i]);
for(i=0;i<n;i++)
l[i]=0;
//To find maximum solution
ag=18;
e[ag]=1;
for(i=0;i<n;i++)
if(e[i]>e[ag])
ag=i;
for(i=0;i<n;i++)
ex[i]=e[i];
AV_WT=0;
AV_TA=0;
//Roundrobin start
ts=2;
fl=1;
MT=0;
tsa=ts;
pre=ts;
while(fl)
{fl=0;
for(i=0;i<n;i++)
{if(l[i]!=1)
{if(MT>a[i])
{fl1=0;
            if(i==ag)
{if(e[i]>tsa)
{fl1=1;
           MT=MT+tsa;
e[i]=e[i]-tsa;
         printf("\nThe process executed just is %s",pro_name[i]); }}
          else
          {
           if(e[i]>ts)
{fl1=1;
           MT=MT+ts;
e[i]=e[i]-ts;
printf("\nThe process executed just is %s",pro_name[i]);}}
if(fl1!=1)
{
    MT=MT+e[i];
    w[i]=MT-a[i]-ex[i];
ta[i]=w[i]+ex[i];
AV_WT=AV_WT+w[i];
AV_TA=AV_TA+ta[i];
l[i]=1;
printf("\nThe process executed just is %s",pro_name[i]);
}}
else
{fl1=0;
    if(i==ag)
{if(e[i]>tsa)
         {fl1=1;
             MT=a[i]+tsa;
e[i]=e[i]-tsa;
          printf("\nThe process executed just is %s",pro_name[i]);
          }}
else
    {if(e[i]>ts)
{fl1=1;
MT=a[i]+ts;
e[i]=e[i]-ts;
printf("\nThe process executed just is %s",pro_name[i]);}}
if(fl1!=1)
{
    MT=a[i]+e[i];
w[i]=MT-a[i]-ex[i];
ta[i]=w[i]+ex[i];
AV_WT=AV_WT+w[i];
AV_TA=AV_TA+ta[i];
l[i]=1;
printf("\nThe process executed just is %s",pro_name[i]);
}}}
}
for(j=0;j<n;j++)
if(l[j]==0)
fl=1;
tsa=2*pre+tsa;
pre=2*pre;
}
AV_WT/=n;
AV_TA/=n;
printf("\nThe processes you entered along with waiting times and turn around times are:\n");
printf("Process name   Exectime   Waitingtime   Turnaroundtime\n");
for(i=0;i<n;i++)
                printf("%s\t\t\t%d\t%d\t%d\n",pro_name[i],e[i],w[i],ta[i]);
printf("The Final time after execution is %d\n\nThe avarage waiting time is %f\n\nThe avarage turn around time is %f\n",MT,AV_WT,AV_TA);
getche();
         return 0;}

1 comment: