ast_comparator/test/student_data/handout/shlab-handout/cases/reference_24_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 i;
    int jobindex = -1;
    if (pid < 1)
	app_error("waitfg: cannot wait on pid");
    for (i = 0; i < MAXJOBS; i++) 
    {
	if ((jobs[i].pid == pid) || (jobs[i].pid2 == pid) )
	{
	  jobindex = i;
	  break;
	}
    }
    if(jobindex <0)
    {
      printf("Could not find job in job list");
      return;
    }
    int status;
    while(    ((waitpid(pid, &status, WNOHANG))<=0)          &&          (jobs[jobindex].state==FG)     )
    {
      sleep(1);
    }
    return;
}
void waitfg ( pid_t pid ) { int i ; int jobindex = -1 ; if ( pid < 1 ) app_error ( " " ) ; for ( i = 0 ; i < MAXJOBS ; i ++ ) { if ( ( jobs [ i ] . pid == pid ) || ( jobs [ i ] . pid2 == pid ) ) { jobindex = i ; break ; } } if ( jobindex < 0 ) { printf ( " " ) ; return ; } int status ; while ( ( ( waitpid ( pid , & status , WNOHANG ) ) <= 0 ) && ( jobs [ jobindex ] . state == FG ) ) { sleep ( 1 ) ; } return ; }
waitfg - Block until process pid is no longer the foreground process
waitfg - Block until process pid is no longer the foreground process
//Find the job in the jobs list //Wait without hanging for the pid passed //Use sleep to avoid 'busy' wait //Wait while process is alive (no hang wait) and in fg //Waitpid returns 0 if nothing to wait for (process is not dead)     //Check that job is in FG //printf("waiting for child %d with state %d and waitpid returning %d \n",pid,jobs[jobindex].state,waitpid(pid, &status, WNOHANG)); //printf("waitfg finished child %d with state %d and waitpid returning %d \n",pid,jobs[jobindex].state,waitpid(pid, &status, WNOHANG));