PROGRAM CODING:-
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct process
{
char id[3];
int arrival,burst,turn,wait,end,remain;
};
int main()
{
int n,i,j,ch,preend,over,count,timer,tottime;
float avgtt=0,avgwt=0;
char order[30][3],c[3];
struct process p[10],temp;
printf("\n||IMPLEMENTATION OF CPU SCHEDULING||\n\n");
printf("\nEnter the number of PROCESSES:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the PROCESS ID of %d process:",i+1);
scanf("%s",p[i].id);
printf("Enter the ARRIVAL TIME of %d process:",i+1);
scanf("%d",&p[i].arrival);
printf("Enter the BURST TIME of %d process:",i+1);
scanf("%d",&p[i].burst);
}
do
{for(i=0;i<n;i++)
for(j=0;j<i;j++)
if(p[j].arrival>p[j+1].arrival)
{temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
for(i=0;i<n;i++)
{p[i].wait=0;
p[i].end=0;
p[i].turn=0;
}
printf("\n\nMENU\n\n1.FCFS\n\n2.SJF non-preemptive\n\n3.SJF preemptive\n\n4.Exit\n\n");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{case 1:
printf("\n\t\t----------------------------------");
printf("\n\t\tFIRST COME FIRST SERVE ALGORITHM\n");
printf("\n\t\t----------------------------------");
p[0].wait=0;
p[0].turn=p[0].burst;
p[0].end=p[0].turn;
for(i=1;i<n;i++)
{p[i].wait=p[i-1].end-p[i].arrival;
p[i].turn=p[i].wait+p[i].burst;
p[i].end=p[i-1].end+p[i].burst;
}
printf("\n-------------------------------------------------------------------------------------------------------------");
printf("\n\n|ID |\tARRIVAL TIME |\tBURST TIME |\tWAITING TIME |\tTURNAROUND TIME |");
printf("\n--------------------------------------------------------------------------------------------------------------");
for(i=0;i<n;i++)
{
printf("\n|%s |\t%d |\t%d | \t%d |\t%d | \t",p[i].id,p[i].arrival,p[i].burst,p[i].wait,p[i].turn);
avgwt+=p[i].wait;
avgtt+=p[i].turn;
}
avgwt/=n;
avgtt/=n;
printf("\n-------------------------------------------------------------------------------");
printf("\n\t\t Average: |\t %f |\t%f",avgwt,avgtt);
printf("\n-------------------------------------------------------------------------------");
printf("\n\nGantt Chart:\n ");
for(i=0;i<(2*p[n-1].end);i++)
printf("-");
printf("\n|");
for(i=0;i<n;i++)
{for(j=0;j<(p[i].burst-2);j++)
printf(" ");
printf("%s",p[i].id);
for(j=0;j<(p[i].burst-1);j++)
printf(" ");
printf("|");
}
printf("\n ");
for(i=0;i<(2*p[n-1].end);i++)
printf("-");
printf("\n0");
for(i=0;i<n;i++)
{for(j=0;j<(2*p[i].burst)-1;j++)
printf(" ");
printf("%d",p[i].end);
}
break;
case 2:
printf("\n\t\t--------------------------------------------------------------------------------");
printf("\n\t\tSHORTEST JOB FIRST ALGORITHM (NON-PREEMPTIVE)\n");
printf("\n\t\t-------------------------------------------------------------------------------");
preend=0;
over=0;
count=0;
avgtt=0;avgwt=0;
while(over<n)
{count=0;
for(i=over;i<n;i++)
if(p[i].arrival<=preend)
count++;
for(i=0;i<count;i++)
for(j=0;j<i;j++)
if(p[j+over].burst>p[j+1+over].burst)
{temp=p[j+over];
p[over+j]=p[j+over+1];
p[j+over+1]=temp;
}
p[over].wait=preend-p[over].arrival;
p[over].turn=p[over].wait+p[over].burst;
p[over].end=preend+p[over].burst;
preend=p[over].end;
over++;
}
printf("\n-------------------------------------------------------------------------------------------------------------");
printf("\n\n|ID |\tARRIVAL TIME |\tBURST TIME |\tWAITING TIME |\tTURNAROUND TIME |");
printf("\n-------------------------------------------------------------------------------------------------------------");
for(i=0;i<n;i++)
{
printf("\n|%s |\t%d |\t%d | \t%d |\t%d | \t",p[i].id,p[i].arrival,p[i].burst,p[i].wait,p[i].turn);
avgwt+=p[i].wait;
avgtt+=p[i].turn;
}
avgwt/=n;
avgtt/=n;
printf("\n-------------------------------------------------------------------------------");
printf("\n\t\t Average: |\t %f |\t%f",avgwt,avgtt);
printf("\n-------------------------------------------------------------------------------");
printf("\n\nGantt Chart:\n ");
for(i=0;i<(2*p[n-1].end);i++)
printf("-");
printf("\n|");
for(i=0;i<n;i++)
{for(j=0;j<(p[i].burst-2);j++)
printf(" ");
printf("%s",p[i].id);
for(j=0;j<(p[i].burst-1);j++)
printf(" ");
printf("|");
}
printf("\n ");
for(i=0;i<(2*p[n-1].end);i++)
printf("-");
printf("\n0");
for(i=0;i<n;i++)
{for(j=0;j<(2*p[i].burst)-1;j++)
printf(" ");
printf("%d",p[i].end);
}
break;
case 3:printf("\n\t\t----------------------------------------------------------------");
printf("\n\t\tSHORTEST JOB FIRST ALGORITHM (PREEMPTIVE)\n");
printf("\n\t\t------------------------------------------------------------------------");
over=0;
timer=0;
tottime=0;
avgwt=0;avgtt=0;
for(i=0;i<n;i++)
{p[i].wait=0;
p[i].remain=p[i].burst;
p[i].end=0;
tottime+=p[i].burst;
}
while(timer<tottime)
{count=0;
for(i=over;i<n;i++)
if(p[i].arrival<=timer)
count++;
for(i=0;i<count;i++)
for(j=0;j<i;j++)
if(p[j+over].remain>p[j+1+over].remain)
{temp=p[j+over];
p[j+over]=p[j+over+1];
p[j+over+1]=temp;
}
p[over].remain--;
for(i=over+1;i<n;i++)
p[i].wait++;
strcpy(order[timer],p[over].id);
if(p[over].remain==0)
{p[over].end=timer;
over++;
}
timer++;
}
for(i=0;i<n;i++)
{p[i].wait-=p[i].arrival;
p[i].turn=p[i].burst+p[i].wait;
p[i].end++;
}
printf("\n-------------------------------------------------------------------------------------------------------------");
printf("\n\n|ID |\tARRIVAL TIME |\tBURST TIME|\tWAITING TIME |\tTURNAROUND TIME |");
printf("\n------------------------------------------------------------------------------------------------------------");
for(i=0;i<n;i++)
{
printf("\n|%s |\t%d |\t%d | \t%d |\t%d | \t",p[i].id,p[i].arrival,p[i].burst,p[i].wait,p[i].turn);
avgwt+=p[i].wait;
avgtt+=p[i].turn;
}
avgwt/=n;
avgtt/=n;
printf("\n-------------------------------------------------------------------------------");
printf("\n\t\t Average: |\t %f |\t%f",avgwt,avgtt);
printf("\n-------------------------------------------------------------------------------");
printf("\n\nGantt Chart:\n ");
for(i=0;i<(2*tottime);i++)
printf("-");
printf("\n|");
count=1;
strcpy(c,order[0]);
for(timer=1;timer<tottime;timer++)
{preend=strcmp(c,order[timer]);
if(preend==0)
count++;
else
{for(j=0;j<count-1;j++)
printf(" ");
printf("%s",c);
for(j=0;j<count-2;j++)
printf(" ");
printf("|");
strcpy(c,order[timer]);
count=1;
}
}
for(i=0;i<count-1;i++)
printf(" ");
printf("%s",c);
for(i=0;i<count-2;i++)
printf(" ");
printf("|\n ");
for(i=0;i<(2*tottime);i++)
printf("-");
printf("\n0");
count=1;
strcpy(c,order[0]);
for(timer=1;timer<tottime;timer++)
{preend=strcmp(c,order[timer]);
if(preend==0)
count++;
else
{for(j=0;j<(2*count)-1;j++)
printf(" ");
printf("%d",timer);
strcpy(c,order[timer]);
count=1;
} }
for(i=0;i<(2*count)-1;i++)
printf(" ");
printf("%d",timer);
break;
case 4:exit(0);
default:printf("\nInvalid choice");break;
}
}while(ch!=4);
scanf("%d",&ch);
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct process
{
char id[3];
int arrival,burst,turn,wait,end,remain;
};
int main()
{
int n,i,j,ch,preend,over,count,timer,tottime;
float avgtt=0,avgwt=0;
char order[30][3],c[3];
struct process p[10],temp;
printf("\n||IMPLEMENTATION OF CPU SCHEDULING||\n\n");
printf("\nEnter the number of PROCESSES:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the PROCESS ID of %d process:",i+1);
scanf("%s",p[i].id);
printf("Enter the ARRIVAL TIME of %d process:",i+1);
scanf("%d",&p[i].arrival);
printf("Enter the BURST TIME of %d process:",i+1);
scanf("%d",&p[i].burst);
}
do
{for(i=0;i<n;i++)
for(j=0;j<i;j++)
if(p[j].arrival>p[j+1].arrival)
{temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
for(i=0;i<n;i++)
{p[i].wait=0;
p[i].end=0;
p[i].turn=0;
}
printf("\n\nMENU\n\n1.FCFS\n\n2.SJF non-preemptive\n\n3.SJF preemptive\n\n4.Exit\n\n");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{case 1:
printf("\n\t\t----------------------------------");
printf("\n\t\tFIRST COME FIRST SERVE ALGORITHM\n");
printf("\n\t\t----------------------------------");
p[0].wait=0;
p[0].turn=p[0].burst;
p[0].end=p[0].turn;
for(i=1;i<n;i++)
{p[i].wait=p[i-1].end-p[i].arrival;
p[i].turn=p[i].wait+p[i].burst;
p[i].end=p[i-1].end+p[i].burst;
}
printf("\n-------------------------------------------------------------------------------------------------------------");
printf("\n\n|ID |\tARRIVAL TIME |\tBURST TIME |\tWAITING TIME |\tTURNAROUND TIME |");
printf("\n--------------------------------------------------------------------------------------------------------------");
for(i=0;i<n;i++)
{
printf("\n|%s |\t%d |\t%d | \t%d |\t%d | \t",p[i].id,p[i].arrival,p[i].burst,p[i].wait,p[i].turn);
avgwt+=p[i].wait;
avgtt+=p[i].turn;
}
avgwt/=n;
avgtt/=n;
printf("\n-------------------------------------------------------------------------------");
printf("\n\t\t Average: |\t %f |\t%f",avgwt,avgtt);
printf("\n-------------------------------------------------------------------------------");
printf("\n\nGantt Chart:\n ");
for(i=0;i<(2*p[n-1].end);i++)
printf("-");
printf("\n|");
for(i=0;i<n;i++)
{for(j=0;j<(p[i].burst-2);j++)
printf(" ");
printf("%s",p[i].id);
for(j=0;j<(p[i].burst-1);j++)
printf(" ");
printf("|");
}
printf("\n ");
for(i=0;i<(2*p[n-1].end);i++)
printf("-");
printf("\n0");
for(i=0;i<n;i++)
{for(j=0;j<(2*p[i].burst)-1;j++)
printf(" ");
printf("%d",p[i].end);
}
break;
case 2:
printf("\n\t\t--------------------------------------------------------------------------------");
printf("\n\t\tSHORTEST JOB FIRST ALGORITHM (NON-PREEMPTIVE)\n");
printf("\n\t\t-------------------------------------------------------------------------------");
preend=0;
over=0;
count=0;
avgtt=0;avgwt=0;
while(over<n)
{count=0;
for(i=over;i<n;i++)
if(p[i].arrival<=preend)
count++;
for(i=0;i<count;i++)
for(j=0;j<i;j++)
if(p[j+over].burst>p[j+1+over].burst)
{temp=p[j+over];
p[over+j]=p[j+over+1];
p[j+over+1]=temp;
}
p[over].wait=preend-p[over].arrival;
p[over].turn=p[over].wait+p[over].burst;
p[over].end=preend+p[over].burst;
preend=p[over].end;
over++;
}
printf("\n-------------------------------------------------------------------------------------------------------------");
printf("\n\n|ID |\tARRIVAL TIME |\tBURST TIME |\tWAITING TIME |\tTURNAROUND TIME |");
printf("\n-------------------------------------------------------------------------------------------------------------");
for(i=0;i<n;i++)
{
printf("\n|%s |\t%d |\t%d | \t%d |\t%d | \t",p[i].id,p[i].arrival,p[i].burst,p[i].wait,p[i].turn);
avgwt+=p[i].wait;
avgtt+=p[i].turn;
}
avgwt/=n;
avgtt/=n;
printf("\n-------------------------------------------------------------------------------");
printf("\n\t\t Average: |\t %f |\t%f",avgwt,avgtt);
printf("\n-------------------------------------------------------------------------------");
printf("\n\nGantt Chart:\n ");
for(i=0;i<(2*p[n-1].end);i++)
printf("-");
printf("\n|");
for(i=0;i<n;i++)
{for(j=0;j<(p[i].burst-2);j++)
printf(" ");
printf("%s",p[i].id);
for(j=0;j<(p[i].burst-1);j++)
printf(" ");
printf("|");
}
printf("\n ");
for(i=0;i<(2*p[n-1].end);i++)
printf("-");
printf("\n0");
for(i=0;i<n;i++)
{for(j=0;j<(2*p[i].burst)-1;j++)
printf(" ");
printf("%d",p[i].end);
}
break;
case 3:printf("\n\t\t----------------------------------------------------------------");
printf("\n\t\tSHORTEST JOB FIRST ALGORITHM (PREEMPTIVE)\n");
printf("\n\t\t------------------------------------------------------------------------");
over=0;
timer=0;
tottime=0;
avgwt=0;avgtt=0;
for(i=0;i<n;i++)
{p[i].wait=0;
p[i].remain=p[i].burst;
p[i].end=0;
tottime+=p[i].burst;
}
while(timer<tottime)
{count=0;
for(i=over;i<n;i++)
if(p[i].arrival<=timer)
count++;
for(i=0;i<count;i++)
for(j=0;j<i;j++)
if(p[j+over].remain>p[j+1+over].remain)
{temp=p[j+over];
p[j+over]=p[j+over+1];
p[j+over+1]=temp;
}
p[over].remain--;
for(i=over+1;i<n;i++)
p[i].wait++;
strcpy(order[timer],p[over].id);
if(p[over].remain==0)
{p[over].end=timer;
over++;
}
timer++;
}
for(i=0;i<n;i++)
{p[i].wait-=p[i].arrival;
p[i].turn=p[i].burst+p[i].wait;
p[i].end++;
}
printf("\n-------------------------------------------------------------------------------------------------------------");
printf("\n\n|ID |\tARRIVAL TIME |\tBURST TIME|\tWAITING TIME |\tTURNAROUND TIME |");
printf("\n------------------------------------------------------------------------------------------------------------");
for(i=0;i<n;i++)
{
printf("\n|%s |\t%d |\t%d | \t%d |\t%d | \t",p[i].id,p[i].arrival,p[i].burst,p[i].wait,p[i].turn);
avgwt+=p[i].wait;
avgtt+=p[i].turn;
}
avgwt/=n;
avgtt/=n;
printf("\n-------------------------------------------------------------------------------");
printf("\n\t\t Average: |\t %f |\t%f",avgwt,avgtt);
printf("\n-------------------------------------------------------------------------------");
printf("\n\nGantt Chart:\n ");
for(i=0;i<(2*tottime);i++)
printf("-");
printf("\n|");
count=1;
strcpy(c,order[0]);
for(timer=1;timer<tottime;timer++)
{preend=strcmp(c,order[timer]);
if(preend==0)
count++;
else
{for(j=0;j<count-1;j++)
printf(" ");
printf("%s",c);
for(j=0;j<count-2;j++)
printf(" ");
printf("|");
strcpy(c,order[timer]);
count=1;
}
}
for(i=0;i<count-1;i++)
printf(" ");
printf("%s",c);
for(i=0;i<count-2;i++)
printf(" ");
printf("|\n ");
for(i=0;i<(2*tottime);i++)
printf("-");
printf("\n0");
count=1;
strcpy(c,order[0]);
for(timer=1;timer<tottime;timer++)
{preend=strcmp(c,order[timer]);
if(preend==0)
count++;
else
{for(j=0;j<(2*count)-1;j++)
printf(" ");
printf("%d",timer);
strcpy(c,order[timer]);
count=1;
} }
for(i=0;i<(2*count)-1;i++)
printf(" ");
printf("%d",timer);
break;
case 4:exit(0);
default:printf("\nInvalid choice");break;
}
}while(ch!=4);
scanf("%d",&ch);
}
No comments:
Post a Comment