0% found this document useful (0 votes)
7 views3 pages

PRIORITY Scheduling Round Robin

This C program implements a scheduling algorithm for processes based on their arrival time and priority. It calculates completion, waiting, and turnaround times for each process and outputs the average waiting and turnaround times. The program prompts the user to input the number of processes and their respective attributes before performing the calculations.

Uploaded by

yo boi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

PRIORITY Scheduling Round Robin

This C program implements a scheduling algorithm for processes based on their arrival time and priority. It calculates completion, waiting, and turnaround times for each process and outputs the average waiting and turnaround times. The program prompts the user to input the number of processes and their respective attributes before performing the calculations.

Uploaded by

yo boi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CODE

#include <stdio.h>

struct Process
{
int pid;
float burst;
float wait;
float turn;
float arr;
float comp;
int priority;
};

void calc_comp(struct Process* temp,int n)


{
temp[0].comp = temp[0].burst;
for(int i=1;i<n;i++)
{
temp[i].comp = temp[i-1].comp + temp[i].burst;
}
}

void calc_wait(struct Process* temp,int n)


{
temp[0].wait = 0;
for(int i=1;i<n;i++)
{
temp[i].wait= temp[i-1].comp - temp[i].arr;
}
}

void calc_turn(struct Process* temp,int n)


{
temp[0].turn = temp[0].burst;
for(int i=1;i<n;i++)
{
temp[i].turn = temp[i].wait + temp[i].burst;
}
}

float avg_wait(struct Process* temp,int n)


{
float avg = 0;
for(int i=0;i<n;i++)
{
avg+= temp[i].wait;
}
return avg/n;
}
float avg_turn(struct Process* temp,int n)
{
float avg=0;
for(int i=0;i<n;i++)
{
avg+=temp[i].turn;
}
return avg/n;
}

void sort (struct Process* temp,int n)


{
int t=0;
for(int i=0;i<n-1;i++)
{
int k = i;
for(int j=i+1;j<n;j++)
{
if(temp[j].arr <= t && temp[j].priority<=temp[k].priority)
{
if(temp[j].priority!=temp[k].priority)
k = j;
else if(temp[j].arr<temp[k].arr)
k = j;
}
}
struct Process trial;
trial = temp[i];
temp[i] = temp[k];
temp[k] = trial;
t = t+temp[i].burst;
}
}

void main()
{
int n;
printf("Enter the number of processes:");
scanf("%d",&n);
struct Process pro[n];

for(int i=0;i<n;i++)
{
printf("Enter the arrival time of the process %d:",i+1);
scanf("%f",&pro[i].arr);
printf("Enter the burst time of process %d: ",i+1);
scanf("%f",&pro[i].burst);
printf("Enter the priority of process %d: ",i+1);
scanf("%d",&pro[i].priority);
pro[i].pid = i+1;
}
sort(pro,n);
calc_comp(pro,n);
calc_wait(pro,n);
calc_turn(pro,n);

printf("Process id\tarrival time\twaiting time\tburst time\tcompletion time\tturnaround


time\n");

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

printf("%d\t\t%f\t%f\t%f\t%f\t%f\n",pro[i].pid,pro[i].arr,pro[i].wait,pro[i].burst,pro[i].comp,
pro[i].turn);
}
printf("Average waiting time = %f\nAverage turnaround time =
%f\n",avg_wait(pro,n),avg_turn(pro,n));

printf("\t");
for(int i=0;i<n;i++)
{
printf("P%d\t|\t",pro[i].pid);
}
printf("\n0\t");
int i;
for(i=0;i<n;i++)
{
printf("%f\t",pro[i].comp);
}
}

OUTPUT

You might also like