-
Notifications
You must be signed in to change notification settings - Fork 0
/
7.c
129 lines (124 loc) · 2.69 KB
/
7.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// CPU Scheduling -Round Robin
#include<stdio.h>
struct process
{
char name;
int at,bt,wt,tt,rt;
int completed;
}p[10];
int n;
int q[10]; //queue
int front=-1,rear=-1;
void enqueue(int i)
{
if(rear==10)
printf("overflow");
rear++;
q[rear]=i;
if(front==-1)
front=0;
}
int dequeue()
{
if(front==-1)
printf("underflow");
int temp=q[front];
if(front==rear)
front=rear=-1;
else
front++;
return temp;
}
int isInQueue(int i)
{
int k;
for(k=front;k<=rear;k++)
{
if(q[k]==i)
return 1;
}
return 0;
}
void sortByArrival()
{
struct process temp;
int i,j;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(p[i].at>p[j].at)
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
}
int main()
{
int i,j,time=0,sum_bt=0,tq;
char c;
float avgwt=0;
printf("Enter Number of Processes:\n");
scanf("%d",&n);
for(i=0,c='A';i<n;i++,c++)
{
p[i].name=c;
printf("\nProcess %c\n",c);
printf("\tArrival Time :");
scanf("%d",&p[i].at);
printf("\tBurst Time :");
scanf("%d",&p[i].bt);
p[i].rt=p[i].bt;
p[i].completed=0;
sum_bt+=p[i].bt;
}
printf("\nEnter the time quantum:");
scanf("%d",&tq);
sortByArrival();
enqueue(0);
printf("\nProcess execution order: ");
for(time=p[0].at;time<sum_bt;)
{
i=dequeue();
if(p[i].rt<=tq)
{
time+=p[i].rt;
p[i].rt=0;
p[i].completed=1;
printf(" %c ",p[i].name);
p[i].wt=time-p[i].at-p[i].bt;
p[i].tt=time-p[i].at;
for(j=0;j<n;j++)
{
if(p[j].at<=time && p[j].completed!=1&& isInQueue(j)!=1)
{
enqueue(j);
}
}
}
else
{
time+=tq;
p[i].rt-=tq;
printf(" %c ",p[i].name);
for(j=0;j<n;j++)
{
if(p[j].at<=time && p[j].completed!=1&&i!=j&& isInQueue(j)!=1)
{
enqueue(j);
}
}
enqueue(i);
}
}
printf("\n\nName\tArrival Time\tBurst Time\tResponse Time\tTurnAround Time");
for(i=0;i<n;i++)
{
avgwt+=p[i].wt;
printf("\n%c\t\t%d\t\t%d\t\t%d\t\t%d",p[i].name,p[i].at,p[i].bt,p[i].wt,p[i].tt);
}
printf("\n\nAverage waiting time:%f",avgwt/n);
}