#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++进行编译,运行结果为: