Print Friendly and PDF
PRIORITY SCHEDULING

Program:

#include<stdio.h>
struct process{
int burst,wait,no,priority;
}p[20]={0,0};
int main(){
int n,i,j,totalwait=0,totalturn=0;
printf("\nEnter The No Of Process :");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Enter The Burst Time (in ms) For
Process #%2d :",i+1);
scanf("%d",&p[i].burst);
printf("Enter The Priority For Process
#%2d :",i+1);
scanf("%d",&p[i].priority);
p[i].no=i+1;
}
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(p[j].priority>p[j+1].priority){
p[j].burst^=p[j+1].burst^=p[j].burst^=p[j+1]
.burst;
p[j].no^=p[j+1].no^=p[j].no^=p[j+1].no;
//Simple way to swap 2 var’s
p[j].priority^=p[j+1].priority^=p[j].priority^
=p[j+1].priority;
//printf("j");
}
printf("\nProcess \t Starting Ending
Waiting TurnAround ");
printf("\n \t Time Time Time
Time ");
for(i=0;i<n;i++){
printf("\nProcess # %-11d%-10d%-10d%-
10d%10d",p[i].no,p[i].wait,p[i].wait+p[i].bu
rst,p[i].wait,p[i].wait+p[i].burst);
p[i+1].wait=p[i].wait+p[i].burst;
totalwait=totalwait+p[i].wait;
totalturn=totalturn+p[i].wait+p[i].burst;
}
printf("\n\nAverage\n---------");
printf("\nWaiting Time : %f
ms",totalwait/(float)n);
printf("\nTurnAround Time : %f
ms\n\n",totalturn/(float)n);
return 0;
}
Output:
Enter The No Of Process :3
Enter The Burst Time (in ms) For Process #1 :30
Enter The Priority For Process # 1 :2
Enter The Burst Time (in ms) For Process #2 :20
Enter The Priority For Process # 2 :1
Enter The Burst Time (in ms) For Process #3 :40
Enter The Priority For Process # 3 :3
Process Starting Ending Waiting

TurnAround

Time Time Time Time
Process # 2 0 20 0 20
Process # 1 20 50 20 50
Process # 3 50 90 50 90
Average
---------
Waiting Time : 23.333333 ms
TurnAround Time : 53.333333 ms

ROUND ROBIN SCHEDULING

Program:

#include<stdio.h>
struct process
{
int burst,wait,comp,f;
}
p[20]={0,0};
int main()
{
int
n,i,j,totalwait=0,totalturn=0,quantum,flag=1,
time=0;
printf("\nEnter The No Of Process :");
scanf("%d",&n);
printf("\nEnter The Quantum time (in ms):");
scanf("%d",&quantum);
for(i=0;i<n;i++)
{
printf("Enter The Burst Time (in ms) For
Process #%2d :",i+1);
scanf("%d",&p[i].burst);
p[i].f=1;
}
printf("\nOrder Of Execution \n");
printf("\nProcess Starting Ending
Remaining");
printf("\n Time Time Time");
while(flag==1)
{
flag=0;
for(i=0;i<n;i++)
{
if(p[i].f==1)
{
flag=1;
j=quantum;
if((p[i].burst-p[i].comp)>quantum)
{
p[i].comp+=quantum;
}
else
{
p[i].wait=time-p[i].comp;
j=p[i].burst-p[i].comp;
p[i].comp=p[i].burst;
p[i].f=0;
}
printf("\nprocess # %-3d %-10d %-10d
%-10d",i+1,time,time+j,p[i].burstp[i].comp);
time+=j;
}
}
}
printf("\n\n------------------");
printf("\nProcess \t Waiting Time
TurnAround Time ");
for(i=0;i<n;i++)
{
printf("\nProcess # %-12d%-15d%-
15d",i+1,p[i].wait,p[i].wait+p[i].burst);
totalwait=totalwait+p[i].wait;
totalturn=totalturn+p[i].wait+p[i].burst;
}
printf("\n\nAverage\n------------------");
printf("\nWaiting Time : %f
ms",totalwait/(float)n);
printf("\nTurnAround Time : %f
ms\n\n",totalturn/(float)n);
return 0;
}

Output:

Enter The No Of Process :3
Enter The Quantum time (in ms) :5
Enter The Burst Time (in ms) For Process #
1 :25
Enter The Burst Time (in ms) For Process #
2 :30
Enter The Burst Time (in ms) For Process #
3 :54

Order Of Execution

Process Starting Ending Remaining

Time Time Time
process # 1 0 5 20
process # 2 5 10 25
process # 3 10 15 49
process # 1 15 20 15
process # 2 20 25 20
process # 3 25 30 44
process # 1 30 35 10
process # 2 35 40 15
process # 3 40 45 39
process # 1 45 50 5
process # 2 50 55 10
process # 3 55 60 34
process # 1 60 65 0
process # 2 65 70 5
process # 3 70 75 29
process # 2 75 80 0
process # 3 80 85 24
process # 3 85 90 19
process # 3 90 95 14
process # 3 95 100 9
process # 3 100 105 4
process # 3 105 109 0

Process Waiting Time Turn Around Time


Process # 1 40 65
Process # 2 50 80
Process # 3 55 109
Average
Waiting Time : 48.333333 ms
TurnAround Time : 84.666667 ms
zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: