- 论坛徽章:
- 0
|
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- int main (int argc, char *argv[]) {
- pid_t childpid = 0;
- int i, n;
-
- if (argc != 2){ /* check for valid number of command-line arguments */
- fprintf(stderr, "Usage: %s processes\n", argv[0]);
- return 1;
- }
- n = atoi(argv[1]);
- for (i = 1; i < n; i++)
- if (childpid = fork())
- break;
-
- printf("i:%d process ID:%ld parent ID:%ld child ID:%ld\n",
- i, (long)getpid(), (long)getppid(), (long)childpid);
- return 0;
- }
复制代码
编译运行后的结果为:
- /usr]simple 5
- i:5 process ID:4484 parent ID:4483 child ID:0
- i:1 process ID:4480 parent ID:4436 child ID:4481
- i:2 process ID:4481 parent ID:1 child ID:4482
- i:3 process ID:4482 parent ID:1 child ID:4483
- i:4 process ID:4483 parent ID:1 child ID:4484
复制代码
i---1后产生的子进程为4481,i---2中显示的 此进程为4481,但父进程却是1,i--3..也是同样的现象,请问 此时的子进程的父进程为什么不是 上个进程的id号,却是 操作系统的 进程?! |
|