-
Notifications
You must be signed in to change notification settings - Fork 0
/
test2.c
145 lines (125 loc) · 2.78 KB
/
test2.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
142
143
144
145
/* Test 2 -- test queue1, the priority queue
In this test, we set waiting ticks to be a large number
--- process in the priority queue cannot move back to the round robin queue
In the beginning, C is forced to stay in the round robin queue (using setpriority)
After a while, C is set to change its priority, and will move to priority queue.
During this time, process A and B should accumulate some waiting ticks.
So after C changes its priority, we except that process A and B should execute for while
before process C starts executing (as C has the lowest priority).
The results after C changes its priority should be:
......ABABABABABAB.....ABCABCABC....
or something like
......AABBAABBAABB.....ABCCBAABCCBA......
(depends on how you deal with two processes with the same priority value).
*/
#include "types.h"
#include "stat.h"
#include "user.h"
#include "fcntl.h"
void loop()
{
int i;
int j=0;
for(i=0;i<1000000;i++)
j=j+1;
}
void
forktest(void)
{
int pid1;
int ret;
int fds1[2];
ret = setrunningticks(2);
if (ret < 0)
{
printf(1, "cannot set running ticks\n");
exit();
}
ret = setwaitingticks(1000000);
if (ret < 0)
{
printf(1, "cannot set waiting ticks\n");
exit();
}
ret = pipe(fds1);
if ( ret < 0)
{
printf(1, "cannot create a pipe\n");
exit();
}
pid1 = fork();
if(pid1 < 0)
return;
if(pid1 == 0){
int i;
char buf[256];
// block here
close(fds1[1]);
read(fds1[0], buf, 1);
printf(1, "\n start process A [%d]\n", getpid());
for (i=0;i<100;i++)
{
//printf(1, "Program C[%d] %d\n", getpid(), i);
loop();
}
}
else
{
int pid2;
int fds2[2];
ret = pipe(fds2);
ret = pipe(fds2);
if (ret < 0)
{
printf(1, "cannot create the second pipe\n");
}
pid2 = fork();
if(pid2 < 0)
return;
if(pid2 == 0){
int i;
char buf[256];
close(fds1[0]);
close(fds2[1]);
//block here
read(fds2[0], buf, 1);
write(fds1[1], "Done", 5);
printf(1, "\n start process B [%d]\n", getpid());
for (i=0;i<100;i++)
{
//printf(1, "Program B[%d] %d\n", getpid(), i);
loop();
}
}
else
{
int i;
close(fds1[0]);
close(fds1[1]);
close(fds2[0]);
write(fds2[1],"Done", 5);
printf(1, "\n start process C [%d]\n", getpid());
setpriority(getpid(), 0);
for (i=0;i<25;i++)
{
//printf(1, "Program A[%d] %d\n", getpid(), i);
loop();
}
setpriority(getpid(), 1);
printf(1, "\n C prority changes\n");
for (i=0;i<50;i++)
{
//printf(1, "Program A[%d] %d\n", getpid(), i);
loop();
}
wait();
wait();
}
}
}
int
main(void)
{
forktest();
exit();
}