【发布时间】:2014-08-31 07:27:14
【问题描述】:
我是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 时它才有效。
【问题讨论】:
标签: c++ exec fork wait waitpid