所以我正在用 C 设计一个基本的 UNIX shell。
signal(SIGCHLD, handler);
pid = fork();
switch (pid) {
case -1: printf("Fork failed; cpid == -1\n");
break;
case 0: child_pid = getpid();
argv[0] = prog;
argv[1] =NULL;
//exit(0);
sid = setsid();
execv(absPath,argv);
//printf("%d: this is the child, pid = %d\n", i, child_pid);
//sleep(1);
//exit(0);
break;
default: printf("This is the parent: waiting for %d to finish\n", pid);
waitpid(pid, NULL, WNOHANG);
printf("Ttttthat's all, folks\n");
//break;
}
//execv(absPath,argv);
//printf("CHILD PROCESS");
}
}
void handler(int sig)
{
pid_t pid;
pid = wait(NULL);
printf("Pid %d exit.\n", pid);
exit(0);
}
但它仍然在同一个 shell 中执行分叉的进程,尽管在“默认”子句之后。
你能帮我让它作为后台进程运行吗?谢谢!
PS:这只是一个片段。