2

Hi I'm working on a unix shell and I'm running into two problems. I was wondering if any of you could help me out. My first problem is that the shell is not waiting for the child process to terminate. I can actually go type more commands while the child process is running. My second problems is in the following two lines. I'm not getting any display on the shell.

    fprintf(stderr, "Process name is: %s\n", commandArgv[0]);
    fprintf(stderr, "Child pid = %d\n", pid);

I have the following method to execute a process entered by the user: i.e. firefox, ls -a, etc

void execute(char *command[], char *file, int descriptor){
    pid_t pid;
    pid = fork();

    if(pid == -1){
        printf("error in execute has occurred\n");
    }
    if(pid == 0){           
        execvp(*command,command);
        fprintf(stderr, "Process name is: %s\n", commandArgv[0]);
        fprintf(stderr, "Child pid = %d\n", pid);
        wait(&status);
            exit(EXIT_SUCCESS);
    }
    else{
        printf("ignore for now\n");
    }
}

This is where I call the execute command. It works fine and launches a process, but it doesn't wait for it to finish.

 execute(commandArgv, "STANDARD",0);

Do you guys have any idea what I might be doing wrong? Thanks I really appreciate any time you take to help me on this.

4

3 回答 3

4

一旦 execvp() 运行,它将永远不会返回。它用提供的任何内容替换内存中正在运行的应用程序。所以你的 fprintf() 和 wait() 是在错误的地方。

于 2009-09-23T18:19:13.237 回答
1

除了正确计算实际逻辑(Stéphane 的建议都很好)之外,您可能还希望在 fprintf-ing 之后执行 fflush(stderr),以确保您的错误消息立即显示出来而不是被缓冲。

于 2009-09-24T21:31:31.407 回答
1

You have a little error in how the process works. After execvp is called, there is no turning back. fork() gives you have the parent and an identical child, but execvp overwrite child image to be the command you are calling.

The execvp returns only when a severe errors occur that prevent overwriting the image. So, you need to print things before its call. So you also may want to change the EXIT_SUCCESS to an EXIT_FAILURE there.

Now there is another mistake using wait: you always want the parent waiting for the child, not the other way around. You cannot ask for the child to wait. She has nothing to wait, she will run and terminate. So, you need to move the wait() call to the else part.

void execute(char *command[], char *file, int descriptor)
{
    pid_t pid;
    pid = fork();

    if(pid == -1)
    {
        printf("fork() error in execute() has occurred\n");
        return; /* return here without running the next else statement*/
    }
    if(pid == 0)
    {           
        fprintf(stderr, "Process name is: %s\n", commandArgv[0]);
        fprintf(stderr, "Child pid = %d\n", getpid());
        execvp(*command,command);
        fprintf(stderr, "Error! Can't overwrite child's image!\n");
        exit(EXIT_FAILURE);
    }
    else
    {
        printf("Parent waiting for child pid: %d\n", pid);
        wait(&status);
        printf("Parent running again\n");
    }
}

But reading your question, maybe you actually don't want the parent to wait. If that is the case, just don't use the wait() function.

Take care, Beco

Edited: some minor mistakes. pid of child is getpid()

于 2011-04-08T17:44:40.617 回答