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最困难之处在于调用它一次,它却返回两次.它在调 ...
随机推荐
- python_GUI
1. 需要安装wxPython软件 2. GUI(图形用户界面)代码的编写顺序 备注: 1. 加入面板和布局管理器,可以使得组件的位置和大小更加灵活 3. 示例: #encoding=utf-8 i ...
- 最流行的PHP 代码规范
“PHP是最好的编程语言” ;-) 那么PHPer习惯使用什么样的代码规范呢?sideeffect.kr通过分析GitHub上托管的开源代码,得出了一些有趣的结果,让我们一起来看看吧. 缩进 空格(7 ...
- jQuery.proxy()函数
jQuery.proxy(),接受一个函数,然后返回一个新函数,并且这个新函数始终保持了特定的上下文(context)语境. context 代表上下文 name是上下文的某个属性 jQuery. ...
- 安装Cocoapods(Pods 管理iOS 第三方库)
安装 可先检测Mac电脑是否安装Pods.打开控制台: $ which pod 如果安装,结果如下;如果没有安装,控制台无反应. /usr/bin/pod 如果没有安装, 以下命令安装之. $ sud ...
- nexenta systemcallerror
最近在试nexenta做iscsi,设置ip出现上面的错误 解决办法,先讲mtu设置为不周与原来的值,比如原来为1500,先设置成1501,就可以了,然后可以再改回来,也是没有问题的!
- [Linux Tips] 1. 查看端口
查看监听的端口 # netstat -lnp
- H2的MVStore
翻译自http://www.h2database.com/html/mvstore.html 转载请著名出处,及译者信息. 第一次翻译,诸多不妥请谅解,谢谢. 概述 MVStore是一个持久化的.日志 ...
- 【转】Tomcat组件生命周期管理
Tomcat组件生命周期管理 Tomcat中Server,Service,Connector,Engine,Host,Context,它们都实现了org.apache.catalina.Lifecyc ...
- PAT 解题报告 1047. Student List for Course (25)
1047. Student List for Course (25) Zhejiang University has 40000 students and provides 2500 courses. ...
- PostgreSQL Replication之第十二章 与Postgres-XC一起工作(7)
12.7 处理故障转移和删除节点 在本节中,我们将看看故障切换如何处理.我们还将看看如何使用安全可靠的方法添加节点到Postgres-XC设置以及如何从Postgres-XC设置删除节点. 12.7. ...