博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
针对fork()函数的深入理解!用事例family家谱来进行说明!
阅读量:2240 次
发布时间:2019-05-09

本文共 989 字,大约阅读时间需要 3 分钟。

通过这段时间的学习,对fork()这个创建进程的函数进行深入的理解,通过一个family一个进程家谱进行查看进程与进程直接的关系。

首先要创建一个进程作为  爷爷 -> 父亲 -> 儿子

                                                               -> 女儿                                               

                                                 ->叔叔

                                                                                 ->姑姑

通过一个三代的家谱来说明fork()函数在创建进程的时候是怎么确定进程关系的。

下面看代码部分:

#include<stdlib.h>

#include<stdio.h>

int main()

{
pid_t pid;
pid=fork();
if(pid==-1)
{
 printf("error!\n");
 exit(1);
}
else if(pid==0)
{
 pid_t pid_1;
 pid_1=fork();
 printf("this is father!\n");
 if(pid_1==-1)
 {
  printf("error!\n");
 }
 else if(pid_1==0)
 {
  printf("this is sunzi\n");
  while(1);
 }
 else
 {
  printf("this is secend grandfather!\n");
  pid_t pid4;
  pid4=fork();
  if(pid4==-1)
  {
   printf("error!\n");
  }
  else if(pid4==0)
  {

    printf("this is nuncle!\n");

   while(1);
  }
  else
  {
   while(1);
  }
  while(1);
 }
  
        while(1);
}
else
{
 printf("this is unt!\n");
 pid_t pid2,pid3;
 pid2=fork();
 if(pid2==-1)
 {
  printf("error!\n");
 }
 else if(pid2==0)
 {
  
  while(1);
 }
 else
 {
  pid3=fork();
                if(pid3==-1)
                {
                printf("error!\n");
                }
                 else if(pid3==0)
                {
                        while(1);
                }
                else
                {
                while(1);
                }
                while(1);
 
 while(1);
 }
 while(1);
}
return 0;
}
运行程序的时候,另开一个终端,使用pstree就可以看到bash下面会打印一个三代的家谱!

转载地址:http://aaqbb.baihongyu.com/

你可能感兴趣的文章
内存堆与栈
查看>>
Leetcode C++《每日一题》20200621 124.二叉树的最大路径和
查看>>
Leetcode C++《每日一题》20200622 面试题 16.18. 模式匹配
查看>>
Leetcode C++《每日一题》20200625 139. 单词拆分
查看>>
Leetcode C++《每日一题》20200626 338. 比特位计数
查看>>
Leetcode C++ 《拓扑排序-1》20200626 207.课程表
查看>>
Go语言学习Part1:包、变量和函数
查看>>
Go语言学习Part2:流程控制语句:for、if、else、switch 和 defer
查看>>
Go语言学习Part3:struct、slice和映射
查看>>
Go语言学习Part4-1:方法和接口
查看>>
Leetcode Go 《精选TOP面试题》20200628 69.x的平方根
查看>>
leetcode 130. Surrounded Regions
查看>>
【Python】详解Python多线程Selenium跨浏览器测试
查看>>
Jmeter之参数化
查看>>
Shell 和Python的区别。
查看>>
【JMeter】1.9上考试jmeter测试调试
查看>>
【虫师】【selenium】参数化
查看>>
【Python练习】文件引用用户名密码登录系统
查看>>
学习网站汇总
查看>>
【Loadrunner】性能测试报告实战
查看>>