priority scheduling program in c cpp | priority scheduling algorithm | priority scheduling program in cpp

priority scheduling program in c | priority scheduling program in cpp | In priority scheduling algorithm  Each process in the system is given a priority, then the scheduling must be done according to the priority of each process. A higher priority job should get CPU whereas lower priority job can be made to wait. if two process has same priority then scheduling will be done using FCFS. Priority scheduling is necessarily a form of preemptive scheduling where priority is the basis of preemption.

problem with priority scheduling algorithm

The problem occurs when the operating system gives a particular task a very low priority, so it sits in the queue for a larger amount of time, not being dealt with by the CPU. If this process is something the user needs, there could be a very long wait, this process is known as “Starvation”.

priority scheduling program in c

#include<bits/stdc++.h>
using namespace std;
struct Process
{
int p;
int bt;
int pr;
};
bool comparison(Process a, Process b)
{
return (a.pr > b.pr);
}
int main()
{
int n,i;
printf(“Total number of process: “);
scanf(“%d”,&n);
Process p[5];
for(i=0;i<n;i++)
{
p[i].p=i+1;
printf(“Enter burst time for process[%d]:- “,i+1);
scanf(“%d”,&p[i].bt);
printf(“Enter pr for process[%d]:- “,i+1);
scanf(“%d”,&p[i].pr);
}
sort(p, p+ n, comparison);
int wt[n], tat[n], total_wt = 0, total_tat = 0;
wt[0] = 0;
for (int i = 1; i < n ; i++ )
wt[i] = p[i-1].bt + wt[i-1] ;
for (int i = 0; i < n ; i++)
tat[i] = p[i].bt + wt[i];
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
}
float temp=(float)total_wt / (float)n;
float temp1=(float)total_tat / (float)n;
printf(“\nAverage waiting time = %f”,temp);
printf(“\nAverage turn around time =%f”,temp1);
return 0;
}

priority scheduling program in c | priority scheduling algorithm

priority scheduling program in cpp/c++

#include<bits/stdc++.h>
using namespace std;
struct Process
{
int p;
int bt;
int pr;
};
bool comparison(Process a, Process b)
{
return (a.pr > b.pr);
}
int main()
{
int n,i;
cout<<“Total number of process: “;
cin>>n;
Process p[5];
for(i=0;i<n;i++)
{
p[i].p=i+1;
cout<<“Enter burst time for process”<<i+1<<“:- “;
cin>>p[i].bt;
cout<<“Enter pr for process”<<i+1<<“:- “;
cin>>p[i].pr;
}
sort(p, p+ n, comparison);
int wt[n], tat[n], total_wt = 0, total_tat = 0;
wt[0] = 0;
for (int i = 1; i < n ; i++ )
wt[i] = p[i-1].bt + wt[i-1] ;
for (int i = 0; i < n ; i++)
tat[i] = p[i].bt + wt[i];
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
}
float temp=(float)total_wt / (float)n;
float temp1=(float)total_tat / (float)n;
cout<<“\nAverage waiting time = “<<temp;
cout<<“\nAverage turn around time “<<temp1;
return 0;
}

priority scheduling program in cpp

Leave a Reply

Your email address will not be published. Required fields are marked *