为什么父 pid return 的值与 getpid() 不同?
why does the parent pid return a different value than getpid()?
根据手册页
getpid() returns the process ID (PID) of the calling process.
- 在下面的代码中,为什么
parent pid
return 与 getpid()
的值不同?
- 主进程和父进程不一样吗?
- 为什么我在不同的系统上 运行 时会得到不同的输出?
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char const *argv[])
{
printf("getpid = %d \n", (int)getpid());
pid_t pid = fork();
printf("fork returned %d \n", (int)pid);
switch (pid)
{
case -1:
perror("fork failed");
break;
case 0:
printf("I am a child with pid = %d\n", (int)pid);
break;
default:
printf("I am a parent with pid = %d\n", (int)pid);
break;
}
return 0;
}
当我 运行 时输出:
getpid = 8208
fork returned 8209
I am a parent with pid = 8209
fork returned 0
I am a child with pid = 0
在另一台电脑上 运行 时的输出:
getpid = 2522
fork returned 2523
I am a parent with pid = 2522
fork returned 0
I am a child with pid = 2523
fork
复制一个进程,在分叉之后,它们都 运行 并行。为了告诉程序它是 child 还是 parent,它 returns 0 对于 child 和 child 进程的 PID 在parent.
所以在 fork 之后,你必须为 parent 和 child 调用 getpid()
,在你的代码中是:
pid_t thispid= getpid();
switch (pid)
{
case -1:
perror("fork failed");
break;
case 0:
printf("I am a child with pid = %d\n", (int)thispid);
break;
default:
printf("I am a parent with pid = %d\n", (int)thispid);
break;
}
是的,parent进程和主进程是一回事。
您的这段代码应该可以为您提供有关解决方案的线索:
switch (pid) {
/* ... */
case 0:
printf("I am a child with pid = %d\n", (int)pid);
break;
这实际上是说 "if pid
is zero then the child pid is always zero"。这显然不是真的,所以您对 fork()
的 return 值的解释不正确。
手册页指出:
Upon successful completion, fork() returns a value of 0 to the child process and returns the process ID of the child process to the parent process.
所以parent进程中的变量pid
就是child的pid ], 不是 parent 自己的 pid。
在child进程中,你需要在fork()
之后调用getpid()
来获取child的自己的 pid.
根据手册页
getpid() returns the process ID (PID) of the calling process.
- 在下面的代码中,为什么
parent pid
return 与getpid()
的值不同? - 主进程和父进程不一样吗?
- 为什么我在不同的系统上 运行 时会得到不同的输出?
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char const *argv[])
{
printf("getpid = %d \n", (int)getpid());
pid_t pid = fork();
printf("fork returned %d \n", (int)pid);
switch (pid)
{
case -1:
perror("fork failed");
break;
case 0:
printf("I am a child with pid = %d\n", (int)pid);
break;
default:
printf("I am a parent with pid = %d\n", (int)pid);
break;
}
return 0;
}
当我 运行 时输出:
getpid = 8208
fork returned 8209
I am a parent with pid = 8209
fork returned 0
I am a child with pid = 0
在另一台电脑上 运行 时的输出:
getpid = 2522
fork returned 2523
I am a parent with pid = 2522
fork returned 0
I am a child with pid = 2523
fork
复制一个进程,在分叉之后,它们都 运行 并行。为了告诉程序它是 child 还是 parent,它 returns 0 对于 child 和 child 进程的 PID 在parent.
所以在 fork 之后,你必须为 parent 和 child 调用 getpid()
,在你的代码中是:
pid_t thispid= getpid();
switch (pid)
{
case -1:
perror("fork failed");
break;
case 0:
printf("I am a child with pid = %d\n", (int)thispid);
break;
default:
printf("I am a parent with pid = %d\n", (int)thispid);
break;
}
是的,parent进程和主进程是一回事。
您的这段代码应该可以为您提供有关解决方案的线索:
switch (pid) {
/* ... */
case 0:
printf("I am a child with pid = %d\n", (int)pid);
break;
这实际上是说 "if pid
is zero then the child pid is always zero"。这显然不是真的,所以您对 fork()
的 return 值的解释不正确。
手册页指出:
Upon successful completion, fork() returns a value of 0 to the child process and returns the process ID of the child process to the parent process.
所以parent进程中的变量pid
就是child的pid ], 不是 parent 自己的 pid。
在child进程中,你需要在fork()
之后调用getpid()
来获取child的自己的 pid.