-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfcfs_test.c
More file actions
76 lines (57 loc) · 1.25 KB
/
fcfs_test.c
File metadata and controls
76 lines (57 loc) · 1.25 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
76
#include "types.h"
#include "stat.h"
#include "user.h"
#define NUM_CHILD 5
#define NUM_LOOP 5
// ret: 0: child, 1: parent
int create_child(void) {
for (int i = 0; i < NUM_CHILD; i++) {
int pid = fork();
if (pid == 0) {
sleep(10);
return 0;
}
}
return 1;
}
void exit_child(int parent) {
if (parent)
while (wait() != -1); // wait for all child processes to finish
else
exit();
}
int main(int argc, char **argv) {
int p; // is this parent?
printf(1, "FCFS test start\n");
// Test 1
printf(1, "\n자식 프로세스를 5번 실행\n");
p = create_child();
for (int i = 0; i < NUM_LOOP; i++) {
if (p == 0)
printf(1, " process %d ", getpid());
}
printf(1, "\n");
exit_child(p);
// Test 2
printf(1, "\n루프돌며 자신 PID 출력하고 CPU 양보\n");
p = create_child();
for (int i = 0; i < NUM_LOOP; i++) {
if (p == 0) {
printf(1, " process %d ", getpid());
yield();
}
}
printf(1, "\n");
exit_child(p);
// Test 3
printf(1, "\n자신 PID 출력하고 1초 대기\n");
p = create_child();
for (int i = 0; i < NUM_LOOP; i++) {
if (p == 0) {
printf(1, " process %d \n", getpid());
sleep(1);
}
}
exit_child(p);
exit();
}