#include <stdio.h>
2 #include <unistd.h>
3 #include <sys/types.h>
4 int main(void)
5 {
6 pid_t pid;
7 //创建子进程
8 pid = fork();
9 if(pid == -1){
10 perror(“fork”);
11 return -1;
12 }
13 if(pid == 0){
14 //子
15 //sleep(1);
16 printf(“子pid %d\n”,getpid());
17 printf(“子父pid %d\n”,getppid());
18
19
20 }else{
21 //父
22 getchar();
23 printf(“父pid %d\n”,getpid());
24 printf(“父父pid %d\n”,getppid());
25
26 }
27 printf(“heheha…\n”);
28 return 0;
29 }
~
fork()函数
最新推荐文章于 2024-08-05 19:27:31 发布