【发布时间】:2015-10-14 04:06:35
【问题描述】:
我正在尝试检查父进程和子进程的 pid,但是每当我运行程序时,我得到的是它们两个相同的 pid 值。
据我所知,getpid 获取当前进程的 pid。我错了吗? same valur of pid for both processes
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main (void){
pid_t pid= fork();
switch (pid){
case -1:
perror("fork");
exit(1);
break;
case 0 :
printf("Child Process - my pid: %d, my parent's pid: %d\n", (int)getpid, (int)getppid);
break;
default :
wait();
printf("parent process - my pid: %d, my parent's pid: %d, my child's pid: %d\n", (int)getpid, (int)getppid, (int)pid);
break;}
printf("End of fork\n");
return 0;
}
【问题讨论】:
标签: process fork parent-child pid