You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
781 B
32 lines
781 B
/*
|
|
* This app fork a child process, and the child process fork a grandchild process.
|
|
* every process waits for its own child exit then prints.
|
|
* Three processes also write their own global variables "flag"
|
|
* to different values.
|
|
*/
|
|
|
|
#include "user/user_lib.h"
|
|
#include "util/types.h"
|
|
|
|
int flag;
|
|
int main(void) {
|
|
flag = 0;
|
|
int pid = fork();
|
|
if (pid == 0) {
|
|
flag = 1;
|
|
pid = fork();
|
|
if (pid == 0) {
|
|
flag = 2;
|
|
printu("Grandchild process end, flag = %d.\n", flag);
|
|
} else {
|
|
wait(pid);
|
|
printu("Child process end, flag = %d.\n", flag);
|
|
}
|
|
} else {
|
|
wait(-1);
|
|
printu("Parent process end, flag = %d.\n", flag);
|
|
}
|
|
exit(0);
|
|
return 0;
|
|
}
|