您的当前位置:首页正文

linux下C++根据pid关闭程序进程

2024-11-09 来源:个人技术集锦
#include<unistd.h>
#include<signal.h>
#include<sys/types.h>
#include<sys/wait.h>
main()
{
pid_t pid;
int status;
if(!(pid= fork())){
printf(“Hi I am child process!\n”);
sleep(10);
return;
}
else{
printf(“send signal to child process (%d) \n”,pid);
sleep(1);
kill(pid ,SIGABRT);    //给pid所指的进程发送SIGABRT(中止一个程序)信号
wait(&status);
if(WIFSIGNALED(status))
printf(“chile process receive signal %d\n”,WTERMSIG(status));
}
}


执行 send signal to child process(3170)
Hi I am child process!
child process receive signal 6
Top