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最困难之处在于调用它一次,它却返回两次.它在调 ...
随机推荐
- navicat 链接不上mysql
1 查看 my.cnf 的配置bindhost 127.0.0.1 注释掉 2 grant all privileges on *.* to root@'%' identified by 'passw ...
- Access 2003版数据库在Win7 64位系统下的不适应
使用ODBC连接不适应 使用microsoft.jet.4.0链接会出现“未在本地计算机上注册microsoft.jet.4.0” 应使用 Provider=Microsoft.ACE.OLEDB.1 ...
- 字符串拷贝函数strcpy写法_转
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...
- python判断key是否在字典用in不用has_key
小测试 in del.py import datetime cur = datetime.datetime.now() num = 1 a_list = {"a":1, " ...
- Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- EBS R12.2快速安装前没有配置Global Inventory报错
EBS R12.2快速安装前没有配置Global Inventory,导致验证时"file systems"这一项没有通过,被标记了"X": (本图其它两个验证 ...
- 12C对ASM rebalance操作的优化
如果在执行"alter diskgroup"操作.或在添加.删除磁盘而引发的隐式rebalance的时,没有指定power选项,rebalance操作会使用初始化参数asm_pow ...
- Custom IFormatProvider
The following example shows how to write a custom IFormatProvider which you can use in methodString. ...
- G面经prepare: Friends Recommendation
想想如果你用linkedin或者facebook, 给你一个人和他的朋友关系网,你会怎么给一个人推荐朋友 一个例子就是A-B, A-C, B - D, B - E, C - D,这个时候问我应该推荐谁 ...
- 算法训练 区间k大数查询
http://lx.lanqiao.org/problem.page?gpid=T11 算法训练 区间k大数查询 时间限制:1.0s 内存限制:256.0MB 问题描述 给定一个 ...