0% found this document useful (0 votes)
86 views8 pages

C Program for Priority Scheduling Algorithm

The document contains source code for a C program that implements priority scheduling algorithm. It takes the burst time and priority of processes as input, sorts them in ascending order of priority, calculates waiting time and turnaround time of each process, and prints the average waiting and turnaround times. The code first takes the number of processes as input, then burst time and priority of each one. It sorts the processes in ascending order of priority. It then calculates waiting time of each process and the average. Finally, it prints the details of all processes along with the average waiting and turnaround times.

Uploaded by

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

C Program for Priority Scheduling Algorithm

The document contains source code for a C program that implements priority scheduling algorithm. It takes the burst time and priority of processes as input, sorts them in ascending order of priority, calculates waiting time and turnaround time of each process, and prints the average waiting and turnaround times. The code first takes the number of processes as input, then burst time and priority of each one. It sorts the processes in ascending order of priority. It then calculates waiting time of each process and the average. Finally, it prints the details of all processes along with the average waiting and turnaround times.

Uploaded by

GururajHudgi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

//Name: Gururaj Mallikarjun Hudgi

//Class: SE Computer; Roll No. 15; Batch:S1

//Aim: Write a C program to implement Priority scheduling algorithm.

Source Code:

#include<stdio.h>

#include<conio.h>

int main()

int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,ts=0,pos,temp;

float 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; //contains process number

//sorting burst time, priority and process number in ascending order using selection sort

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; //waiting time for first process is zero

//calculate waiting time

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; //average waiting time

total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");

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

tat[i]=bt[i]+wt[i]; //calculate turnaround time

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; //average turnaround time

printf("\n\nAverage Waiting Time=%f",avg_wt);

printf("\nAverage Turnaround Time=%f\n",avg_tat);

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

printf("| p %d ",p[i]);

printf("| \n");

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

printf("%d ",ts);

ts=ts +bt[i];

return 0;

}
OUTPUT:
//Name: Karishma Gajanan Mokal

//Class: SE Computer; Roll No. 35; Batch:S2

//Aim: Write a C program to implement Priority scheduling algorithm.

Source Code:

#include<stdio.h>

#include<conio.h>

int main()

int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,ts=0,pos,temp;

float 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; //contains process number

//sorting burst time, priority and process number in ascending order using selection sort

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; //waiting time for first process is zero

//calculate waiting time

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; //average waiting time

total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");

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

tat[i]=bt[i]+wt[i]; //calculate turnaround time

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; //average turnaround time

printf("\n\nAverage Waiting Time=%f",avg_wt);

printf("\nAverage Turnaround Time=%f\n",avg_tat);

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

printf("| p %d ",p[i]);

printf("| \n");

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

printf("%d ",ts);

ts=ts +bt[i];

return 0;

}
OUTPUT:

You might also like