-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab1_test.c
92 lines (79 loc) · 3.41 KB
/
lab1_test.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
#include "types.h"
#include "stat.h"
#include "user.h"
int exitWait(void);
int waitPid(void);
int main(int argc, char *argv[]) {
printf(1, "####################################################\n");
printf(1, "# This program tests the correctness of your lab#1\n");
printf(1, "####################################################\n");
if (atoi(argv[1]) == 1)
exitWait(); // Test exit and wait
else if (atoi(argv[1]) == 2)
waitPid(); // Test waitpid
// For corner cases
else
printf(1, "\ntype \"lab1 1\" to test exit and wait, \"lab1 2\" to test waitpid \n");
// End of test
exit(0);
}
int exitWait(void) {
int pid, ret_pid, exit_status=0;
int i;
// use this part to test exit(int status) and wait(int* status)
printf(1, "Step 1: testing exit(int status) and wait(int* status):\n");
for (i = 0; i < 2; i++) {
pid = fork();
if (pid == 0) { // only the child executed this code
if (i == 0) {
printf(1, " - This is child with PID# %d and I will exit with status %d\n", getpid(), 0);
exit(0);
} else {
printf(1, " - This is child with PID# %d and I will exit with status %d\n" ,getpid(), -1);
exit(-1);
}
} else if (pid > 0) { // only the parent exeecutes this code
ret_pid = wait(0);
printf(1, " - This is the parent: child with PID# %d has exited with status %d\n", ret_pid, exit_status);
} else { // something went wrong with fork system call
printf(2, " - Error using fork\n");
exit(-1);
}
}
return 0;
}
int waitPid(void){
int ret_pid, exit_status;
int i;
int pid_a[5]={0, 0, 0, 0, 0};
// use this part to test wait(int pid, int* status, int options)
printf(1, "Step 2: testing waitpid(int pid, int* status, int options):\n");
for (i = 0; i <5; i++) {
pid_a[i] = fork();
if (pid_a[i] == 0) { // only the child executed this code
printf(1, " - The is child with PID# %d and I will exit with status %d\n", getpid(), 0);
exit(2);
}
}
sleep(5);
printf(1, " - This is the parent: Now waiting for child with PID# %d\n",pid_a[3]);
ret_pid = waitpid(pid_a[3], &exit_status, 0);
printf(1, " - This is the parent: Child# %d has exited with status %d\n",ret_pid, exit_status);
sleep(5);
printf(1, " - This is the parent: Now waiting for child with PID# %d\n",pid_a[1]);
ret_pid = waitpid(pid_a[1], &exit_status, 0);
printf(1, " - This is the parent: Child# %d has exited with status %d\n",ret_pid, exit_status);
sleep(5);
printf(1, " - This is the parent: Now waiting for child with PID# %d\n",pid_a[2]);
ret_pid = waitpid(pid_a[2], &exit_status, 0);
printf(1, " - This is the parent: Child# %d has exited with status %d\n",ret_pid, exit_status);
sleep(5);
printf(1, " - This is the parent: Now waiting for child with PID# %d\n",pid_a[0]);
ret_pid = waitpid(pid_a[0], &exit_status, 0);
printf(1, " - This is the parent: Child# %d has exited with status %d\n",ret_pid, exit_status);
sleep(5);
printf(1, " - This is the parent: Now waiting for child with PID# %d\n",pid_a[4]);
ret_pid = waitpid(getpid(), &exit_status, 0);
printf(1, " - This is the parent: Child# %d has exited with status %d\n",ret_pid, exit_status);
return 0;
}