?
7.4??實(shí)驗(yàn)內(nèi)容
7.4.1??編寫多進(jìn)程程序
1.實(shí)驗(yàn)?zāi)康?/h4>
通過(guò)編寫多進(jìn)程程序,使讀者熟練掌握f(shuō)ork()、exec()、wait()和waitpid()等函數(shù)的使用,進(jìn)一步理解在Linux中多進(jìn)程編程的步驟。
2.實(shí)驗(yàn)內(nèi)容
該實(shí)驗(yàn)有3個(gè)進(jìn)程,其中一個(gè)為父進(jìn)程,其余兩個(gè)是該父進(jìn)程創(chuàng)建的子進(jìn)程,其中一個(gè)子進(jìn)程運(yùn)行“l(fā)s?-l”指令,另一個(gè)子進(jìn)程在暫停5s之后異常退出,父進(jìn)程先用阻塞方式等待第一個(gè)子進(jìn)程的結(jié)束,然后用非阻塞方式等待另一個(gè)子進(jìn)程的退出,待收集到第二個(gè)子進(jìn)程結(jié)束的信息,父進(jìn)程就返回。
3.實(shí)驗(yàn)步驟
(1)畫出該實(shí)驗(yàn)流程圖。
該實(shí)驗(yàn)流程圖如圖7.8所示。
圖7.8??實(shí)驗(yàn)7.4.1流程圖
(2)實(shí)驗(yàn)源代碼。
先看一下下面的代碼,這個(gè)程序能得到我們所希望的結(jié)果嗎,它的運(yùn)行會(huì)產(chǎn)生幾個(gè)進(jìn)程?請(qǐng)讀者回憶一下fork()調(diào)用的具體過(guò)程。
/*?multi_proc_wrong.c?*/
#include?<stdio.h>
#include?<stdlib.h>
#include?<sys/types.h>
#include?<unistd.h>
#include?<sys/wait.h>
int?main(void)
{
????pid_t?child1,?child2,?child;
????/*創(chuàng)建兩個(gè)子進(jìn)程*/
????child1?=?fork();
????child2?=?fork();
????/*子進(jìn)程1的出錯(cuò)處理*/
????if?(child1?==?-1)
????{
????????printf("Child1?fork?errorn");
????????exit(1);
????}
????else?if?(child1?==?0)?/*在子進(jìn)程1中調(diào)用execlp()函數(shù)*/
????{
????????printf("In?child1:?execute?'ls?-l'n");
????????if?(execlp("ls",?"ls","-l",?NULL)<0)
????????{
????????????printf("Child1?execlp?errorn");
????????}
????}
??
????if?(child2?==?-1)?/*子進(jìn)程2的出錯(cuò)處理*/
????{
?????????printf("Child2?fork?errorn");
?????????exit(1);
????}
????else?if(?child2?==?0?)?/*在子進(jìn)程2中使其暫停5s*/
????{
?????????printf("In?child2:?sleep?for?5?seconds?and?then?exitn");
?????????sleep(5);
?????????exit(0);
????}
????else?/*在父進(jìn)程中等待兩個(gè)子進(jìn)程的退出*/
????{
?????????printf("In?father?process:n");
?????????child?=?waitpid(child1,?NULL,?0);?/*?阻塞式等待?*/
?????????if?(child?==?child1)
?????????{
?????????????printf("Get?child1?exit?coden");
?????????}
?????????else
?????????{
?????????????printf("Error?occured!n");
?????????}
??????
?????????do
?????????{
?????????????child?=waitpid(child2,?NULL,?WNOHANG);/*?非阻塞式等待?*/
?????????????if?(child?==?0)
?????????????{
?????????????????printf("The?child2?process?has?not?exited!n");
?????????????????sleep(1);
?????????????}
????????}?while?(child?==?0);
??????
?????????if?(child?==?child2)
?????????{
?????????????printf("Get?child2?exit?coden");
?????????}
?????????else
?????????{
?????????????printf("Error?occured!n");
?????????}
????}??
????exit(0);
}
?
編譯和運(yùn)行以上代碼,并觀察其運(yùn)行結(jié)果。它的結(jié)果是我們所希望的嗎?
看完前面的代碼之后,再觀察下面的代碼,它們之間有什么區(qū)別,會(huì)解決哪些問(wèn)題。
/*multi_proc.c?*/
#include?<stdio.h>
#include?<stdlib.h>
#include?<sys/types.h>
#include?<unistd.h>
#include?<sys/wait.h>
int?main(void)
{
????pid_t?child1,?child2,?child;
????
????/*創(chuàng)建兩個(gè)子進(jìn)程*/
????child1?=?fork();
????????
????/*子進(jìn)程1的出錯(cuò)處理*/
????if?(child1?==?-1)
????{
????????printf("Child1?fork?errorn");
????????exit(1);
????}
????else?if?(child1?==?0)?/*在子進(jìn)程1中調(diào)用execlp()函數(shù)*/
????{
????????printf("In?child1:?execute?'ls?-l'n");
????????if?(execlp("ls",?"ls",?"-l",?NULL)?<?0)
????????{
????????????printf("Child1?execlp?errorn");
????????}
????}
????else?/*在父進(jìn)程中再創(chuàng)建進(jìn)程2,然后等待兩個(gè)子進(jìn)程的退出*/
????{
?????????child2?=?fork();
?????????if?(child2?==?-1)?/*子進(jìn)程2的出錯(cuò)處理*/
?????????{
?????????????printf("Child2?fork?errorn");
?????????????exit(1);
????????}
????????else?if(child2?==?0)?/*在子進(jìn)程2中使其暫停5s*/
?????????{
?????????????printf("In?child2:?sleep?for?5?seconds?and?then?exitn");
?????????????sleep(5);
?????????????exit(0);
?????????}
??
?????????printf("In?father?process:n");
?????????child?=?waitpid(child1,?NULL,?0);?/*?阻塞式等待?*/
?????????if?(child?==?child1)
?????????{
?????????????printf("Get?child1?exit?coden");
?????????}
?????????else
?????????{
??????????????printf("Error?occured!n");
?????????}
??????
?????????do
?????????{
?????????????child?=?waitpid(child2,?NULL,?WNOHANG?);?/*?非阻塞式等待?*/
?????????????if?(child?==?0)
?????????????{
??????????????????printf("The?child2?process?has?not?exited!n");
??????????????????sleep(1);
?????????????}
?????????}?while?(child?==?0);
??
?????????if?(child?==?child2)
?????????{
?????????????printf("Get?child2?exit?coden");
?????????}
?????????else
?????????{
?????????????printf("Error?occured!n");
?????????}
????}??
????exit(0);
}
(3)首先在宿主機(jī)上編譯調(diào)試該程序:
$?gcc?multi_proc.c?–o?multi_proc(或者使用Makefile)
(4)在確保沒(méi)有編譯錯(cuò)誤后,使用交叉編譯該程序:
$?arm-linux-gcc?multi_proc.c?–o?multi_proc?(或者使用Makefile)
(5)將生成的可執(zhí)行程序下載到目標(biāo)板上運(yùn)行。
4.實(shí)驗(yàn)結(jié)果
在目標(biāo)板上運(yùn)行的結(jié)果如下所示(具體內(nèi)容與各自的系統(tǒng)有關(guān)):
$?./multi_proc
In?child1:?execute?'ls?-l'????????/*?子進(jìn)程1的顯示,?以下是“l(fā)s?–l”的運(yùn)行結(jié)果?*/
total?28
-rwxr-xr-x?1?david?root??232?2008-07-18?04:18?Makefile
-rwxr-xr-x?1?david?root?8768?2008-07-20?19:51?multi_proc
-rw-r--r--?1?david?root?1479?2008-07-20?19:51?multi_proc.c
-rw-r--r--?1?david?root?3428?2008-07-20?19:51?multi_proc.o
-rw-r--r--?1?david?root?1463?2008-07-20?18:55?multi_proc_wrong.c
In?child2:?sleep?for?5?seconds?and?then?exit?/*?子進(jìn)程2的顯示?*/
In?father?process:?????????????????????????????????/*?以下是父進(jìn)程顯示?*/
Get?child1?exit?code???????????????????????????????/*?表示子進(jìn)程1結(jié)束(阻塞等待)*/
The?child2?process?has?not?exited!??????????????/*?等待子進(jìn)程2結(jié)束(非阻塞等待)*/
The?child2?process?has?not?exited!
The?child2?process?has?not?exited!
The?child2?process?has?not?exited!
The?child2?process?has?not?exited!
Get?child2?exit?code???????????????????????????????/*?表示子進(jìn)程2終于結(jié)束了*/
因?yàn)閹讉€(gè)子進(jìn)程的執(zhí)行有競(jìng)爭(zhēng)關(guān)系,因此,結(jié)果中的順序是隨機(jī)的。讀者可以思考,怎樣才可以保證子進(jìn)程的執(zhí)行順序呢?
?
7.4.2??編寫守護(hù)進(jìn)程
1.實(shí)驗(yàn)?zāi)康?/h4>
通過(guò)編寫一個(gè)完整的守護(hù)進(jìn)程,使讀者掌握守護(hù)進(jìn)程編寫和調(diào)試的方法,并且進(jìn)一步熟悉如何編寫多進(jìn)程程序。
2.實(shí)驗(yàn)內(nèi)容
在該實(shí)驗(yàn)中,讀者首先建立起一個(gè)守護(hù)進(jìn)程,然后在該守護(hù)進(jìn)程中新建一個(gè)子進(jìn)程,該子進(jìn)程暫停10s,然后自動(dòng)退出,并由守護(hù)進(jìn)程收集子進(jìn)程退出的消息。在這里,子進(jìn)程和守護(hù)進(jìn)程的退出消息都在系統(tǒng)日志文件(例如“/var/log/messages”,日志文件的全路徑名因版本的不同可能會(huì)有所不同)中輸出。子進(jìn)程退出后,守護(hù)進(jìn)程循環(huán)暫停,其間隔時(shí)間為10s。
3.實(shí)驗(yàn)步驟
(1)畫出該實(shí)驗(yàn)流程圖。
該程序流程圖如圖7.9所示。
圖7.9??實(shí)驗(yàn)7.4.2流程圖
?
(2)實(shí)驗(yàn)源代碼。
具體代碼設(shè)置如下:
/*?daemon_proc.c?*/
#include?<stdio.h>
#include?<stdlib.h>
#include?<sys/types.h>
#include?<unistd.h>
#include?<sys/wait.h>
#include?<syslog.h>
int?main(void)
{
????pid_t?child1,child2;
????int?i;
????
?????/*創(chuàng)建子進(jìn)程1*/
????child1?=?fork();
????if?(child1?==??1)
????{
?????????perror("child1?fork");
?????????exit(1);
????}
????else?if?(child1?>?0)
????{
????????exit(0);????????/*?父進(jìn)程退出*/
????}
????/*打開日志服務(wù)*/
????openlog("daemon_proc_info",?LOG_PID,?LOG_DAEMON);
????
????/*以下幾步是編寫守護(hù)進(jìn)程的常規(guī)步驟*/
????setsid();
????chdir("/");
????umask(0);
????for(i?=?0;?i?<?getdtablesize();?i++)
????{
???????close(i);
????}
????
????/*創(chuàng)建子進(jìn)程2*/
????child2?=?fork();
????if?(child2?==??1)
????{
?????????perror("child2?fork");
?????????exit(1);
????}
????else?if?(child2?==?0)
????{?/*?進(jìn)程child2?*/
??????/*在日志中寫入字符串*/
????????syslog(LOG_INFO,?"?child2?will?sleep?for?10s?");
????????sleep(10);
????????syslog(LOG_INFO,?"?child2?is?going?to?exit!?");
????????exit(0);
????}
????else
????{?/*?進(jìn)程child1*/
????????waitpid(child2,?NULL,?0);
????????syslog(LOG_INFO,?"?child1?noticed?that?child2?has?exited?");
????????/*關(guān)閉日志服務(wù)*/
????????closelog();
????????while(1)
????????{
?????????????sleep(10);
??????????}
????}
}
(3)由于有些嵌入式開發(fā)板沒(méi)有syslog服務(wù),讀者可以在宿主機(jī)上編譯運(yùn)行。
$?gcc?daemon_proc.c?–o?daemon_proc?(或者使用Makefile)
(4)運(yùn)行該程序。
(5)等待10s后,以root身份查看系統(tǒng)日志文件(例如“/var/log/messages”)。
(6)使用ps?–ef?|?grep?daemon_proc查看該守護(hù)進(jìn)程是否在運(yùn)行。
4.實(shí)驗(yàn)結(jié)果
(1)在系統(tǒng)日志文件中有類似如下的信息顯示:
Jul?20?21:15:08?localhost?daemon_proc_info[4940]:??child2?will?sleep?for?10s?
Jul?20?21:15:18?localhost?daemon_proc_info[4940]:??child2?is?going?to?exit!?
Jul?20?21:15:18?localhost?daemon_proc_info[4939]:??child1?noticed?that?child2?has?exited
讀者可以從時(shí)間戳里清楚地看到child2確實(shí)暫停了10s。
(2)使用命令ps?–ef?|?grep?daemon_proc可看到如下結(jié)果:
david?????4939?????1??0?21:15??????????00:00:00?./daemon_proc
可見(jiàn),daemon_proc確實(shí)一直在運(yùn)行。