fork和exec一起使用
先预览一下工程的目录树:

实现的功能:master进程启动slave进程。
看看Makefile内容:
all: master.out slave.out master.out: master.cpp
g++ master.cpp -o master.out slave.out: slave.cpp
g++ slave.cpp -o slave.out clean:
rm -rf *.out
master.cpp内容:
/*************************************************************************
* File: a.cpp
* Brief:
* Created Time: Wed 23 Dec 2015 08:50:13 AM CST
************************************************************************/ #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<signal.h>
using namespace std; #define Sleep(ms) usleep(ms * 1000) int main(int argc,char* argv[])
{
printf("pid=%d, ",getpid());
for(int i=; i<argc;i++)
printf("[%d]=%s, ",i,argv[i]);
printf("\n"); struct sigaction sigchld_action;
sigchld_action.sa_handler = SIG_DFL;
sigchld_action.sa_flags = SA_NOCLDWAIT;
sigaction(SIGCHLD, &sigchld_action, NULL); //new process
int m_subProcessID=fork();
if (m_subProcessID==)
{
int ret=execl("./slave.out","a=abc","b=def","c=12345",NULL);
if(!=ret)
{
printf("execl fails.\n");
return -;
}
printf("execl done wit ok\n");
return ;
}
for(int i=;i<;i++)
{
Sleep();
printf("master say: i=%d\n",i);
}
printf("master say: i am done\n");
return ;
}
slave.cpp内容:
/*************************************************************************
* File: a.cpp
* Brief:
* Created Time: Wed 23 Dec 2015 08:50:13 AM CST
************************************************************************/ #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<signal.h>
using namespace std; #define Sleep(ms) usleep(ms * 1000) int main(int argc,char* argv[])
{
printf("pid=%d, ",getpid());
for(int i=; i<argc;i++)
printf("[%d]=%s, ",i,argv[i]);
printf("\n"); for(int i=;i<;i++)
{
Sleep();
printf("slave say: -----------------------------------------------------ndex=%d\n",i);
}
printf("slave say: i am done\n");
return ;
}
make编译,看看运行效果:
[root@localhost testfork]# ./master.out
pid=, []=./master.out,
pid=, []=a=abc, []=b=def, []=c=,
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
master say: i=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
master say: i=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
master say: i=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
master say: i=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
master say: i=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
master say: i=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
master say: i=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
master say: i=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
master say: i=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
master say: i=
master say: i am done
[root@localhost testfork]# slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
slave say: -----------------------------------------------------ndex=
slave say: i am done [root@localhost testfork]#
可见,在第38行,master正常退出。在第55行,slave也正常退出。
注意到,我们常规启动进程的时候,进程打印的启动参数里面第一个是程序文件名称(也可能包含了路径)。而在这里,我们看到slave.out通过exec函数启动的时候,启动参数里的第一个参数变成了我们在exec函数里设置的参数"a=abc"。
在两个进程都运行的时候,我们查看一下进程信息:
ps -A |grep -sn --color=auto "master\.out\|slave\.out"

