我的BBB上的系统为Angstrom Linux,我在上面编写了一个程序,如下:
- #include <stdio.h>
- /* finding pids within a process using system calls */
- int main(void)
- {
- int pid, ppid;
- pid = fork();
- printf("%d first output from both processes
", pid);
- sleep(20);
- if( pid>0 )
- {
- printf("%d %s", pid, "This is the child's pid, output by the parent process.
");
- ppid = getppid();
- printf("%s %d
", "The pid of the parent of parent process is", ppid);
- }
- else if( pid==0 )
- {
- printf("%d%s", pid, "is printed inside the child process if the fork succeeds.
");
- ppid = getppid();
- printf("%s %d
", "The pid of the parent of child process is", ppid);
- pid = getpid();
- printf("%d %s", pid, "is the child pid printed by the child, obtained by getpid().
");
- }
- else
- {
- printf("fork failed.
");
- }
- return 0;
- }
复制代码父进程和子进程都需要打印出自己的父进程的pid,下面是运行结果截图:
子进程打印出的自己的父进程的pid竟然是1。我查看了一下系统的所有进程:
这。怎么可能!!!
同样的程序, 我又在Ubuntu上编译运行,就可以得到正确的结果:
为什么呀?
此帖出自
小平头技术问答
一周热门 更多>