getpid(), getppid()); _exit(0); //子进程使用_exit()退出
时间: 2024-05-23 07:14:20 浏览: 12
这段代码是一个子进程的退出代码。getpid()函数返回当前进程的进程ID,getppid()函数返回当前进程的父进程的进程ID。这段代码的作用是打印出当前子进程的进程ID和父进程的进程ID,并使用_exit()函数退出子进程。_exit()函数是一个系统调用函数,它可以使进程立即结束,而不会像exit()函数那样执行一些清理工作。因此,子进程使用_exit()函数来快速结束自己。
相关问题
linux环境 子进程使用prctl(PR_SET_PDEATHSIG, SIGTERM);检测父进程是否退出
在 Linux 环境下,子进程可以使用 `prctl` 函数来检测父进程是否退出。具体实现步骤如下:
1. 父进程在启动子进程之前,调用 `prctl` 函数设置 `PR_SET_PDEATHSIG` 属性,并指定一个信号(如 `SIGTERM`)。
2. 子进程在启动后,调用 `getppid` 函数获取自己的父进程 ID。
3. 子进程通过 `kill` 函数向父进程发送一个 0 信号,检测父进程是否存在。如果父进程已经退出,子进程会收到一个 `SIGTERM` 信号。
下面是一个简单的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/prctl.h>
int main() {
pid_t parent_pid = getpid();
// 设置 PR_SET_PDEATHSIG 属性
if (prctl(PR_SET_PDEATHSIG, SIGTERM) == -1) {
perror("prctl");
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
} else if (pid == 0) {
// 子进程
while (1) {
// 检测父进程是否存在
if (kill(parent_pid, 0) == -1) {
if (errno == ESRCH) {
// 父进程已经退出
printf("Parent process has exited.\n");
exit(EXIT_SUCCESS);
} else {
perror("kill");
exit(EXIT_FAILURE);
}
}
sleep(1);
}
} else {
// 父进程
atexit(parent_exit_handler);
sleep(2);
printf("Parent process is exiting.\n");
exit(EXIT_SUCCESS);
}
}
void parent_exit_handler() {
// 父进程退出时不需要做任何处理
}
```
在上面的代码中,父进程在启动子进程之前,调用了 `prctl` 函数设置 `PR_SET_PDEATHSIG` 属性,并指定一个信号(如 `SIGTERM`)。子进程在启动后,通过 `getppid` 函数获取自己的父进程 ID,并通过 `kill` 函数向父进程发送一个 0 信号,检测父进程是否存在。如果父进程已经退出,子进程会收到一个 `SIGTERM` 信号。
修改这段代码,使其能够顺利运行:#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> int main(){ pid_t pc1,pc2,pw1,pw2; pc1=fork(); pc2=fork(); if (pc1>0&&pc2>0) { //*父进程 pw1=wait(NULL); pw2=wait(NULL); printf("***Catch a dead child process with pid: %d\n",pw1); printf("***Catch a dead child process with pid: %d\n",pw2); printf("***I'M %d,THE MAIN PROCESS LEAVE!\n",getpid()); }//if if (pc1==0&&pc2>0) { //*子进程1 printf("===I'M the first child PID:%d,my father is:%d\n",getpid(),getppid()); sleep(10); }//if if (pc1>0&&pc2==0) //*子进程2 printf("===I'M the 2nd child PID:%d,my father is:%d,i don't sleep.\n",getpid(),getppid()); if (pc1==0&&pc2==0) //*孙进程 printf("I'M grandson PID:%d,my father is:%d,no one is waiting for – me.\n",getpid(),getppid()); exit(0); } //main
这段代码存在一些问题,需要进行修改:
1. 在第一个子进程执行pc2=fork()之前需要加上exit(0),否则会产生多余的孙进程。
2. 在if (pc1>0&&pc2>0)的代码块中需要加上else,否则会执行多次输出的操作。
修改后的代码如下:
```
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(){
pid_t pc1,pc2,pw1,pw2;
pc1=fork();
if (pc1 == 0) {
//*子进程1
printf("===I'M the first child PID:%d,my father is:%d\n",getpid(),getppid());
exit(0);
} else if (pc1 > 0) {
pc2=fork();
if (pc2 == 0) {
//*子进程2
printf("===I'M the 2nd child PID:%d,my father is:%d,i don't sleep.\n",getpid(),getppid());
exit(0);
} else if (pc2 > 0) {
//*父进程
pw1=wait(NULL);
pw2=wait(NULL);
printf("***Catch a dead child process with pid: %d\n",pw1);
printf("***Catch a dead child process with pid: %d\n",pw2);
printf("***I'M %d,THE MAIN PROCESS LEAVE!\n",getpid());
}
}
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)