master的进程ID为7749,slave的进程ID为7750,两个进程为父子进程。
这里说说fork,在父进程运行到fork时,会分离出一个子进程。两个进程的内存完全一样,子进程会共享父进程当前持有的内核对象,包括互斥量这些同步变量。
例如,父进程打开了一个文件FILE* fp,子进程fclose后,父进程调用fwrite不会报错,但实际上数据并没有写入文件。
资源:
百度网盘\软件源码\testfork.zip
完。
fork和exec一起使用的更多相关文章
- 【转】Linux下Fork与Exec使用
Linux下Fork与Exec使用 转自 Linux下Fork与Exec使用 一.引言 对于没有接触过Unix/Linux操作系统的人来说,fork是最难理解的概念之一:它执行一次却返回两个值.for ...
- Linux下Fork与Exec使用
Linux下Fork与Exec使用 一.引言 对于没有接触过Unix/Linux操作系统的人来说,fork是最难理解的概念之一:它执行一次却返回两个值.fork函数是Unix系统最杰出的成就之一, ...
- linux进程之fork 和 exec函数
---恢复内容开始--- fork函数 该函数是unix中派生新进程的唯一方法. #include <unistd.h> pid_t fork(void); 返回: (调用它一次, 它 ...
- linux c语言 fork() 和 exec 函数的简介和用法
linux c语言 fork() 和 exec 函数的简介和用法 假如我们在编写1个c程序时想调用1个shell脚本或者执行1段 bash shell命令, 应该如何实现呢? 其实在<std ...
- Linux下Fork与Exec
一.引言 对于没有接触过Unix/Linux操作系统的人来说,fork是最难理解的概念之一:它执行一次却返回两个值.fork函数是Unix系统最杰出的成就之一,它是七十年代UNIX早期的开发者经过长期 ...
- fork 和 exec
https://blog.csdn.net/disadministrator/article/details/39347333 进程创建方法:fork.exec.clone,父进程等待子进程结束是用w ...
- fork、exec 和 exit 对 IPC 对象的影响
GitHub: https://github.com/storagezhang Emai: debugzhang@163.com 华为云社区: https://bbs.huaweicloud.com/ ...
- Linux fork()、exec() Hook Risk、Design-Principle In Multi-Threadeed Program
目录 . Linux exec指令执行监控Hook方案 . 在"Multi-Threadeed Program"环境中调用fork存在的风险 . Fork When Multi-T ...
- fork和exec函数
#include<unistd.h> pid_t fork(void); 返回:在子进程中为0,在父进程中为子进程IO,若出错则为- fork最困难之处在于调用它一次,它却返回两次.它在调 ...
随机推荐
- [BS-14] 打印NSArray和NSDictionary的3种方法
打印NSArray和NSDictionary的3种方法 NSArray *arr = @[@"奔驰",@"宝马",@"路虎",@" ...
- Power-BI 仪表盘实现动态预警
BI系统中仪表盘“序列”中的“预警”属性可以手工填入固定的值,也可从数据集里取值设定预警范围 ,以实现动态预警.其中的“范围”属性手工输入固定值应该为百分比(0-1),而在下拉选择绑定列为值是为数值. ...
- [转载]WGS84坐标与Web墨卡托坐标互转
//经纬度转Wev墨卡托 dvec3 CMathEngine::lonLat2WebMercator(dvec3 lonLat) { dvec3 mercator; ; +lonLat.y)*PI/) ...
- linux系统服务名称
服务列表(按字母顺序排列) 服务名 必需(是/否) 用途描述 acon 否 语言支持 acpi 否 电源管理 acpid 否 监听精灵进程 adsl 否 内部ADSL开关控制 alsa 否 高级Lin ...
- javaEE开发案例——购物车
一.页面 流程:登录页面(login.jsp)——>购物大厅页面(hall.jsp)——>购物车页面(showMyCart.jsp)——>订单页面(myorder.jsp)——> ...
- 前端实战——照片墙gallery的实现
对应的html代码 <!doctype html> <html lang="zh-hans"> <head> <meta charset= ...
- canvas 基础知识整理(一)
canvas这个 HTML 元素是为了客户端矢量图形而设计的.它自己没有行为,但却把一个绘图 API 展现给客户端 JavaScript 以使脚本能够把想绘制的东西都绘制到一块画布上. html的基本 ...
- redhat linux 安装mysql5.6.27
1.yum安装mysql(root身份) yum install mysql-server mysql-devel mysql -y 如没有配置yum,请参见博客:http://www.cnblogs ...
- Notice: Undefined offset 的解决方法
Notice: Undefined offset: 1 in D:\wwwroot\wr\askseo\404.php on line 5 Notice: Undefined offset: 2 in ...
- PHP——生成随机数和日期时间
在PHP里面两个常用的函数 rand(); 生成随机数 当括号内无参数时 系统会以当前时间为种子进行随机数的生成 rand(1,10); 括号里面是生成随机数的范围,在形成的随机数在1~1 ...