forked from huannguyen2008/os2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04.practical.work.fork.exec.c
34 lines (33 loc) · 1.21 KB
/
04.practical.work.fork.exec.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
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main(){
int pid = fork();
if (pid) {
// you are parent
printf("Main process, first child process has id: %d\n",pid);
wait(NULL);
printf("Finishing executed first child process!\n");
pid = fork();
if(pid) {
printf("Main process, second child process id: %d\n",pid);
wait(NULL);
printf("Finishing executed second child process!\n");
}
else if (pid == 0) {
// you are child 2
printf("child process 2 begin run:\n");
char *arg[]={"/bin/free","-h",NULL};
execvp(arg[0],arg);
}
else printf("error can not fork second child\n");
}
else if (pid == 0) {
// you are child 1
printf("child process 1 begin run:\n");
char *arg[]={"/bin/ps","-ef",NULL};
execvp(arg[0],arg);
}
else printf("error can not fork anything\n");
return 0;
}