Solved* Waitpid (unix) modifying other processes status in a loop if the process it checks is finished

49 Views Asked by At

So I wanna create a loop that checks all the processes stored in a list and prints information about them, the most important thing is their status.

It works normally, but the problem comes when a process is finished, it shows up on the list but the processes that come after it show they were finished as well even if they weren't.

I think it might be something about the status variable, maybe I gotta reset it somehow, i've tried doing it with a pointer and then freeing it but it had the same outcome. It only happens with finished and not with stopped or signaled though.

This is the function:

void cmdListJobs(){
    int i;
    int status; //No borrar más de aquí
    int pid;
    
    for(i=0;i<nprocesos;i++){
        printf("--- El pid de histprocesoelemento es %d\n",HistProcesosElemento(i)->PID);
        pid=waitpid(HistProcesosElemento(i)->PID,&status,WNOHANG| WUNTRACED| WCONTINUED);
        printf("----waitpid es %d y senal es %d\n",pid,HistProcesosElemento(i)->senal);
        
        if(strcmp("FINISHED",HistProcesosElemento(i)->status)!=0 && pid != -1){
            if(WIFEXITED(status)){
                strcpy(HistProcesosElemento(i)->status,"FINISHED");
                HistProcesos[i]->prioridad=-1;
                printf("Terminado con WIFEXITED(status)=%d\n",WIFEXITED(status));
            }else if(WIFSIGNALED(status) && (pid!=0 || HistProcesosElemento(i)->senal !=0)){
                printf("Entra\n");
                if(WTERMSIG(status)<=31){
                    strcpy(HistProcesosElemento(i)->status,"SIGNALED");
                    HistProcesos[i]->prioridad=-1;
                    HistProcesosElemento(i)->senal=WTERMSIG(status);
                    printf("----senal es %d\n",HistProcesosElemento(i)->senal);
                    printf("Terminado con WIFSIGNALED(status)=%d\n",WIFSIGNALED(status));
                }
            }else if(WIFSTOPPED(status)){
                strcpy(HistProcesosElemento(i)->status,"STOPPED");
                HistProcesosElemento(i)->senal=WSTOPSIG(status);
                printf("----senal es %d\n",HistProcesosElemento(i)->senal);
                printf("Terminado con WIFSTOPPED(status)=%d\n",WIFSTOPPED(status));
            }else if(HistProcesosElemento(i)->senal==0 && pid==0){
                strcpy(HistProcesosElemento(i)->status,"ACTIVE");
            }
        }
        
        printf("Status es %d\n",status);
        if(HistProcesosElemento(i)->senal!=0){
            printf("%d \t %s p=%d  %s  %d  %d:%02d  %s  (%s)  %s\n",HistProcesosElemento(i)->PID,HistProcesosElemento(i)->usuario, HistProcesos[i]->prioridad,nombreMes(HistProcesosElemento(i)->month), HistProcesosElemento(i)->dia, HistProcesosElemento(i)->hora, HistProcesosElemento(i)->min, HistProcesosElemento(i)->status, NombreSenal(HistProcesosElemento(i)->senal),HistProcesosElemento(i)->nombre);
        }else printf("%d \t %s p=%d  %s  %d  %d:%02d  %s  (%03d)  %s\n",HistProcesosElemento(i)->PID,HistProcesosElemento(i)->usuario, HistProcesos[i]->prioridad,nombreMes(HistProcesosElemento(i)->month), HistProcesosElemento(i)->dia, HistProcesosElemento(i)->hora, HistProcesosElemento(i)->min, HistProcesosElemento(i)->status, HistProcesosElemento(i)->senal,HistProcesosElemento(i)->nombre);
    }

}

I tried using pointers and freeing the status variable but it didn't work out, it keeps making processes after the finished one show as finished, while they're not.

Solved: I had to assign status to the result of the macro it entered

0

There are 0 best solutions below