【发布时间】:2016-10-29 03:39:04
【问题描述】:
我写了一个简单的代码,用fork创建新进程,然后我想看看谁是子进程,谁是父进程。 据我所知,fork 对子 pc 的返回值为 0,而对父进程的返回值为 pid 号或子进程。 不知何故,在孩子和父亲身上我都得到了 0。 有人可以向我解释这段代码有什么问题吗? 谢谢。
代码是:
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
void main()
{
pid_t childpid;
int status,i;
if(childpid = fork() == -1){
perror("fork err");
exit(1);
}
if(childpid == 0){
printf("child process, pid number is %d and returned value from fork is %d\n",getpid(),childpid);
}
else{
printf("father process, pid number is %d and returned value from fork is %d\n",getpid(),childpid);
}
}
【问题讨论】:
-
==的优先级高于=。