#include<iostream>
#include<sys/types.h>
#include<signal.h>
#include<unistd.h>
#include<stdlib.h>
using namespace std;
int main()
{
cout<<"Father process is running!"<<endl;
pid_t pid;
pid=fork();
if(-1==pid)
{
cout<<"Fork failed!"<<endl;
exit(1);
}
if(0==pid) //Child process
{
cout<<"This is child process!"<<endl;
kill(getppid(),SIGKILL);
sleep(5);
exit(0);
}
sleep(10);
cout<<"This is father process!"<<endl;
sleep(5);
exit(0);
}
1.g++进行编译,运行结果为:
本文探讨了在C++中使用fork()函数创建子进程,并通过kill()函数终止父进程的过程,展示了进程间的通信和控制。
6815

被折叠的 条评论
为什么被折叠?



