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!
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.stateis 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 noWIFRUNNINGmacro.