父进程等待C完成子进程

时间:2013-09-18 16:34:13

标签: 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()并让父进程在后台等待。我的错误来自与父代码的代码的等待部分。这里所说的我的代码之上的所有内容都是给出的,不应该被编辑

2 个答案:

答案 0 :(得分:6)

if(&background == 0)
   ^

这条线没有多大意义。您可能希望比较实际存储的值时比较地址,即您可能希望放弃&

否则该测试将永远不会成立,即变量background的地址永远不会为0.

答案 1 :(得分:1)

你的'Fork failed'消息应该是'Exec failed'(fork工作; exec没有)。您还应该有一个单独的“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会更好。并修复编译器的警告。