[英]Parent Process Waiting for Child to Finish in C
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX_LINE 80 /* 80 chars per line, per command, should be enough. */
/**
* setup() reads in the next command line, separating it into distinct tokens
* using whitespace as delimiters. It also sets the args parameter as a
* null-terminated string.
*/
void setup(char inputBuffer[], char *args[],int *background)
{
int length, /* Number of characters in the command line */
i, /* Loop index for inputBuffer array */
start, /* Index where beginning of next command parameter is */
ct; /* Index of where to place the next parameter into args[] */
ct = 0;
/* Read what the user enters on the command line */
length = read(STDIN_FILENO, inputBuffer, MAX_LINE);
start = -1;
if (length == 0)
exit(0); /* ^d was entered, end of user command stream */
if (length < 0){
perror("error reading command");
exit(-1); /* terminate with error code of -1 */
}
/* Examine every character in the inputBuffer */
for (i = 0; i < length; i++) {
switch (inputBuffer[i]){
case ' ':
case '\t' : /* argument separators */
if(start != -1){
args[ct] = &inputBuffer[start]; /* set up pointer */
ct++;
}
inputBuffer[i] = '\0'; /* add a null char; make a C string */
start = -1;
break;
case '\n': /* should be the final char examined */
if (start != -1){
args[ct] = &inputBuffer[start];
ct++;
}
inputBuffer[i] = '\0';
args[ct] = NULL; /* no more arguments to this command */
break;
case '&':
*background = 1;
inputBuffer[i] = '\0';
break;
default : /* some other character */
if (start == -1)
start = i;
}
}
args[ct] = NULL; /* just in case the input line was > 80 */
}
int main(void)
{
char inputBuffer[MAX_LINE]; /* Buffer to hold the command entered */
int background; /* Equals 1 if a command is followed by '&' */
char *args[MAX_LINE/2+1];/* Command line (of 80) has max of 40 arguments */
while (1){ /* program terminates normally inside setup */
background = 0;
printf("CSE2431Sh->");
fflush(0);
setup(inputBuffer, args, &background); /* get next command */
/* the steps are:
(1) fork a child process using fork()
(2) the child process will invoke execvp()
(3) if background == 0, the parent will wait,
otherwise returns to the setup() function. */
/* MY CODE HERE */
pid_t pid;
pid = fork();
if(pid == 0)
{
execvp(args[0],args);
/* If execvp returns, it must have failed. */
printf("Fork Failed\n");
exit(0);
}
else
{
if(&background == 0)
{
while( wait(&background) != pid)
{/* Do nothing, waiting */}
}
else
{
setup(inputBuffer, args, &background);
}
}
}
}
我試圖派生一個子進程,讓子進程調用execvp()並讓父進程在后台等待。 我的錯誤來自與父代碼的等待部分。 上面說我的代碼的所有內容都已給出,不應進行編輯
if(&background == 0) ^
那條線沒有多大意義。 當您可能想比較實際的存儲值時,即在比較地址時,即您可能要刪除&
。
否則,測試將永遠不會為真,也就是說,變量background
的地址永遠不會為0。
您的“前叉失敗”消息應為“執行失敗”(前叉有效;執行程序未執行)。 您還應該有一個單獨的“ fork failed”錯誤報告,但此刻您仍未找到該報告。 錯誤消息應該寫入stderr
而不是stdout
。
wait()
循環條件應為:
int corpse;
int status;
while ((corpse = wait(&status)) != -1 && corpse != pid)
;
調試時,在每次迭代中打印來自wait()
的信息。 注意, waitpid()
允許您在有屍體要收集時等待,而在沒有死者要哀悼的情況下可以返回。
所有這些僅在處理了應該從編譯器獲得的警告之后才有意義。 如果沒有收到有關if (&background == 0)
始終為false的警告,則需要提高編譯器警告級別。 如果您使用的是GCC,則gcc -Wall
是一個好的開始, gcc -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes
更好。 並修復來自編譯器的警告。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.