我是 and 的新手fork
,exec
我尝试了以下程序。
方案一:
int main(int argc, char *argv[]){
pid_t pid;
int status;
pid = fork();
if(pid == 0){
printf("new process");
execv("p1",argv);
}
else{
pid_t pr = wait(&status);// I am trying to get the exit value
// of the sub process.
printf("the child process exit with %d",status);
printf("father still running\n");
}
}
方案二:
int main(){
std::cout<<"I am the new thread"<<std::endl;
sleep(1);
std::cout<<"after 1 second"<<std::endl;
exit(1);
}
我运行第一个程序,输出是“子进程以 256 退出”。为什么结果256
不是1
? 如果我更改exit(1)
为exit(2)
,结果变为512
,这是为什么呢?只有当我返回 0 时它才有效。