library/src/blockclass/subclasses/BP_ForkDispatcher.cc
// waits for any pid in the running list (useful for loops)
waits for any pid in the running list (useful for loops)
waits for any pid in the running list ( useful for loops )
size_t BP_ForkDispatcher::waitForAny()
{
	pid_t cur_pid = getpid();
	if(cur_pid != this->top_pid)
		return ERR_FAILURE;
	int childExitStatus = 0;
	while(1)
	{
		if(this->running_pid_n)
		if(this->running_pids)
		{
			childExitStatus = 0;
			pid_t ws = waitpid( -1, &childExitStatus, 0);
			if (ws == -1)
			{
				return -1;
			}
			P_BP_FORKDISPATCH_PARENT_PID_INFO p_info = NULL;
			size_t i = 0;
			for(; i < this->running_pid_n; i++)
			{
				if(this->running_pids[i].pid == ws)
				{
					p_info = (P_BP_FORKDISPATCH_PARENT_PID_INFO) &this->running_pids[i];
					break;
				}
			}
			if( WIFEXITED(childExitStatus) )
			{
				p_info->exited      = true;
				p_info->exit_status = WEXITSTATUS(childExitStatus);
				if(this->wait_policy == BP_FORK_WAIT_POLICY_RETURN_ON_EXIT)
					return p_info->pid;
			}
			if( WIFSIGNALED(childExitStatus) )
			{
				p_info->signaled    = true;
				p_info->exited      = true;
				p_info->last_signal = WTERMSIG(childExitStatus);
				if(this->wait_policy == BP_FORK_WAIT_POLICY_RETURN_ON_EXIT)
					return p_info->pid;
			}
		}
	}
	return 0;
}
size_t BP_ForkDispatcher :: waitForAny ( ) { pid_t cur_pid = getpid ( ) ; if ( cur_pid != this -> top_pid ) return ERR_FAILURE ; int childExitStatus = 0 ; while ( 1 ) { if ( this -> running_pid_n ) if ( this -> running_pids ) { childExitStatus = 0 ; pid_t ws = waitpid ( -1 , & childExitStatus , 0 ) ; if ( ws == -1 ) { return -1 ; } P_BP_FORKDISPATCH_PARENT_PID_INFO p_info = NULL ; size_t i = 0 ; for ( ; i < this -> running_pid_n ; i ++ ) { if ( this -> running_pids [ i ] . pid == ws ) { p_info = ( P_BP_FORKDISPATCH_PARENT_PID_INFO ) & this -> running_pids [ i ] ; break ; } } if ( WIFEXITED ( childExitStatus ) ) { p_info -> exited = true ; p_info -> exit_status = WEXITSTATUS ( childExitStatus ) ; if ( this -> wait_policy == BP_FORK_WAIT_POLICY_RETURN_ON_EXIT ) return p_info -> pid ; } if ( WIFSIGNALED ( childExitStatus ) ) { p_info -> signaled = true ; p_info -> exited = true ; p_info -> last_signal = WTERMSIG ( childExitStatus ) ; if ( this -> wait_policy == BP_FORK_WAIT_POLICY_RETURN_ON_EXIT ) return p_info -> pid ; } } } return 0 ; }
waits for any pid in the running list (useful for loops)
waits for any pid in the running list ( useful for loops )
// get real current pid before comparison // can only wait from the top pid // child exit status // while loop // only wait if we have pids to wait on // set exit status to 0 // wait onn any child process // will hold the pid info structure // loop to calculate which pid structure to use // if the process has exited, store exit status // if the process has exited by signal