-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram12.c
119 lines (105 loc) · 2.36 KB
/
Program12.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 100
struct process
{
int process_id;
int arrival_time;
int burst_time;
int waiting_time;
int turn_around_time;
int remaining_time;
};
int queue[N];
int front = 0, rear = 0;
struct process proc[N];
void push(int process_id)
{
queue[rear] = process_id;
rear = (rear+1)%N;
}
int pop()
{
if(front == rear)
return -1;
int return_position = queue[front];
front = (front +1)%N;
return return_position;
}
int main()
{
float wait_time_total = 0.0, tat = 0.0;
int n,time_quantum;
printf("Enter the number of processes: ");
scanf("%d", &n);
for(int i=0; i<n; i++)
{
printf("Enter the Arrival Time for the Process%d : ",i+1);
scanf("%d", &proc[i].arrival_time);
printf("Enter the Burst Time for the Process%d : ",i+1);
scanf("%d", &proc[i].burst_time);
proc[i].process_id = i+1;
proc[i].remaining_time = proc[i].burst_time;
}
printf("Enter time quantum: ");
scanf("%d",&time_quantum);
int time=0;
int processes_left=n;
int position=-1;
int local_time=0;
for(int j=0; j<n; j++)
if(proc[j].arrival_time == time)
push(j);
while(processes_left)
{
if(local_time == 0)
{
if(position != -1)
push(position);
position = pop();
}
for(int i=0; i<n; i++)
{
if(proc[i].arrival_time > time)
continue;
if(i==position)
continue;
if(proc[i].remaining_time == 0)
continue;
proc[i].waiting_time++;
proc[i].turn_around_time++;
}
if(position != -1)
{
proc[position].remaining_time--;
proc[position].turn_around_time++;
if(proc[position].remaining_time == 0)
{
processes_left--;
local_time = -1;
position = -1;
}
}
else
local_time = -1;
time++;
local_time = (local_time +1)%time_quantum;
for(int j=0; j<n; j++)
if(proc[j].arrival_time == time)
push(j);
}
printf("\n");
printf("Process\t\tArrival Time\tBurst Time\tWaiting time\tTurn around time\n");
for(int i=0; i<n; i++)
{
printf("%d\t\t%d\t\t", proc[i].process_id, proc[i].arrival_time);
printf("%d\t\t%d\t\t%d\n", proc[i].burst_time, proc[i].waiting_time, proc[i].turn_around_time);
tat += proc[i].turn_around_time;
wait_time_total += proc[i].waiting_time;
}
tat = tat/(1.0*n);
wait_time_total = wait_time_total/(1.0*n);
printf("\nAverage Waiting Time : %f",wait_time_total);
printf("\nAverage Turn Around Time : %f\n", tat);
}