先预览一下工程的目录树:
实现的功能: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=0; i<argc;i++) printf("[%d]=%s, ",i,argv[i]); printf(" "); 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==0) { int ret=execl("./slave.out","a=abc","b=def","c=12345",NULL); if(0!=ret) { printf("execl fails. "); return -1; } printf("execl done wit ok "); return 0; } for(int i=0;i<10;i++) { Sleep(2000); printf("master say: i=%d ",i); } printf("master say: i am done "); return 0; }
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=0; i<argc;i++) printf("[%d]=%s, ",i,argv[i]); printf(" "); for(int i=0;i<40;i++) { Sleep(800); printf("slave say: -----------------------------------------------------ndex=%d ",i); } printf("slave say: i am done "); return 0; }
make编译,看看运行效果:
1 [root@localhost testfork]# ./master.out 2 pid=7749, [0]=./master.out, 3 pid=7750, [0]=a=abc, [1]=b=def, [2]=c=12345, 4 slave say: -----------------------------------------------------ndex=0 5 slave say: -----------------------------------------------------ndex=1 6 master say: i=0 7 slave say: -----------------------------------------------------ndex=2 8 slave say: -----------------------------------------------------ndex=3 9 master say: i=1 10 slave say: -----------------------------------------------------ndex=4 11 slave say: -----------------------------------------------------ndex=5 12 slave say: -----------------------------------------------------ndex=6 13 master say: i=2 14 slave say: -----------------------------------------------------ndex=7 15 slave say: -----------------------------------------------------ndex=8 16 master say: i=3 17 slave say: -----------------------------------------------------ndex=9 18 slave say: -----------------------------------------------------ndex=10 19 slave say: -----------------------------------------------------ndex=11 20 master say: i=4 21 slave say: -----------------------------------------------------ndex=12 22 slave say: -----------------------------------------------------ndex=13 23 master say: i=5 24 slave say: -----------------------------------------------------ndex=14 25 slave say: -----------------------------------------------------ndex=15 26 slave say: -----------------------------------------------------ndex=16 27 master say: i=6 28 slave say: -----------------------------------------------------ndex=17 29 slave say: -----------------------------------------------------ndex=18 30 master say: i=7 31 slave say: -----------------------------------------------------ndex=19 32 slave say: -----------------------------------------------------ndex=20 33 slave say: -----------------------------------------------------ndex=21 34 master say: i=8 35 slave say: -----------------------------------------------------ndex=22 36 slave say: -----------------------------------------------------ndex=23 37 master say: i=9 38 master say: i am done 39 [root@localhost testfork]# slave say: -----------------------------------------------------ndex=24 40 slave say: -----------------------------------------------------ndex=25 41 slave say: -----------------------------------------------------ndex=26 42 slave say: -----------------------------------------------------ndex=27 43 slave say: -----------------------------------------------------ndex=28 44 slave say: -----------------------------------------------------ndex=29 45 slave say: -----------------------------------------------------ndex=30 46 slave say: -----------------------------------------------------ndex=31 47 slave say: -----------------------------------------------------ndex=32 48 slave say: -----------------------------------------------------ndex=33 49 slave say: -----------------------------------------------------ndex=34 50 slave say: -----------------------------------------------------ndex=35 51 slave say: -----------------------------------------------------ndex=36 52 slave say: -----------------------------------------------------ndex=37 53 slave say: -----------------------------------------------------ndex=38 54 slave say: -----------------------------------------------------ndex=39 55 slave say: i am done 56 57 [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不会报错,但实际上数据并没有写入文件。
资源:
百度网盘软件源码 estfork.zip
完。