|
- #include <stdlib.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <time.h>
- int main()
- {
- pid_t pid_1;
- pid_t pid_2;
- pid_1 = fork();
- pid_2 = fork();
- if (pid_1 < 0 || pid_2 < 0)
- {
- printf("erro_fork\n");
- exit(1);
- }
- else if (pid_1 == 0)
- {
- sleep(5);
- printf("pid_1 = %d\n", getpid());
- _exit;
- }
- else if (pid_2 == 0)
- {
- sleep(3);
- printf("pid_2 = %d\n", getpid());
- _exit;
- }
- else
- {
- sleep(6);
- printf("pid_parrent = %d\n", getpid());
- }
- return 0;
- }
复制代码
运行结果:
- pid_2 = 8162
- pid_1 = 8161
- pid_1 = 8160
- pid_parrent = 8159
复制代码
为什么会有两个pid_1呢?
fork()函数可以这样用吗?……看了APUE2还是不太理解这个函数的调用…… |
|