What exactly does fork return?

2019-01-17 01:32发布

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?

标签: c linux fork
8条回答
时光不老,我们不散
2楼-- · 2019-01-17 01:59

Fork creates a duplicate process and a new process context. When it returns a 0 value it means that a child process is running, but when it returns another value that means a parent process is running. We usually use wait statement so that a child process completes and parent process starts executing.

查看更多
Ridiculous、
3楼-- · 2019-01-17 02:00
                             p = fork();
                        /* assume no errors */
                        /* you now have two */
                        /* programs running */
                         --------------------
      if (p > 0) {                |            if (p == 0) {
        printf("parent\n");       |              printf("child\n");
        ...                       |              ...
查看更多
登录 后发表回答