//PROGRAM FOR SHORTEST-JOB-FIRST(SJF) "CPU SCHEDULING ALGORITHM" WITHOUT PRE_EMPTION
#include<stdio.h>
#include<conio.h>
int main()
{
int at[10], bt[10], ct[10], wt[10], ta[10], tat[10];
//at-ArritvalTime::br-BurstTime::ct-CompletionTime::ta-TemporaryArray
//wt-WaitingTime::tat-TurnAroundTime::tn-CurrentTime(TimeNow)
int n, i, k, pc=0, pointer = 0, tn =0, c;//pc-ProcessesCompleted
char pn[10][10]; //pn-ProcessName
printf("Enter the number of processes: ");
scanf("%d",&n);
printf("Enter <ProcessName> <ArrivalTime> <BurstTime>\n");
for(i=0;i<n;i++)
scanf("%s%d%d",&pn[i],&at[i],&bt[i]);
for(i=0; i<n; i++)
{
ct[i] = -1;
ta[i] = bt[i];
}
while(pc!=n)
{
c = 0;
for(i=0; i<n; i++)
if(ct[i]<0 && at[i]<=tn)
c++;
if(c==0)
tn++;
else
{
pointer = 0;
while(at[pointer]>tn || ct[pointer]>0)
pointer++;
for(k=pointer+1; k<n; k++)
if(at[k]<=tn && ct[k]<0 && bt[pointer]>bt[k])
pointer = k;
if(ct[pointer]<0)
{
tn=tn+bt[pointer];
bt[pointer] = 0;
ct[pointer] = tn;
wt[pointer] = ct[pointer] - ( at[pointer]+ ta[pointer] );
tat[pointer] = ct[pointer] - at[pointer];
pc++;
}
}
}
printf("\nPN\tAT\tBT\tCT\tWT\tTAT\n");
for(i=0;i<n;i++)
printf("%s\t%d\t%d\t%d\t%d\t%d\n",pn[i],at[i],ta[i],ct[i],wt[i],tat[i]);
return 0;
}
/* EXAMPLE OUTPUT
Enter the number of processes: 6
Enter <ProcessName> <ArrivalTime> <BurstTime>
p1 3 2
p2 6 5
p3 1 1
p4 6 2
p5 15 3
p6 20 10
PN AT BT CT WT TAT
p1 3 2 5 0 2
p2 6 5 13 2 7
p3 1 1 2 0 1
p4 6 2 8 0 2
p5 15 3 18 0 3
p6 20 10 30 0 10
*/
//ANOTHER PROGRAM for the same problem
//PROGRAM FOR SHORTEST-JOB-FIRST(SJF) "CPU SCHEDULING ALGORITHM" WITH NO-PRE_EMPTION & ARRIVAL TIMES
#include<stdio.h>
#include<string.h>
int main(void)
{
//VARIABLE DECLARATION
char pn[20][20], c[20][20]; //PN-PROGRAM NAMES C-A TEMPORARY ARRAY
int n,i,j,at[20], bt[20], wt[20],ct[20],tat[20]; //bt-BURST TIME ; wt-WAITING TIME; tat-TURN AROUND TIME
int temp1, temp2, count=0,twt=0;//,tat=0;
printf("Enter number of processes:");
scanf("%d", &n);
printf("Enter PN, AT, BT:\n");
//TAKING INPUT VALUES i.e., PROCESS-NAMES, ARRIVAL-TIMES AND BURST-TIMES
for(i=0; i<n; i++)
scanf("%s%d%d",&pn[i],&at[i],&bt[i]);
//SCHEDULING THE PROCESSES ACCORDING TO SJF
for(i=0 ; i<n; i++)
{
for(j=i+1; j<n; j++)
if (
//IF THERE IS NO PROCESS IN MAIN MEMORY,
//SORT PROCESSES ACCORDING TO ARRIVAL TIMES &
//IF WE GOT PROCESSES WITH SAME ARRIVAL TIME SORT THEM BY BURST TIMES
( (i==0||count<1)&&(at[i]>at[j]||(at[i]==at[j]&&bt[i]>bt[j])) )
//IF WE GOT ONLY ONE PROCESS IN MAIN MEMORY AFTER COMPLETION OF THE CURRENT PROCESS
|| (count == 1 && ct[i-1]>=at[j])
//IF WE GOT MORE THAN ONE PROCESS IN MAIN MEMORY, SORT THEM BY BURST TIMES
|| ((ct[i-1]>=at[j]&&bt[i]>bt[j]))// || (ct[i-1]<at[i]&&ct[i-1]>=at[j]))
)
//SWAPPING PROCESSES
{
temp1 = bt[i];
temp2 = at[i];
bt[i] = bt[j];
at[i] = at[j];
bt[j] = temp1;
at[j] = temp2;
strcpy(c[i],pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],c[i]);
}
if(i==0 || count<1)
ct[i] = at[i] + bt[i];
else
ct[i] = ct[i-1] + bt[i];
wt[i] = ct[i] - (at[i] + bt[i]);
tat[i] = ct[i] - at[i];
count = 0 ;
for(j=i+1; j<n; j++)
if(ct[i]>=at[j])
count++;
}
//PRINTING THE VALUES AFTER ALL PREOCESSES COMPLETED
printf("S.N.\tProcess-Name\tArrival-Time\tBurst-Time\tCT\tWT\tTAT\n");
for(i=0; i<n; i++)
printf("%d\t\t%s\t\t%d\t\t%d\t%d\t%d\t%d\n",(i+1),pn[i],at[i],bt[i],ct[i],wt[i],tat[i]);
} //END OF MAIN
/* EXAMPLE OUTPUT
Enter number of processes:6
Enter PN, AT, BT:
p1 3 2
p2 6 5
p3 1 1
p4 6 2
p5 15 3
p6 20 10
S.N. Process-Name Arrival-Time Burst-Time CT WT TAT
1 p3 1 1 2 0 1
2 p1 3 2 5 0 2
3 p4 6 2 8 0 2
4 p2 6 5 13 2 7
5 p5 15 3 18 0 3
6 p6 20 10 30 0 10
*/
No comments:
Post a Comment