测试代码:
#include<iostream>
#include<unistd.h>
using namespace std;
int main()
{
pid_t id = fork();
if(id == 0)
{
//child
while(true)
{
cout<<"i am child, running!"<<endl;
sleep(2);
}
}
else{
//parent
cout << "father do nothing!" <<endl;
sleep(10);
exit(1);//终止进程
}
return 0;
}
我们让父进程睡眠10秒,然后直接退出
此时我们的子进程还在运行
查看状态我们就能发现,子进程进入了孤儿状态
状态检测代码:
开始实验:
延伸关闭进程
kill -9 进程的pid