Ankoku
Ankoku

Reputation: 379

wait4 and waitpid not returning the correct state

I'm executing this code:

void update_process(PROCSP * process){

    int state;
    printf("process.pid = %d\n", process->pid);
    if (process->state== TERM || process->state == SIG)
        return;
    process->prio = getpriority(PRIO_PROCESS, process->pid);
    errno = 0;
    if (wait4(process->pid, &state, WNOHANG | WUNTRACED, &(process->r)) != -1){
        if (WIFEXITED(state))
            process->state= TERM;
        else if(WIFSIGNALED(state))
            process->state= SIG;
        else if(WIFSTOPPED(state))
            process->state= STOP;
        else process->state= ACT;
    } else{
        perror("wait4");
    return;
    }
}

The weird thing is that when I print the value of state after calling the function wait4 it doesn't change, so the function gives me the wrong state of it. I'm sure that the process exists and it has the right pid (at least when I execute "ps all" in Linux it shows the same pid than the one I pass to the function).

All this code is executed after a fork and an execv (included in the function CMD_execute):

void CMD_background(char * argv[]){
    if(argv[0]==NULL){
        return;
    }
    pid_t pid = fork();
    if (pid == 0){
        CMD_execute(argv);   
        return;
    } else {
        background_add_proc(argv, pid);   
    }
}

I've also changed the wait4 to a waitpid but the same thing happens.

What can be the problem?

Thanks!

Upvotes: 0

Views: 742

Answers (2)

Per Johansson
Per Johansson

Reputation: 6887

My man page says When the WNOHANG option is specified and no processes wish to report status, wait4() returns a process id of 0. which is probably what's happening.

state is only set if a process has actually exited/stopped, and wait4 returns a value > 0. It's not specifically stated, but only that makes sense, since there's no WIFRUNNING macro.

Upvotes: 1

Alex
Alex

Reputation: 1232

You might have SIGCHLD on ignore. In that case you can't check the exit code of child process. If it doesn't look like your code is ignoring the signal, look into any 3-rd party libraries you're using, I believe Oracle ignores it, so some 3'rd party libraries change default behavior of the signal.

Upvotes: 1

Related Questions