0% found this document useful (0 votes)
108 views

Process Scheduling Algorithms

It contains solution to three process scheduling algorithms namely: first come first serve scheduling,priority scheduling and multilevel queue scheduling

Uploaded by

Kartik Verma
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)
108 views

Process Scheduling Algorithms

It contains solution to three process scheduling algorithms namely: first come first serve scheduling,priority scheduling and multilevel queue scheduling

Uploaded by

Kartik Verma
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
You are on page 1/ 3

Q 1. The first Come first serve sceduling problem solution.

Solution:
#include<stdio.h>
#define MAX_COUNT 20
int main(){
float bt[MAX_COUNT],wt[MAX_COUNT],tat[MAX_COUNT],av_wt,av_tat,p=0,q=0;
int n,i,j;
printf("Enter the number of processes:(1-20) ");
scanf("%d",&n);
for(i = 0;i < n;++i){
printf("Enter burst time for process %d: ",i+1);
scanf("%f",&bt[i]);
}
av_wt = 0;
av_tat = bt[0];
wt[0] = 0;
tat[0] = bt[0];
for(i = 1;i < n;++i){
wt[i] = wt[i-1]+bt[i-1];
tat[i] = tat[i-1] + bt[i];
av_wt = av_wt + wt[i];
av_tat = av_tat + tat[i];
}
printf("\n\nPID\tBurst Time(ms)\tWaiting time(ms)\tTurn Around time(ms)\n");
printf("------------------------------------------------------------\n");
for(i = 0;i < n;++i){
printf("%3d\t%-14.3f\t%-16.3f\t%-20.3f\n",i+1,bt[i],wt[i],tat[i]);
}
printf("\n\n\tAverage burst time(ms): %.3f\n\tAverage turn around time(ms):
%.3f\n",av_wt/n,av_tat/n
);
return 0;
}

Q2.The priority scheduling algorithm solution


solution:
#include<stdio.h>
int main()
{
float bt[20],wt[20],tat[20];
int pid[20],n,i,j;
float wt_avg, tat_avg;
printf("Enter the number of Processes : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the BurstTime for process %d : ",i+1);
scanf("%f",&bt[i]);
pid[i]=i+1;
}

for(i=1;i<n;i++)
{
int key = bt[i],p = pid[i];
j = i-1;
while(j >= 0 && bt[j] > key){
bt[j+1] =bt[j];
pid[j+1] = pid[j];
j-=1;
}
bt[j+1] = key;
pid[j+1] =p;
}
wt[0]=0;
wt_avg=0;
tat_avg=bt[0];
tat[0]=bt[0];

for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i]=tat[i-1]+bt[i];
wt_avg+=wt[i];
tat_avg+=tat[i];
}

printf("\nPID\tBurstTime\tWaitingTime\tTurnAroundTime\n");
printf("-----------------------------------------------------\n");
for(i=0;i<n;i++)
{
printf("%3d\t%9.3f\t%11.3f\t%14.3f\n",pid[i],bt[i],wt[i],tat[i]);
}
printf("\nAverage WaitingTime : %0.3f",wt_avg/n);
printf("\nAverage TurnAroundTime : %0.3f\n",tat_avg/n);
return 0;
}
Q3.Multilevel queue Scheduling algorithm in process scheduling.
Solution:
#include<iostream>
#include<iomanip>
using namespace std;
struct pcb{
int pid;
float bt,tat,wt;
};
const int SZ = 20;
int main(){
pcb sysProc[SZ],userProc[SZ];
int i,n,type,sys=0,usr=0,p,q;
float temp,wt,tat;
cout<<"Enter the number of processes $ ";
cin>>n;
for(i = 0;i < n;++i){
cout<<"Enter the burst time and type(usr/sys(0/1)) $ ";
cin>>temp>>type;
if(type == 0){
sysProc[sys].bt = temp;
sysProc[sys].pid = (i+1);
sysProc[sys].tat = sysProc[sys].wt = 0;
sys++;
}
else if(type == 1){
userProc[usr].bt = temp;
userProc[usr].pid = (i+1);
userProc[usr].tat = 0;
userProc[usr].wt = 0;
usr++;
}
}
p = 0;q = 0;wt = 0;
for(i = 0;i < n;++i){
if(p < sys){
sysProc[p].wt = wt;
sysProc[p].tat = sysProc[p].bt + wt;
tat+= sysProc[p].tat;
wt += sysProc[p].bt;
p++;
}
else if(q < usr){
userProc[q].wt = wt;
userProc[q].tat = wt + userProc[q].bt;
tat += userProc[q].tat;
wt += userProc[q].bt;
q++;
}
}
p = q = 0;
cout<<"PID SYS/USR BURST_TIME WAITING_TIME
TURN_AROUND_TIME"<<endl;
cout<<"--------------------------------------------------------"<<endl;
for(i = 0;i < n;++i){
if(p < sys){
cout<<setw(3)<<sysProc[p].pid<<" "<<setw(7)<<0<<" "<<setw(10)<<"
"<<sysProc[p].bt<<setw(12)<<" "<<sysProc[p].wt<<setw(16)<<sysProc[p].tat<<endl;
p++;
}
else if(q < usr){
cout<<setw(3)<<userProc[q].pid<<" "<<setw(7)<<1<<" "<<setw(10)<<"
"<<userProc[q].bt<<setw(12)<<" "<<userProc[q].wt<<setw(16)<<userProc[q].tat<<endl;
q++;
}
}
cout<<"Average Waiting Time # "<<(wt/n)<<endl;
cout<<"Average Turn Around Time #"<<(tat/n)<<endl;
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy