-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab2.c
54 lines (45 loc) · 1.65 KB
/
lab2.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
#include "types.h"
#include "user.h"
int main(int argc, char *argv[])
{
int PScheduler(void);
PScheduler();
// End of test
exit(0);
}
int PScheduler(void){
// use this part to test the priority scheduler. Assuming that the priorities range between range between 0 to 63
// 0 is the highest priority. All processes have a default priority of 20
int pid, ret_pid, exit_status;
int i,j,k;
printf(1, "\n Step 2: testing the priority scheduler and setpriority(int priority)) systema call:\n");
printf(1, "\n Step 2: Assuming that the priorities range between range between 0 to 31\n");
printf(1, "\n Step 2: 0 is the highest priority. All processes have a default priority of 10\n");
printf(1, "\n Step 2: The parent processes will switch to priority 0\n");
setpriority(0);
for (i = 0; i < 3; i++) {
pid = fork();
if (pid > 0 ) {
continue;}
else if ( pid == 0) {
// printf(1, "\n Hello! this is child# %d and I will change my priority to %d \n",getpid(),60-20*i);
setpriority(30-10*i);
for (j=0;j<50000;j++) {
for(k=0;k<10000;k++) {
asm("nop"); }}
printf(1, "\n child# %d with priority %d has finished! \n",getpid(),getpriority());
exit(0);
}
else {
printf(2," \n Error \n");
exit(-1);
}
}
if(pid > 0) {
for (i = 0; i < 3; i++) {
ret_pid = wait(&exit_status);
printf(1,"\n This is the parent: child with PID# %d has finished with status %d \n",ret_pid,exit_status);
}
printf(1,"\n if processes with highest priority finished first then its correct \n");
}
return 0;}