-
Notifications
You must be signed in to change notification settings - Fork 0
/
4d.c
82 lines (76 loc) · 1.76 KB
/
4d.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
// Round Robin
#include <stdio.h>
#define MAX 20
int rq[20], front = 0, rear = -1;
struct process
{
int pid, bt, btcopy, wt, tat;
} p[20], s;
void enqueue(int pid)
{
if (rear > MAX - 1)
return;
else
{
rear = rear + 1; rq[rear] = pid;
}
}
int dequeue()
{
int ele, i;
if (rear == -1)
return -1;
else
{
ele = rq[0];
for (i = 0; i <= rear; i++)
rq[i] = rq[i + 1];
rear--; return ele;
}
}
int main()
{
int n, i, j, sum = 0, ts, id, ct = 0;
printf("\nEnter the no. of processes : ");
scanf("%d", &n);
printf("\nEnter the Time Slice : ");
scanf("%d", &ts);
for (i = 0; i < n; i++)
{
p[i].pid = i;
printf("\nEnter the burst time for Process %d : ", i);
scanf("%d", &p[i].bt);
p[i].btcopy = p[i].bt;
}
for (i = 0; i < n; i++)
{
enqueue(i);
}
id = dequeue();
printf("\nExecution order: ");
printf(" P%d->", id);
while (id != -1)
{
if (p[id].bt > ts)
{
ct = ct + ts; p[id].tat = ct;
p[id].bt = p[id].bt - ts;
if (p[id].bt != 0)
enqueue(p[id].pid);
}
else
{
ct = ct + p[id].bt; p[id].tat = ct;
p[id].bt = 0;
}
id = dequeue(); if (id != -1)
printf(" P%d->", id);
}
for (i = 0; i < n; i++)
p[i].wt = p[i].tat - p[i].btcopy;
printf("\n\n\n\tPID\t\tBurst Time\t\t Turnaround Time\t Waiting Time\n");
for (i = 0; i < n; i++)
printf("\n\t%d\t\t\t%d\t\t\t%d\t\t\t%d", p[i].pid, p[i].btcopy, p[i].tat, p[i].wt);
printf("\n");
return 0;
}