前面讲述的fork函数的基本用法,下面通过两个程序来说明fork函数同步与异步之间的关系:

<1>通过fork函数实现在父进程下的四个兄弟子进程(即异步) :

函数实现代码:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/wait.h> int main (void)
{
pid_t pid;
char *message;
int i; for(i=0;i<3;i++){
pid=fork();
if(pid==0)break;
}
if(pid<0){
perror("fork failed");
exit(1);
}
if(pid==0){
int i;
for(i=5;i>0;i--){
printf("pid=%d\tpid=%d\n",getpid(),getppid());
printf("This is the child\n");
sleep(1);
while(1);
}
exit(3);
} else{
int stat_val;
waitpid(pid,&stat_val,0);
// wait(&stat_val);
if(WIFEXITED(stat_val))
printf("Child exited with code %d\n",WEXITSTATUS(stat_val));
else if(WIFSIGNALED(stat_val))
printf("Child terminated abnormally,signal %d\n",WTERMSIG(stat_val));
}
return 0;
}

用工具pstree观察如下图:

可以看到有三个a.out并列,即他们直接属于兄弟进程。

<2>通过fork函数实现在父进程下的父子孙进程(即同步):

函数代码如下:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/wait.h> int main (void)
{
pid_t pid;
char *message;
int i; for(i=0;i<3;i++){
pid=fork();
if(pid>0)break;
}
if(pid<0){
perror("fork failed");
exit(1);
}
if(pid==0){
int i;
for(i=5;i>0;i--){
printf("pid=%d\tpid=%d\n",getpid(),getppid());
printf("This is the child\n");
sleep(1);
//while(1);
}
exit(3);
} else{
int stat_val;
waitpid(pid,&stat_val,0);
// wait(&stat_val);
if(WIFEXITED(stat_val))
printf("Child exited with code %d\n",WEXITSTATUS(stat_val));
else if(WIFSIGNALED(stat_val))
printf("Child terminated abnormally,signal %d\n",WTERMSIG(stat_val));
}
return 0;
}

用pstree观看如下:

可知他们之间是父子孙进程,是同步关系。

通过以上我们了解了fork函数同步与异步之间大的差异,又不正确的地方希望指正。。。。

[置顶] Linux高编之进程--------fork函数的同步与异步(兄弟子进程和父子孙进程示列)的更多相关文章

  1. [置顶] linux中fork()函数详解(原创!!实例讲解)

    分类: 计算机系统 linux2010-06-01 23:35 60721人阅读 评论(105) 收藏 举报 linux2010存储  一.fork入门知识 一个进程,包括代码.数据和分配给进程的资源 ...

  2. [置顶] Linux信号相关笔记

    最近又温习了一遍Linux中的信号知识,发现有很多东西以前没有注意到,就通过这篇博客记录一下,巩固一下知识点. 一,信号基础: 信号是什么?为了回答这个问题,首先要从异常说起,这里的异常不是指c++/ ...

  3. [置顶] Linux学习总结(20)——Linux 文件夹结构和作用

     /bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的基点,比如用户user的主目录就是/home/us ...

  4. [置顶] linux getline()函数

    getline()函数是什么?百度百科这样解释:      getline不是C库函数,而是C++库函数.它会生成一个包含一串从输入流读入的字符的字符串,直到以下情况发生会导致生成的此字符串结束.1) ...

  5. linux源码阅读笔记 fork函数

    在阅读源码的过程中,发现找不到fork函数的定义.后来在linux/init/main.c中找到了这样一条语句 static inline _syscall0(int,fork) 原来这里就是fork ...

  6. C语言 进程控制---创建进程fork()函数

    #include "sys/types.h" #include "stdio.h" #include "stdlib.h" #include ...

  7. [置顶] linux内核启动2-setup_arch中的内存初始化(目前分析高端内存)

    上一篇微博留下了这几个函数,现在我们来分析它们         sanity_check_meminfo();         arm_memblock_init(&meminfo, mdes ...

  8. [置顶] linux常用命令大全

    SSH 密令控制台 user/pwd 一:停止tomcat 1,cd .. 进入根目录 2,cd home/ 3,ll 4,cd bin/ 进入tomcat bin目录 5,ll 6,ps -ef | ...

  9. [置顶] Linux 常用命令集锦

    出处:http://www.vaikan.com/what-are-the-most-useful-swiss-army-knife-one-liners-on-unix/ Linux命令行里的&qu ...

随机推荐

  1. Lost connection to MySQL server at ‘reading initial communication packet', system error: 0 mysql远程连接问题

    在用Navicat for MySQL远程连接mysql的时候,出现了 Lost connection to MySQL server at ‘reading initial communicatio ...

  2. 安装sql server 2008,提示要删除SQL Server 2005 Express 工具 怎么解决?

    x86 修改注册表:HKLM\Software\Microsoft\Microsoft SQL Server\90\Tools\ShellSEM,把 ShellSEM重命名即可. x64       ...

  3. PHP IP互换数字[转]

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 请大神帮忙解决 jquery 控制 li 标签问题

    <li class="active"><a href="#1" data-toggle="tab">日志详细情况&l ...

  5. 一种轻量的openresty路由设计

    在使用openresty开发接口的过程会发现一个问题,那就是接口的地址问题怎么解决,最好一个接口地址对应一个lua文件,也可以在nginx.conf 配置中使用content_by_lua 来编写接口 ...

  6. IntraWeb.v14.0.32安装及破解指南

    一.下载 首先从这里下载14.0.32版本的IntraWeb: 链接:http://pan.baidu.com/s/1c0rjnKO 密码:8kv2 二.卸载旧版 1. 我的Delphi版本是XE6, ...

  7. matlab操作之--读取指定文件夹下的“指定格式”文件

    %% 正负样本所在folder fext='*.png';%要读取的文件格式 positiveFolder='F:\课题\Crater detection\machingLearning\Positi ...

  8. OGNL学习-静态方法调用

    <constant name="struts.ognl.allowStaticMethodAccess" value="true"/> 1.小写 & ...

  9. BZOJ 4009 接水果

    Description 风见幽香非常喜欢玩一个叫做osu!的游戏,其中她最喜欢玩的模式就是接水果. 由于她已经DT FC了The big black, 她觉得这个游戏太简单了,于是发明了一个更加难的版 ...

  10. The Model Complexity Myth

    The Model Complexity Myth (or, Yes You Can Fit Models With More Parameters Than Data Points) An oft- ...