Ç - fork()的code [英] c - fork() code

查看:149
本文介绍了Ç - fork()的code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void main ()
{
   if ( fork () )
   {
       printf ( "PID1 %d\n", getpid () );
   }
   else
   {
       printf ( "PID2 %d\n", getpid () );
   }
}

这是什么code呢?我知道它是与进程ID,但不应叉返回的东西进入状态,以确定它是否是一个子/父进程?

What does this code do? I know it has something to do with process IDs but shouldn't fork return something into the condition to determine whether it's a child/parent process?

推荐答案

通常是:

pid_t pid = fork();
if(pid == 0) {
  //child
} else if(pid > 0) {
  //parent
} else {
 //error
}

该名男子页说:

RETURN VALUE
   Upon successful completion, fork() shall return 0 to the child 
   process and shall return the process ID of the child process to the 
   parent process.  Both processes shall continue to execute from 
   the fork() function. 
   Otherwise, -1 shall be returned to the parent process, no child process 
   shall be created, and errno shall be set to indicate the error.

这篇关于Ç - fork()的code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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