54 lines
1.3 KiB
C
54 lines
1.3 KiB
C
#include <unistd.h>
|
||
#include <stdio.h>
|
||
#include <time.h>
|
||
#include <stdlib.h>
|
||
|
||
int main(){
|
||
__pid_t pid;
|
||
|
||
//父进程输出
|
||
|
||
printf("P1:I am father process,PID=%d,PPID=%d\n", getpid(), getppid());
|
||
|
||
//生成子进程
|
||
pid = fork();
|
||
|
||
//父进程
|
||
if(pid != 0){
|
||
//pid判断归零
|
||
pid = 0;
|
||
pid = fork();
|
||
|
||
//子进程
|
||
if (pid == 0){
|
||
printf("P12:I am young brother process,PID=%d,PPID=%d\n", getpid(), getppid());
|
||
|
||
//此时pid为0,继续生成子进程
|
||
pid = fork();
|
||
if (pid == 0){
|
||
printf("P121:我的学号是2024414290124,PID=%d,PPID=%d\n", getpid(), getppid());
|
||
}
|
||
//父进程
|
||
else{
|
||
//pid判断归零
|
||
pid = 0;
|
||
pid = fork();
|
||
//子进程
|
||
if (pid == 0){
|
||
printf("P122:我的姓名是吕锦中,PID=%d,PPID=%d\n", getpid(), getppid());
|
||
}
|
||
//父进程
|
||
else{
|
||
}
|
||
}
|
||
}
|
||
//父进程
|
||
else{
|
||
}
|
||
}
|
||
//子进程
|
||
else{
|
||
time_t t = time(NULL);
|
||
printf("P11:当前时间是:%s,PID=%d,PPID=%d\n", ctime(&t), getpid(), getppid());
|
||
}
|
||
} |