究竟是什么叉子回报? [英] What exactly does fork return?

查看:94
本文介绍了究竟是什么叉子回报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

在成功,孩子的PID
  过程是在父母的返回
  执行的线程,和一个0是
         在执行中的孩子的线程返回。


  P = fork()的;

我在它的手册页困惑,是 P 等于 0 PID


解决方案

我不知道手册如何能更清楚! 叉()创建一个新的进程,所以你现在有两个相同的进程。为了区分它们,叉的返回值()不同。在最初的过程中,你得到的子进程的PID。在子过程中,你会得到0。

因此​​,一个规范的使用方法如下:

  P = fork()的;
如果(0 == P)
{
    //我们的子进程
}
否则如果(P&0)
{
    //我们的父进程
}
其他
{
    //我们的父进程,但孩子不能创建
}

On success, the PID of the child process is returned in the parent’s thread of execution, and a 0 is returned in the child’s thread of execution.

p = fork();

I'm confused at its manual page,is p equal to 0 or PID?

解决方案

I'm not sure how the manual can be any clearer! fork() creates a new process, so you now have two identical processes. To distinguish between them, the return value of fork() differs. In the original process, you get the PID of the child process. In the child process, you get 0.

So a canonical use is as follows:

p = fork();
if (0 == p)
{
    // We're the child process
}
else if (p > 0)
{
    // We're the parent process
}
else
{
    // We're the parent process, but child couldn't be created
}

这篇关于究竟是什么叉子回报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