ast_comparator/test/student_data/handout/shlab-handout/cases/reference_26_tsh.c
/*
 * waitfg - Block until process pid is no longer the foreground process
 */
Block until process pid is no longer the foreground process
Block until process pid is no longer the foreground process
void waitfg(pid_t pid) {
    int status; 
    struct job_t *pjob; 
    int ret; 
    do {
        ret = waitpid(pid, &status, WUNTRACED); 
    } while (ret == -1 && errno == EINTR); 
    if (ret == -1 && errno == ECHILD) { 
        unix_error("waitfg: waitpid error");
        exit(1);
    } else if (ret > 0) { 
        if (WIFSTOPPED(status)) { 
            if ((pjob = getjobpid(jobs, pid)) != NULL) { 
                pjob->state = ST; 
                printf("Job [%d] (%d) stopped by signal 20\n", pjob->jid, pjob->pid);
            } else { 
                printf("waitfg: getjobpid error\n");
                exit(1);
            }
        } else if (WIFSIGNALED(status)) { 
            if ((pjob = getjobpid(jobs, pid)) != NULL) { 
                printf("Job [%d] (%d) terminated by signal 2\n", pjob->jid, pjob->pid);
                if (!deletejob(jobs, pid)) { 
                    printf("waitfg: deletejob error\n");
                    exit(1);
                }
            } else { 
                printf("waitfg: getjobpid error\n");
                exit(1);
            }
        } else if (WIFEXITED(status)) {
            if (!deletejob(jobs, pid)) {
                printf("waitfg: deletejob error\n");
                exit(1);
            }
        }
    }
    return;
}
void waitfg ( pid_t pid ) { int status ; struct job_t * pjob ; int ret ; do { ret = waitpid ( pid , & status , WUNTRACED ) ; } while ( ret == -1 && errno == EINTR ) ; if ( ret == -1 && errno == ECHILD ) { unix_error ( " " ) ; exit ( 1 ) ; } else if ( ret > 0 ) { if ( WIFSTOPPED ( status ) ) { if ( ( pjob = getjobpid ( jobs , pid ) ) != NULL ) { pjob -> state = ST ; printf ( " \n " , pjob -> jid , pjob -> pid ) ; } else { printf ( " \n " ) ; exit ( 1 ) ; } } else if ( WIFSIGNALED ( status ) ) { if ( ( pjob = getjobpid ( jobs , pid ) ) != NULL ) { printf ( " \n " , pjob -> jid , pjob -> pid ) ; if ( ! deletejob ( jobs , pid ) ) { printf ( " \n " ) ; exit ( 1 ) ; } } else { printf ( " \n " ) ; exit ( 1 ) ; } } else if ( WIFEXITED ( status ) ) { if ( ! deletejob ( jobs , pid ) ) { printf ( " \n " ) ; exit ( 1 ) ; } } } return ; }
waitfg - Block until process pid is no longer the foreground process
waitfg - Block until process pid is no longer the foreground process
// Wait status // Job pointer // Wait return value // Wait for terminated or stopped // In case being interrupted // No such child process // Terminated or stopped // Stopped // Get job // Change state // Invalid job // Terminated by other process signal // Get job // Delete job // Invalid job