-
Notifications
You must be signed in to change notification settings - Fork 0
/
os7.1.c
141 lines (120 loc) · 4.78 KB
/
os7.1.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
130
131
132
133
134
135
136
137
138
139
140
141
#include<stdio.h>
/*
void fcfs(int processes[],int n,int burst_time[]){
int waiting_time[n],turnaround_time[n],total_waiting_time = 0,total_turnaround_time =0;
waiting_time[0]=0;
for(int i = 1; i < n;i++) {
waiting_time[i] = burst_time[i-1] + waiting_time[i-1];
total_waiting_time += waiting_time[i];
}
for(int i=0; i<n ;i++) {
turnaround_time[i]=burst_time[i] + waiting_time[i];
total_turnaround_time += turnaround_time[i];
}
printf("First come ,first served (FCFS) Scheduling Algorithm\n");
printf("-------------------------------------------------\n");
printf("Process\tBurst Time\t Waiting Time \tTurnarounf Time\n");
for(int i = 0;i < n;i++){
printf("%d \t%d\t\t%d\t\t%d\n",processes[i],burst_time[i],waiting_time[i],turnaround_time[i]);
}
printf("Average waiting time: %.2f\n",(float)total_waiting_time/n);
printf("Average Turnaround Time: %.2f \n",(float)total_turnaround_time /n);
printf("\n");
}
void sjf(int processes[],int n,int burst_time[]){
int waiting_time[n],turnaround_time[n],completion_time[n],total_waiting_time =0,total_turnaround_time = 0;
for(int i=0;i<n;i++){
int shortest_job_index = i;
for(int j= i+1; j<n ;j++){
if(burst_time[j] < burst_time[shortest_job_index])
shortest_job_index = j;
}
int temp = burst_time[i];
burst_time[i] = burst_time[shortest_job_index];
burst_time[shortest_job_index] = temp;
temp = processes[i];
processes[i] = processes[shortest_job_index];
processes[shortest_job_index] =temp;
}
waiting_time[0] = 0;
for(int i = 1;i< n;i++){
waiting_time[i]=burst_time[i-1] + waiting_time[i-1];
total_waiting_time += waiting_time[i];
}
for(int i=0; i<n;i++){
turnaround_time[i] = burst_time[i] + waiting_time[i];
total_turnaround_time += turnaround_time[i];
}
printf("Shortest Job first(SJF) scheduling Algorithm\n");
printf("--------------------------------------------\n");
printf("Process\tBurst Time\tWaitng Time\tTurnaround Time\n");
for(int i=0;i<n;i++){
printf("%d\t%d\t\t%d\t\t%d\n",processes[i],burst_time[i],waiting_time[i],turnaround_time[i]);
}
printf("Average Waiting Time : %.2f\n",(float)total_waiting_time/n);
printf("Average Turnaround Time: %.2f\n",(float)total_turnaround_time/n);
printf("\n");
}
*/
void roundRobin(int processes[],int n,int burst_time[],int quantum){
int remaining_time[n],waiting_time[n],turnaround_time[n],total_waiting_time = 0, total_turnaround_time=0;
for(int i=0;i<n;i++){
remaining_time[i] = burst_time[i];
int time= 0;
while(1){
int all_processes_completed =2;
for(int i = 0;i<n;i++){
if(remaining_time[i]> 0){
all_processes_completed =0;
if(remaining_time[i]>quantum){
time += quantum;
remaining_time[i] =quantum;
}else{
time += remaining_time[i];
waiting_time[i] = time - burst_time[i];
remaining_time[i]=0;
}
}
}
if(all_processes_completed){
break;
}
}
for(int i=0;i<n;i++){
turnaround_time[i]=burst_time[i] + waiting_time[i];
total_turnaround_time += turnaround_time[i];
}
printf("Round robin scheduling algorithm\n");
printf("--------------------------------------\n");
printf("Process\tburst time\twaiting time\t turnaround time\n");
for(int i=0;i<n;i++){
printf("%d\t%d\t\t%d\t\t%d\n",processes[i],burst_time[i],waiting_time[i],turnaround_time[i]);
}
printf("Average waitng time : %.2f\n",(float)total_waiting_time /n);
printf("Average turnaround time: %.2f\n",(float)total_turnaround_time/n);
printf("\n");
}
}
int main(){
int n;
printf("Enter the number of processes:");
scanf("%d",&n);
int processes[n],burst_time[n];
// printf("Enter the burst time for each process: \n");
for(int i=0;i<n;i++){
printf("Enter the burst time for each process:\n");
for(int i=0;i<n;i++){
printf("Process %d:",i+1);
scanf("%d",&burst_time[i]);
processes[i] = i +1;
}
int quantum;
printf("Enter the time quantum fo round robin: ");
scanf("%d",&quantum);
printf("\n");
// fcfs(processes,n,burst_time);
//sjf(processes,n,burst_time);
roundRobin(processes,n,burst_time,quantum);
return 0;
}
}