Skip to content

Instantly share code, notes, and snippets.

@Robogeek95
Created December 15, 2020 09:15
Show Gist options
  • Save Robogeek95/0dd55859588c96b4136d0f37eb89a80b to your computer and use it in GitHub Desktop.
Save Robogeek95/0dd55859588c96b4136d0f37eb89a80b to your computer and use it in GitHub Desktop.
Ayomide algorithm

1. FCFS CPU SCHEDULING ALGORITHM

For FCFS scheduling algorithm, read the number of processes/jobs in the system, their CPU burst times. The scheduling is performed on the basis of arrival time of the processes irrespective of their other parameters. Each process will be executed according to its arrival time. Calculate the waiting time and turnaround time of each of the processes accordingly.

Algorithm:

#include<stdio.h>

// #include<conio.h>
int main()

{

int bt[20], wt[20], tat[20], i, n;
float wtavg, tatavg;

// clrscr();

printf("\nEnter the number of processes -- ");

scanf("%d", &n);
for(i=0;i<n;i++)

{

printf("\nEnter Burst Time for Process %d -- ", i);

scanf("%d", &bt[i]);
}

wt[0] =  wtavg = 0;

tat[0] = tatavg = bt[0];
for(i=1;i<n;i++)

{

wt[i] = wt[i-1] +bt[i-1];

tat[i] = tat[i-1] +bt[i];
wtavg = wtavg + wt[i];

tatavg = tatavg + tat[i];

}

printf("\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");

for(i=0;i<n;i++)

printf("\n\t P%d \t\t %d \t\t %d \t\t %d", i, bt[i], wt[i], tat[i]); printf("\nAverage Waiting Time -- %f", wtavg/n); printf("\nAverage Turnaround Time -- %f", tatavg/n);
//  getch();
}

solution:

Enter the number of processes -- 5

Enter Burst Time for Process 0 -- 4

Enter Burst Time for Process 1 -- 56

Enter Burst Time for Process 2 -- 18

Enter Burst Time for Process 3 -- 30

Enter Burst Time for Process 4 -- 81
     PROCESS    BURST TIME   WAITING TIME    TURNAROUND TIME

     P0          4       0       4
     P1          56          4       60
     P2          18          60          78
     P3          30          78          108
     P4          81          108         189
Average Waiting Time -- 50.000000
Average Turnaround Time -- 87.800003

2. SJF CPU SCHEDULING ALGORITHM

For SJF scheduling algorithm, read the number of processes/jobs in the system, their CPU burst times. Arrange all the jobs in order with respect to their burst times. There may be two jobs in queue with the same execution time, and then FCFS approach is to be performed. Each process will be executed according to the length of its burst time. Then calculate the waiting time and turnaround time of each of the processes accordingly.

Algorithm:

#include<stdio.h>
int main()
{
int p[20], bt[20], wt[20], tat[20], i, k, n, temp;
float wtavg, tatavg;

printf("\nEnter the number of processes -- ");
scanf("%d", &n);

for(i=0;i<n;i++)

{

p[i]=i;
printf("Enter Burst Time for Process %d -- ", i);

scanf("%d", &bt[i]);

}
for(i=0;i<n;i++)

for(k=i+1;k<n;k++)

if(bt[i]>bt[k])
{

temp=bt[i];

bt[i]=bt[k];

bt[k]=temp;

temp=p[i];

p[i]=p[k];

p[k]=temp;
}

wt[0] =  wtavg = 0;

tat[0] = tatavg = bt[0];
for(i=1;i<n;i++)

{

wt[i] = wt[i-1] +bt[i-1];

tat[i] = tat[i-1] +bt[i];
wtavg = wtavg + wt[i];

tatavg = tatavg + tat[i];

}

printf("\n\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n"); for(i=0;i<n;i++)

printf("\n\t P%d \t\t %d \t\t %d \t\t %d", p[i], bt[i], wt[i], tat[i]); printf("\nAverage Waiting Time -- %f", wtavg/n); printf("\nAverage Turnaround Time -- %f", tatavg/n);
}

solution:

Enter the number of processes -- 4
Enter Burst Time for Process 0 -- 12
Enter Burst Time for Process 1 -- 30
Enter Burst Time for Process 2 -- 50
Enter Burst Time for Process 3 -- 20

     PROCESS    BURST TIME   WAITING TIME    TURNAROUND TIME

     P0          12          0       12
     P3          20          12          32
     P1          30          32          62
     P2          50          62          112
Average Waiting Time -- 26.500000
Average Turnaround Time -- 54.500000

3. ROUND ROBIN CPU SCHEDULING ALGORITHM

For round robin scheduling algorithm, read the number of processes/jobs in the system, their CPU burst times, and the size of the time slice. Time slices are assigned to each process in equal portions and in circular order, handling all processes execution. This allows every process to get an equal chance. Calculate the waiting time and turnaround time of each of the processes accordingly.

