34 lines
770 B
C
34 lines
770 B
C
#include <stdio.h>
|
||
#include <unistd.h>
|
||
|
||
int main(int arg,char* argv[]){
|
||
|
||
// int 被 typedef 为 pid_t
|
||
pid_t pid=fork();
|
||
|
||
// 当pid==0时,是子进程代码运行区域。其他则是父进程运行区域。
|
||
if(pid<0){
|
||
printf("Create child process failed ...\n");
|
||
}else if(pid==0){
|
||
//子进程执行体
|
||
printf("Create child process successfully %i \n",getpid());
|
||
}
|
||
else{
|
||
//父进程执行体
|
||
printf("This is parent process %i \n",getpid());
|
||
}
|
||
|
||
// 执行体结束标志
|
||
if(pid==0)
|
||
{
|
||
sleep(3);
|
||
printf("pid=%i child process end ... \n",getpid());
|
||
}
|
||
else{
|
||
// 睡眠5s,等待子进程结束
|
||
sleep(5);
|
||
printf("pid=%i Parent process end ... \n",getpid());
|
||
}
|
||
return 0;
|
||
}
|