先预览一下工程的目录树:
实现的功能: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
完。