Algorithm:

#include<stdio.h>
int main(){

int i,j,n,bu[10],wa[10],tat[10],t,ct[10],max;

float awt=0,att=0,temp=0;
// clrscr();

printf("Enter the no of processes -- ");

scanf("%d",&n);

for(i=0;i<n;i++)

{

printf("\nEnter Burst Time for process %d -- ", i+1);
scanf("%d",&bu[i]);

ct[i]=bu[i];

}

printf("\nEnter the size of time slice -- ");
scanf("%d",&t);

max=bu[0];

for(i=1;i<n;i++)
if(max<bu[i])

max=bu[i];

for(j=0;j<(max/t)+1;j++)

for(i=0;i<n;i++)
if(bu[i]!=0)

if(bu[i]<=t)

{

tat[i]=temp+bu[i];
temp=temp+bu[i];

bu[i]=0;

}
else
{

bu[i]=bu[i]-t;

temp=temp+t;
}

for(i=0;i<n;i++)

{

wa[i]=tat[i]-ct[i];
att+=tat[i];

awt+=wa[i];

}


printf("\nThe Average Turnaround time is -- %f",att/n);

printf("\nThe Average Waiting time is -- %f ",awt/n);

printf("\n\tPROCESS\t BURST TIME \t WAITING TIME\tTURNAROUND TIME\n");

for(i=0;i<n;i++)


printf("\t%d \t %d \t\t %d \t\t %d \n",i+1,ct[i],wa[i],tat[i]);

}

Solution:

Enter the no of processes -- 5

Enter Burst Time for process 1 -- 12

Enter Burst Time for process 2 -- 43

Enter Burst Time for process 3 -- 16

Enter Burst Time for process 4 -- 73

Enter Burst Time for process 5 -- 11

Enter the size of time slice -- 1

The Average Turnaround time is -- 92.000000
The Average Waiting time is -- 61.000000 
    PROCESS  BURST TIME      WAITING TIME   TURNAROUND TIME
    1    12          44          56 
    2    43          81          124 
    3    16          54          70 
    4    73          82          155 
    5    11          44          55 

4. PRIORITY CPU SCHEDULING ALGORITHM

For priority scheduling algorithm, read the number of processes/jobs in the system, their CPU burst times, and the priorities. Arrange all the jobs in order with respect to their priorities. There may be two jobs in queue with the same priority, and then FCFS approach is to be performed. Each process will be executed according to its priority. Calculate the waiting time and turnaround time of each of the processes accordingly.

Algorithm:

#include<stdio.h>

int main()

{
int p[20],bt[20],pri[20], wt[20],tat[20],i, k, n, temp;

float wtavg, tatavg;

printf("Enter the number of processes --- ");
scanf("%d",&n);

for(i=0;i<n;i++)

{
p[i] = i;

printf("Enter the Burst Time & Priority of Process %d --- ",i); scanf("%d %d",&bt[i], &pri[i]);
}

for(i=0;i<n;i++)

for(k=i+1;k<n;k++)

if(pri[i] > pri[k])
{

temp=p[i];

p[i]=p[k];

p[k]=temp;

temp=bt[i];

bt[i]=bt[k];
bt[k]=temp;

temp=pri[i];

pri[i]=pri[k];
pri[k]=temp;

}

wtavg = wt[0] = 0;
tatavg = tat[0] = bt[0];

{

wt[i] = wt[i-1] + bt[i-1];

tat[i] = tat[i-1] + bt[i];

wtavg = wtavg + wt[i];

tatavg = tatavg + tat[i];

}

printf("\nPROCESS\t\tPRIORITY\tBURST TIME\tWAITING TIME\tTURNAROUND TIME"); for(i=0;i<n;i++)

printf("\n%d \t\t %d \t\t %d \t\t %d \t\t %d ",p[i],pri[i],bt[i],wt[i],tat[i]);

printf("\nAverage Waiting Time is --- %f",wtavg/n);

printf("\nAverage Turnaround Time is --- %f",tatavg/n);

}

solution:

Enter the number of processes --- 5
Enter the Burst Time & Priority of Process 0 --- 5
3
Enter the Burst Time & Priority of Process 1 --- 13 20
Enter the Burst Time & Priority of Process 2 --- 40 19
Enter the Burst Time & Priority of Process 3 --- 13 20
Enter the Burst Time & Priority of Process 4 --- 11 22

PROCESS     PRIORITY    BURST TIME  WAITING TIME    TURNAROUND TIME
0        3       5       0       5
2        19          40          0       32701
1        20          13          1700966438          0
3        20          13          0       0
4        22          11          1983920792          -1903357744
Average Waiting Time is --- 396784160.000000
Average Turnaround Time is --- -380671552.000000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment