[置顶] Linux高编之进程--------fork函数的同步与异步(兄弟子进程和父子孙进程示列)
前面讲述的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函数的同步与异步(兄弟子进程和父子孙进程示列)的更多相关文章
- [置顶] linux中fork()函数详解(原创!!实例讲解)
分类: 计算机系统 linux2010-06-01 23:35 60721人阅读 评论(105) 收藏 举报 linux2010存储 一.fork入门知识 一个进程,包括代码.数据和分配给进程的资源 ...
- [置顶] Linux信号相关笔记
最近又温习了一遍Linux中的信号知识,发现有很多东西以前没有注意到,就通过这篇博客记录一下,巩固一下知识点. 一,信号基础: 信号是什么?为了回答这个问题,首先要从异常说起,这里的异常不是指c++/ ...
- [置顶]
Linux学习总结(20)——Linux 文件夹结构和作用
/bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的基点,比如用户user的主目录就是/home/us ...
- [置顶]
linux getline()函数
getline()函数是什么?百度百科这样解释: getline不是C库函数,而是C++库函数.它会生成一个包含一串从输入流读入的字符的字符串,直到以下情况发生会导致生成的此字符串结束.1) ...
- linux源码阅读笔记 fork函数
在阅读源码的过程中,发现找不到fork函数的定义.后来在linux/init/main.c中找到了这样一条语句 static inline _syscall0(int,fork) 原来这里就是fork ...
- C语言 进程控制---创建进程fork()函数
#include "sys/types.h" #include "stdio.h" #include "stdlib.h" #include ...
- [置顶] linux内核启动2-setup_arch中的内存初始化(目前分析高端内存)
上一篇微博留下了这几个函数,现在我们来分析它们 sanity_check_meminfo(); arm_memblock_init(&meminfo, mdes ...
- [置顶] linux常用命令大全
SSH 密令控制台 user/pwd 一:停止tomcat 1,cd .. 进入根目录 2,cd home/ 3,ll 4,cd bin/ 进入tomcat bin目录 5,ll 6,ps -ef | ...
- [置顶]
Linux 常用命令集锦
出处:http://www.vaikan.com/what-are-the-most-useful-swiss-army-knife-one-liners-on-unix/ Linux命令行里的&qu ...
随机推荐
- Ubuntu系列Crontab日记记录
修改rsyslog文件,将/etc/rsyslog.d/50-default.conf 文件中的#cron.*前的#删掉 重启rsyslog服务service rsyslog restart 重启cr ...
- mysql 链接数据库
一.MySQL 连接本地数据库,用户名为“root”,密码“root”(注意:“-p”和“root” 之间不能有空格) C:\>mysql -h localhost -u root -proot ...
- 按钮效果 css
<!doctype html><html lang="en"><head> <meta charset="UTF-8" ...
- PHP 常用命令
php常用命令: #输出语句 $ php -r "echo '123' . PHP_EOL;" #执行php脚本文件 $ php -f file.php #查看版本号 $ ph ...
- 【Python开发实战】Windows7+VirtualBox+Ubuntu环境配置
1. VirtualBox的安装 参考常规安装方式即可. VirtualBox 4.3.14 for Windows hosts:http://download.virtualbox.org/virt ...
- Linux内存映射(mmap)系列(1)
看到同事的代码中出现了mmap.所以自己私下学习学习,研究研究..... http://www.cnblogs.com/lknlfy/archive/2012/04/27/2473804.html ( ...
- 学习Swift -- 可选链
可空链式调用 可空链式调用是一种可以请求和调用属性.方法及下标的过程,它的可空性体现于请求或调用的目标当前可能为空(nil).如果可空的目标有值,那么调用就会成功:如果选择的目标为空(nil),那么这 ...
- GetSystemMetrics() 函数的用法
可以用GetSystemMetrics函数可以获取系统分辨率,但这只是其功能之一,GetSystemMetrics函数只有一个参数,称之为「索引」,这个索引有75个标识符,通过设置不同的标识符就可以获 ...
- C#使用字符串分割字符串
我们都会用字符分割字符串: string[] recvArr = recv.Split(';'); 如果用字符串分割呢?下面: string[] sArray = Regex.Split(recv, ...
- 小游戏 Lights Out (关灯) 的求解 —— 异或方程组
Author : Evensgn Blog Link : http://www.cnblogs.com/JoeFan/ Article Link : http://www.cnblogs.com/J ...