進程控制開發(fā)之:實驗內(nèi)容
該實驗有3個進程,其中一個為父進程,其余兩個是該父進程創(chuàng)建的子進程,其中一個子進程運行“ls-l”指令,另一個子進程在暫停5s之后異常退出,父進程先用阻塞方式等待第一個子進程的結(jié)束,然后用非阻塞方式等待另一個子進程的退出,待收集到第二個子進程結(jié)束的信息,父進程就返回。
3.實驗步驟(1)畫出該實驗流程圖。
該實驗流程圖如圖7.8所示。
圖7.8實驗7.4.1流程圖
(2)實驗源代碼。
先看一下下面的代碼,這個程序能得到我們所希望的結(jié)果嗎,它的運行會產(chǎn)生幾個進程?請讀者回憶一下fork()調(diào)用的具體過程。
/*multi_proc_wrong.c*/
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
intmain(void)
{
pid_tchild1,child2,child;
/*創(chuàng)建兩個子進程*/
child1=fork();
child2=fork();
/*子進程1的出錯處理*/
if(child1==-1)
{
printf("Child1forkerrorn");
exit(1);
}
elseif(child1==0)/*在子進程1中調(diào)用execlp()函數(shù)*/
{
printf("Inchild1:execute'ls-l'n");
if(execlp("ls","ls","-l",NULL)<0)
{
printf("Child1execlperrorn");
}
}
if(child2==-1)/*子進程2的出錯處理*/
{
printf("Child2forkerrorn");
exit(1);
}
elseif(child2==0)/*在子進程2中使其暫停5s*/
{
printf("Inchild2:sleepfor5secondsandthenexitn");
sleep(5);
exit(0);
}
else/*在父進程中等待兩個子進程的退出*/
{
printf("Infatherprocess:n");
child=waitpid(child1,NULL,0);/*阻塞式等待*/
if(child==child1)
{
printf("Getchild1exitcoden");
}
else
{
printf("Erroroccured!n");
}
do
{
child=waitpid(child2,NULL,WNOHANG);/*非阻塞式等待*/
if(child==0)
{
printf("Thechild2processhasnotexited!n");
sleep(1);
}
}while(child==0);
if(child==child2)
{
printf("Getchild2exitcoden");
}
else
{
printf("Erroroccured!n");
}
}
exit(0);
}
編譯和運行以上代碼,并觀察其運行結(jié)果。它的結(jié)果是我們所希望的嗎?
看完前面的代碼之后,再觀察下面的代碼,它們之間有什么區(qū)別,會解決哪些問題。
/*multi_proc.c*/
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
intmain(void)
{
pid_tchild1,child2,child;
/*創(chuàng)建兩個子進程*/
child1=fork();
/*子進程1的出錯處理*/
if(child1==-1)
{
printf("Child1forkerrorn");
exit(1);
}
elseif(child1==0)/*在子進程1中調(diào)用execlp()函數(shù)*/
{
printf("Inchild1:execute'ls-l'n");
if(execlp("ls","ls","-l",NULL)<0)
{
printf("Child1execlperrorn");
}
}
else/*在父進程中再創(chuàng)建進程2,然后等待兩個子進程的退出*/
{
child2=fork();
if(child2==-1)/*子進程2的出錯處理*/
{
printf("Child2forkerrorn");
exit(1);
}
elseif(child2==0)/*在子進程2中使其暫停5s*/
{
printf("Inchild2:sleepfor5secondsandthenexitn");
sleep(5);
exit(0);
}
printf("Infatherprocess:n");
child=waitpid(child1,NULL,0);/*阻塞式等待*/
if(child==child1)
{
printf("Getchild1exitcoden");
}
else
{
printf("Erroroccured!n");
}
do
{
child=waitpid(child2,NULL,WNOHANG);/*非阻塞式等待*/
if(child==0)
{
printf("Thechild2processhasnotexited!n");
sleep(1);
}
}while(child==0);
if(child==child2)
{
printf("Getchild2exitcoden");
}
else
{
printf("Erroroccured!n");
}
}
exit(0);
}
(3)首先在宿主機上編譯調(diào)試該程序:
$gccmulti_proc.c–omulti_proc(或者使用Makefile)
(4)在確保沒有編譯錯誤后,使用交叉編譯該程序:
$arm-linux-gccmulti_proc.c–omulti_proc(或者使用Makefile)
(5)將生成的可執(zhí)行程序下載到目標板上運行。
4.實驗結(jié)果在目標板上運行的結(jié)果如下所示(具體內(nèi)容與各自的系統(tǒng)有關(guān)):
$./multi_proc
Inchild1:execute'ls-l'/*子進程1的顯示,以下是“ls–l”的運行結(jié)果*/
total28
-rwxr-xr-x1davidroot2322008-07-1804:18Makefile
-rwxr-xr-x1davidroot87682008-07-2019:51multi_proc
-rw-r--r--1davidroot14792008-07-2019:51multi_proc.c
-rw-r--r--1davidroot34282008-07-2019:51multi_proc.o
-rw-r--r--1davidroot14632008-07-2018:55multi_proc_wrong.c
Inchild2:sleepfor5secondsandthenexit/*子進程2的顯示*/
Infatherprocess:/*以下是父進程顯示*/
Getchild1exitcode/*表示子進程1結(jié)束(阻塞等待)*/
Thechild2processhasnotexited!/*等待子進程2結(jié)束(非阻塞等待)*/
Thechild2processhasnotexited!
Thechild2processhasnotexited!
Thechild2processhasnotexited!
Thechild2processhasnotexited!
Getchild2exitcode/*表示子進程2終于結(jié)束了*/
因為幾個子進程的執(zhí)行有競爭關(guān)系,因此,結(jié)果中的順序是隨機的。讀者可以思考,怎樣才可以保證子進程的執(zhí)行順序呢?
7.4.2編寫守護進程1.實驗?zāi)康?p>通過編寫一個完整的守護進程,使讀者掌握守護進程編寫和調(diào)試的方法,并且進一步熟悉如何編寫多進程程序。2.實驗內(nèi)容在該實驗中,讀者首先建立起一個守護進程,然后在該守護進程中新建一個子進程,該子進程暫停10s,然后自動退出,并由守護進程收集子進程退出的消息。在這里,子進程和守護進程的退出消息都在系統(tǒng)日志文件(例如“/var/log/messages”,日志文件的全路徑名因版本的不同可能會有所不同)中輸出。子進程退出后,守護進程循環(huán)暫停,其間隔時間為10s。
3.實驗步驟(1)畫出該實驗流程圖。
該程序流程圖如圖7.9所示。
圖7.9實驗7.4.2流程圖
(2)實驗源代碼。
具體代碼設(shè)置如下:
/*daemon_proc.c*/
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
#include<syslog.h>
intmain(void)
{
pid_tchild1,child2;
inti;
/*創(chuàng)建子進程1*/
child1=fork();
if(child1==1)
{
perror("child1fork");
exit(1);
}
elseif(child1>0)
{
exit(0);/*父進程退出*/
}
/*打開日志服務(wù)*/
openlog("daemon_proc_info",LOG_PID,LOG_DAEMON);
/*以下幾步是編寫守護進程的常規(guī)步驟*/
setsid();
chdir("/");
umask(0);
for(i=0;i<getdtablesize();i++)
{
close(i);
}
/*創(chuàng)建子進程2*/
child2=fork();
if(child2==1)
{
perror("child2fork");
exit(1);
}
elseif(child2==0)
{/*進程child2*/
/*在日志中寫入字符串*/
syslog(LOG_INFO,"child2willsleepfor10s");
sleep(10);
syslog(LOG_INFO,"child2isgoingtoexit!");
exit(0);
}
else
{/*進程child1*/
waitpid(child2,NULL,0);
syslog(LOG_INFO,"child1noticedthatchild2hasexited");
/*關(guān)閉日志服務(wù)*/
closelog();
while(1)
{
sleep(10);
}
}
}
(3)由于有些嵌入式開發(fā)板沒有syslog服務(wù),讀者可以在宿主機上編譯運行。
$gccdaemon_proc.c–odaemon_proc(或者使用Makefile)
(4)運行該程序。
(5)等待10s后,以root身份查看系統(tǒng)日志文件(例如“/var/log/messages”)。
(6)使用ps–ef|grepdaemon_proc查看該守護進程是否在運行。
4.實驗結(jié)果(1)在系統(tǒng)日志文件中有類似如下的信息顯示:
Jul2021:15:08localhostdaemon_proc_info[4940]:child2willsleepfor10s
Jul2021:15:18localhostdaemon_proc_info[4940]:child2isgoingtoexit!
Jul2021:15:18localhostdaemon_proc_info[4939]:child1noticedthatchild2hasexited
讀者可以從時間戳里清楚地看到child2確實暫停了10s。
(2)使用命令ps–ef|grepdaemon_proc可看到如下結(jié)果:
david49391021:15?00:00:00./daemon_proc
可見,daemon_proc確實一直在運行。





