tags:

views:

621

answers:

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.

+3  A: 

Once execvp() runs, it will never return. It replaces in-memory the running app with whatever was provided. So your fprintf() and wait() are in the wrong place.

Stéphane
Re-reading your question...perhaps all you need is a call to system() to run your command? Or do you specifically want to fork+execvp?
Stéphane
yes I need to fork and then execvp
A: 

Other than getting the actual logic worked out correctly (Stéphane's suggestions all good) you might also want to fflush(stderr) after fprintf-ing, to ensure your error messages make it out right away instead of being buffered.

pbr