CPU Scheduling Algorithms......

avatar

0_4r78AS31Nd_P88mX.jfif

In this post i will be showing different types of CPU Scheduling Algorithms in C language.

FCFS Scheduling:

#include<stdio.h>
struct process
{
char processname[5];
int arrivaltime;
int bursttime;

int waitingtime;
int turnaroundtime;

int starttime;
int endtime;

};

int main()
{
int n;
float averagewaiting, averageturnaround;
printf("Enter the total number of processes:");
scanf("%d", &n);

struct process allprocess[n], temp;


for(int i=0; i<n; i++)
{
    allprocess[i].processname[0]='P';
    allprocess[i].processname[1]= (char)(i+1 + '0');         //creating process names for all processes.
    allprocess[i].processname[2]='\0';
}
printf("Enter the CPU brust time for all processes:\n");
for(int i=0; i<n; i++)
{
    printf("P%d:  ", i+1);
    scanf("%d", &allprocess[i].bursttime);                   //getting the burst times.
    
}


printf("Enter the Arrival times of each processes:\n");
for(int i=0; i<n; i++)
{
    printf("P%d:  ", i+1);
    scanf("%d", &allprocess[i].arrivaltime);                 //getting arrivaltimes.
}



for(int i=(n-1); i>=0; i--)
{
    for(int j=1; j<=i; j++)
    if(allprocess[j].arrivaltime < allprocess[j-1].arrivaltime)
    {
        temp = allprocess[j];
        allprocess[j] = allprocess[j-1];                         
        allprocess[j-1] = temp;
    }
    
}
int time = allprocess[0].arrivaltime;

int total_turnaroundtime=0, total_waitingtime=0;
for(int i=0; i<n;  i++)
{
    allprocess[i].waitingtime = time - allprocess[i].arrivaltime;
    allprocess[i].turnaroundtime = allprocess[i].waitingtime + allprocess[i].bursttime;
    
    allprocess[i].starttime = time;
    
    time += allprocess[i].bursttime;                
    
    allprocess[i].endtime = time;
    
    
    total_turnaroundtime += allprocess[i].turnaroundtime;
    total_waitingtime += allprocess[i].waitingtime;
}



    printf("Process | BurstTime | ArrivalTime | TurnaroundTime | WaitingTime\n");  

for(int i=0; i<n; i++)
{
    printf("  %s           %d           %d             %d              %d\n", allprocess[i].processname, allprocess[i].bursttime, allprocess[i].arrivaltime, allprocess[i].turnaroundtime, allprocess[i].waitingtime);   
    printf("                                           \n");
}

printf("\n\n\n\nThe Gantt Chart of FCFS Algorithm based scheduling for given information is as follows:\n");
int t=n; 
while(t--){printf("-----------");}
printf("\n");
for(int i=0; i<n; i++)
{
    printf("|    %s    ",  allprocess[i].processname );         
} 
printf("|\n");

t=n;
while(t--){printf("-----------");}
printf("\n");

for(int i=0; i<n; i++)
{
    printf("%d          ", allprocess[i].starttime);
}
printf("%d\n", allprocess[n-1].endtime);
printf("\n\n\n\n");

averagewaiting = ((float)total_waitingtime) / n;
averageturnaround = ((float)total_turnaroundtime) / n;
printf("The average waiting time is: %f\n", averagewaiting);
printf("The average turnaround time is: %f\n", averageturnaround);  
return 0;

}

Shortest Remaining Time First:

#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);

printf("\nEnter Burst Time:\n");
for(i=0;i<n;i++)
{
    printf("p%d:",i+1);
    scanf("%d",&bt[i]);
    p[i]=i+1;         
}

for(i=0;i<n;i++)
{
    pos=i;
    for(j=i+1;j<n;j++)
    {
        if(bt[j]<bt[pos])
            pos=j;
    }

    temp=bt[i];
    bt[i]=bt[pos];
    bt[pos]=temp;

    temp=p[i];
    p[i]=p[pos];
    p[pos]=temp;
}

