-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexperiment2.c
More file actions
75 lines (57 loc) · 2.4 KB
/
experiment2.c
File metadata and controls
75 lines (57 loc) · 2.4 KB
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
//Write a C program to simulate multi-level queue scheduling algorithm considering the following scenario. All the processes in the system are divided into two categories - system processes and user processes. System processes are to be given higher priority than user processes. Use FCFS scheduling for the processes in each queue.
#include <stdio.h>
struct Process {
int pid;
int arrivalTime;
int burstTime;
int waitingTime;
int turnaroundTime;
};
void calculateFCFS(struct Process p[], int n, int startTime) {
int totalWaiting = 0, totalTurnaround = 0;
int currentTime = startTime;
for (int i = 0; i < n; i++) {
if (currentTime < p[i].arrivalTime)
currentTime = p[i].arrivalTime;
p[i].waitingTime = currentTime - p[i].arrivalTime;
currentTime += p[i].burstTime;
p[i].turnaroundTime = p[i].waitingTime + p[i].burstTime;
totalWaiting += p[i].waitingTime;
totalTurnaround += p[i].turnaroundTime;
}
printf("\nPID\tArrival\tBurst\tWaiting\tTurnaround");
for (int i = 0; i < n; i++) {
printf("\nP%d\t%d\t%d\t%d\t%d",
p[i].pid, p[i].arrivalTime, p[i].burstTime,
p[i].waitingTime, p[i].turnaroundTime);
}
printf("\nAverage Waiting Time: %.2f", (float)totalWaiting / n);
printf("\nAverage Turnaround Time: %.2f\n", (float)totalTurnaround / n);
}
int main() {
int n1, n2;
printf("Enter number of System processes: ");
scanf("%d", &n1);
struct Process systemP[n1];
for (int i = 0; i < n1; i++) {
systemP[i].pid = i + 1;
printf("\nEnter Arrival Time and Burst Time for System Process P%d: ", i + 1);
scanf("%d %d", &systemP[i].arrivalTime, &systemP[i].burstTime);
}
printf("\nEnter number of User processes: ");
scanf("%d", &n2);
struct Process userP[n2];
for (int i = 0; i < n2; i++) {
userP[i].pid = i + 1;
printf("\nEnter Arrival Time and Burst Time for User Process P%d: ", i + 1);
scanf("%d %d", &userP[i].arrivalTime, &userP[i].burstTime);
}
printf("\n===== SYSTEM PROCESSES (HIGH PRIORITY) =====");
calculateFCFS(systemP, n1, 0);
int totalSystemTime = 0;
for (int i = 0; i < n1; i++)
totalSystemTime += systemP[i].burstTime;
printf("\n===== USER PROCESSES (LOW PRIORITY) =====");
calculateFCFS(userP, n2, totalSystemTime);
return 0;
}