1
#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() を呼び出させ、バックグラウンドで親に待機させようとしています。私のエラーは、コードの親との待機部分から来ています。私のコードがここにあると書かれている上記のすべては与えられており、編集すべきではありません

4

2 に答える 2

6
if(&background == 0)
   ^

その行はあまり意味がありません。実際に格納された値を比較したい場合、つまり& .

そうしないと、そのテストが真になることはありません。つまり、変数のアドレスbackgroundが 0 になることはありません。

于 2013-09-18T16:36:34.863 に答える
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()が、弔う死んだ子供がいない場合は戻ることができます。

これらすべては、コンパイラから得られるはずの警告に対処した後にのみ関連します。常に false であるという警告が表示されない場合はif (&background == 0)、コンパイラの警告レベルを上げる必要があります。GCC を使用している場合gcc -Wallは、良いスタートgcc -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypesです。そして、コンパイラからの警告を修正します。

于 2013-09-18T16:38:40.223 に答える