#include <stdio.h>
#include <unistd.h>
using namespace std;
int main(int argc ,char *argv[])
{
pid_t pid;
pid_t pid_num;
pid_t farther_pid_num;
farther_pid_num=getpid();
printf("farther_pid_num = %d \n",farther_pid_num);
static int static_num=10;
pid=fork();
if(pid<0)
{
printf("fork error\n");
}else if(pid==0)
{
printf("this is son thread\n");
pid_num=getpid();
printf("pid_num = %d \n",pid_num);
}
else
{
printf("this is farther thread\n");
pid_num=getpid();
printf("pid_num = %d \n",pid_num);
}
for (int i=0;i<10;++i)
{
static_num++;
printf("i = %d ,static_num=%d\n",i,static_num);
}
}
执行结果如下:
/*
* fork_test.c
* version 2
* Created on: 2010-5-29
* Author: wangth
*/
#include <unistd.h>
#include <stdio.h>
int main(void)
{
int i=0;
printf("i son/pa ppid pid fpid\n");
//ppid指当前进程的父进程pid
//pid指当前进程的pid,
//fpid指fork返回给当前进程的值
for(i=0;i<2;i++){
pid_t fpid=fork();
if(fpid==0)
printf("%d child %4d %4d %4d\n",i,getppid(),getpid(),fpid);
else
printf("%d parent %4d %4d %4d\n",i,getppid(),getpid(),fpid);
}
return 0;
}
2.僵尸进程和wait/waitpid
孤儿进程:父进程退出,子进程还在运行;
僵尸进程:子进程运行结束,但是父进程未调用wait/waitpid函数获取状态信息;
避免僵尸进程的方法:(1)使用wait/waitpid;(2)使用两次fork函数。
//wait/taipid
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>
#include <stdlib.h>
using namespace std;
int main(int argc ,char *argv[])
{
pid_t pid;
pid_t pid_num;
pid_t farther_pid_num;
farther_pid_num=getpid();
printf("farther_pid_num = %d \n",farther_pid_num);
pid=fork();
if(pid<0)
{
printf("fork error\n");
}else if(pid==0)
{
printf("============================\n");
printf("this is son thread\n");
pid_num=getpid();
printf("son pid_num = %d \n",pid_num);
}else{
printf("++++++++++++++++++++++++++++++++\n");
printf("this is farther thread\n");
printf("son pid = %d\n",pid);//父进程返回的是子进程的ID
pid_num=getpid();
printf("farther pid_num = %d \n",pid_num);
system("ps -ef|grep fork");
sleep(10);
int ret = waitpid(-1,NULL,WNOHANG);
if(ret < 0)
{
printf("ret = %d\n",ret);
}
system("ps -ef|grep fork");
}
}
运行结果 :
//两次fork
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>
#include <stdlib.h>
using namespace std;
int main(int argc ,char *argv[])
{
pid_t pid;
pid_t pid_num;
pid_t farther_pid_num;
farther_pid_num=getpid();
pid_t second_pid_num;
printf("farther_pid_num = %d \n",farther_pid_num);
pid=fork();
if(pid<0)
{
printf("fork error\n");
}else if(pid==0)
{
printf("============================\n");
printf("this is son thread\n");
pid_num=getpid();
printf("son pid_num = %d \n",pid_num);
second_pid_num =fork();
if(second_pid_num<0)
{
printf("second fork error\n");
}else if(second_pid_num==0){
sleep(30);
printf("second fork son pid_num = %d\n",getpid());
system("ps -ef|grep fork");
}else{
printf("second fork father pid_num = %d\n",getpid());
system("ps -ef|grep fork");
}
}else{
printf("++++++++++++++++++++++++++++++++\n");
printf("this is farther thread\n");
printf("son pid = %d\n",pid);//父进程返回的是子进程的ID
pid_num=getpid();
printf("farther pid_num = %d \n",pid_num);
sleep(10);
int ret = waitpid(-1,NULL,WNOHANG);
if(ret < 0)
{
printf("ret = %d\n",ret);
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/5a5b164cda94890eccc27f87d187db1e.png)