wt[0]=0;            


for(i=1;i<n;i++)
{
    wt[i]=0;
    for(j=0;j<i;j++)
        wt[i]+=bt[j];

    total+=wt[i];
}

avg_wt=(float)total/n;      
total=0;

printf("\nProcesst    Burst Time    \tWaiting TimetTurnaround Time");
for(i=0;i<n;i++)
{
    tat[i]=bt[i]+wt[i];   
    total+=tat[i];
    printf("\np%d\t\t  %d\t\t    %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}

avg_tat=(float)total/n;    
printf("\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%fn",avg_tat);

}

Priority Queue:

#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf("Enter Total Number of Process:");
scanf("%d",&n);

printf("\nEnter Burst Time and Priority\n");
for(i=0;i<n;i++)
{
    printf("\nP[%d]\n",i+1);
    printf("Burst Time:");
    scanf("%d",&bt[i]);
    printf("Priority:");
    scanf("%d",&pr[i]);
    p[i]=i+1;          
}

for(i=0;i<n;i++)
{
    pos=i;
    for(j=i+1;j<n;j++)
    {
        if(pr[j]<pr[pos])
            pos=j;
    }

    temp=pr[i];
    pr[i]=pr[pos];
    pr[pos]=temp;

    temp=bt[i];
    bt[i]=bt[pos];
    bt[pos]=temp;

    temp=p[i];
    p[i]=p[pos];
    p[pos]=temp;
}

wt[0]=0; 

for(i=1;i<n;i++)
{
    wt[i]=0;
    for(j=0;j<i;j++)
        wt[i]+=bt[j];

    total+=wt[i];
}

avg_wt=total/n;    
total=0;

printf("\nProcess\t    Burst Time    \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
    tat[i]=bt[i]+wt[i];    
    total+=tat[i];
    printf("\nP[%d]\t\t  %d\t\t    %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}

avg_tat=total/n;    
printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);

return 0;
}

Shortest Job First:

#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
printf("\nEnter Burst Time:\n");
for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n;
total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n;
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
}

Round Robin Scheduling:

#include<stdio.h>

int main()
{
int i, limit, total = 0, x, counter = 0, time_quantum;
int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], temp[10];
float average_wait_time, average_turnaround_time;
printf("Enter Total Number of Processes:");
scanf("%d", &limit);
x = limit;
for(i = 0; i < limit; i++)
{
printf("\nEnter Details of Process[%d]\n", i + 1);

        printf("Arrival Time:");

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

        printf("Burst Time:");

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

        temp[i] = burst_time[i];
  }

  printf("\nEnter Time Quantum:");
  scanf("%d", &time_quantum);
  printf("\nProcess ID Burst Timet Turnaround Time Waiting Time\n");
  for(total = 0, i = 0; x != 0;)
  {
        if(temp[i] <= time_quantum && temp[i] > 0)
        {
              total = total + temp[i];
              temp[i] = 0;
              counter = 1;
        }
        else if(temp[i] > 0)
        {
              temp[i] = temp[i] - time_quantum;
              total = total + time_quantum;
        }
        if(temp[i] == 0 && counter == 1)
        {
              x--;
              printf("\nProcess[%d]\t\t%d\t\t %d\t\t\t %d", i + 1, burst_time[i], total - arrival_time[i], total - arrival_time[i] - burst_time[i]);
              wait_time = wait_time + total - arrival_time[i] - burst_time[i];
              turnaround_time = turnaround_time + total - arrival_time[i];
              counter = 0;
        }
        if(i == limit - 1)
        {
              i = 0;
        }
        else if(arrival_time[i + 1] <= total)
        {
              i++;
        }
        else
        {
              i = 0;
        }
  }

  average_wait_time = wait_time * 1.0 / limit;
  average_turnaround_time = turnaround_time * 1.0 / limit;
  printf("\nAverage Waiting Time:\t%f", average_wait_time);
  printf("\nAvg Turnaround Time:\t%fn", average_turnaround_time);
  return 0;

}



0
0
0.000
0 comments