資源簡介
實驗一:參考以上示例程序中建立并發進程的方法,編寫一個多進程并發執行程序。父進程首先創建一個執行ls命令的子進程然后再創建一個執行ps命令的子進程,并控制ps命令總在ls命令之前執行。

代碼片段和文件信息
/*?
*?Filename?:?pctl.c?
*?copyright?:?(C)?2006?by?zhanghonglie?
*?Function?:?父子進程的并發執行?
*/?
#include?“pctl.h“?
int?main(int?argc?char?*argv[])?
{
int?i;?
int?pid;?//存放子進程號?
int?status;?//存放子進程返回狀態?
char?*args[]?=?{“/bin/ls““-a“NULL};?//子進程要缺省執行的命令?
signal(SIGINT(sighandler_t)sigcat);?//注冊一個本進程處理鍵盤中斷的函數?
pid=fork()?;?//建立子進程
if(pid<0)?//?建立子進程失敗??
{?
??printf(“Create?Process?fail!\n“);?
??exit(EXIT_FAILURE);?
}?
if(pid?==?0)?//?子進程執行代碼段?
{?//報告父子進程進程號?
?printf(“I?am?Child?process?%d\nMy?father?is?%d\n“getpid()getppid());
?pause();?//暫停,等待鍵盤中斷信號喚醒?
//子進程被鍵盤中斷信號喚醒繼續執行?
?printf(“%d?child?will?Running:?\n“getpid());?//?
?if(argv[1]?!=?NULL){?
//如果在命令行上輸入了子進程要執行的命令?
//則執行輸入的命令?
???for(i=1;?argv[i]?!=?NULL;?i++)?printf(“%s?“argv[i]);?
???printf(“\n“);?
???//裝入并執行新的程序?
???status?=?execve(argv[1]&argv[1]NULL);?
??}?
?else{?
//如果在命令行上沒輸入子進程要執行的命令
//則執行缺省的命令?
???for(i=0;?args[i]?!=?NULL;?i++)?printf(“%s?“args[i]);?
???printf(“\n“);?
????//裝入并執行新的程序?
???status?=?execve(args[0]argsNULL);?
?????}?
}?
else?//父進程執行代碼段?
{?
??printf(“\nI?am?Parent?process?%d\n“getpid());?//報告父進程進程號
??if(argv[1]?!=?NULL){?
??//如果在命令行上輸入了子進程要執行的命令?
??//則父進程等待子進程執行結束?
????printf(“Waiting?for?child?done.\n\n“);?
?????waitpid(pid&status0);?//等待子進程結束?
????printf(“\nMy?child?exit!?status?=?%d\n\n“status);
???}
??else{
????//如果在命令行上沒輸入子進程要執行的命令?
????//喚醒子進程,與子進程并發執行不等待子進程執行結束,?
????????if(kill(pidSIGINT)?>=?0)?
???????????printf(“%d?Wakeup?%d?child.\n“getpid()pid)?;?
??????printf(“%d?don‘t?Wait?for?child?done.\n\n“getpid());?
??????}?
?}?
??return?EXIT_SUCCESS;?
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2014-04-13?17:00??test1\
?????文件?????????174??2014-04-13?15:20??test1\Makefile
?????文件?????????174??2014-04-13?15:11??test1\Makefile~
?????文件????????8931??2014-04-13?16:20??test1\pctl
?????文件????????2139??2014-04-13?16:20??test1\pctl.c
?????文件????????2142??2014-04-13?14:51??test1\pctl.c~
?????文件?????????280??2014-04-13?14:53??test1\pctl.h
?????文件???????????0??2014-04-13?14:51??test1\pctl.h~
?????文件????????4284??2014-04-13?16:20??test1\pctl.o
?????目錄???????????0??2014-04-23?22:27??test2\
?????文件?????????151??2014-04-23?22:00??test2\Makefile
?????文件????????9163??2014-04-23?22:20??test2\ppipe
?????文件????????2574??2014-04-23?22:20??test2\ppipe.c
?????文件????????4824??2014-04-23?22:20??test2\ppipe.o
?????目錄???????????0??2014-04-28?19:46??test3\
?????文件?????????168??2014-04-27?22:34??test3\Makefile
?????文件??????375242??2014-04-28?19:44??test3\X67F9PVLKR}2WRFP$5OCY58.jpg
?????文件????????9780??2014-04-28?19:40??test3\psched
?????文件????????1975??2014-04-28?19:40??test3\psched.c
?????文件????????5440??2014-04-28?19:40??test3\psched.o
?????目錄???????????0??2014-05-02?22:33??test4\
?????文件?????????379??2014-05-02?21:56??test4\Makefile
?????文件???????????0??2014-05-02?21:56??test4\Makefile~
?????文件???????61913??2014-05-02?22:31??test4\QQ圖片20140502223119.jpg
?????文件???????24968??2014-05-02?22:32??test4\QQ圖片20140502223219.jpg
?????文件???????18417??2014-05-02?22:27??test4\consumer
?????文件????????3573??2014-05-02?21:55??test4\consumer.c
?????文件????????3573??2014-05-02?21:54??test4\consumer.c~
?????文件????????6816??2014-05-02?21:59??test4\consumer.o
?????文件????????4204??2014-05-02?20:38??test4\ipc.c
?????文件????????4204??2014-05-02?20:37??test4\ipc.c~
............此處省略38個文件信息
- 上一篇:IOCP 客戶端和服務端
- 下一篇:模擬段式存儲管理的分配與回收操作系統課程設計
評論
共有 條評論