jesus elvira
jesus elvira

Reputation: 21

waitpid returns 0 when SIGINT (Crtl+c) on a children

I'm working in a terminal for an university work, but I'm having troubles when looking for finished children.

I have a list in which i save the background processes and this works fine, but when i bring it to the foreground and i send him a SIGINT signal with ctrl+c the waitpid returns 1 on all unfinished children but the one I kill. That's my code. I'm sure that my error is in that waitpid, but I don't know why returns a wrong value. What I want my code to do is to select when I kill a process with SIGINT and kill this one and remove this from the list but keep the unfinished commands.

int emptyBackground(List *list){ 

    ListNode *aux = list->first;
    ListNode *aux2 = NULL; 

    while(aux != NULL){
        if(waitpid(aux->pid, NULL, WNOHANG) > 0){ // This waitpid returns a non expected value
            if(aux2 != NULL){
                aux2->next = aux->next;
                free(aux);
                aux = aux2->next;
            }else{
                list->first = aux->next;
                free(aux);
                aux = list->first;
            }
        }else{
            aux2 = aux;
            aux = aux->next;
        }
    }
    return 0;
}

PD: My list has the command pid, the command and a pointer to the next node.

Upvotes: 2

Views: 486

Answers (1)

Gabriel Marks
Gabriel Marks

Reputation: 31

waitpid should never return 1. It should return either -1, 0, or the pid of the child that exited. Try using the wstatus field of waitpid to debug exactly what waitpid thinks happened to the child.

Upvotes: 3

Related Questions