#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#include <error.h>
char command[256];
void main()
{
int rtn; /*子進程的返回數(shù)值*/
while(1)
{
printf( ">" ); //從終端讀取要執(zhí)行的命令
fgets(command, 256, stdin); //將命令數(shù)據(jù)存放到command中
command[strlen(command)-1] = 0;
if (fork() == 0 ) //在子進程中執(zhí)行這個命令
{
execlp(command,command,NULL);
//如果exec函數(shù)返回,表明沒有正常執(zhí)行命令,打印錯誤信息
perror(command);
exit(1);
}
else //在父進程中等待字進程結(jié)束,并且打印子進程的返回值
{
wait( &rtn );
printf("子進程返回%d\n",rtn);
exit(0);
}
}
exit(0);
}
當前文章:[Linux進程]一個完整的進程操作實例
文章地址:http://www.chinadenli.net/article16/gsphdg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、網(wǎng)頁設計公司、服務器托管、云服務器、網(wǎng)站維護、網(wǎng)站設計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)