++ 举例:
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
pid_t pid = fork();
if (pid == 0) { // 子进程
exit(42); // 假设我们用一个特定的非零值来表示某种状态
} else if (pid > 0) { // 父进程
int status;
waitpid(pid, &status, 0); // 等待子进程结束
if (WIFEXITED(status))
printf("Child exited with status %d\n", WEXITSTATUS(status));
else
printf("Child did not exit normally\n");
} else { // fork失败
perror("fork");
exit(EXIT_FAILURE);
}
return 0;
}
谢谢