wait(2)

Manuel M. T. Chakravarty chak@cse.unsw.edu.au
Fri, 27 Jul 2001 11:44:04 +1000


George Russell <ger@tzi.de> wrote,

> Is there a way in Glasgow Haskell to get a thread to wait on a child process
> in the same way as the Posix function wait(), and get the termination status
> of the child?  If so, how?

This is what I use when I want to avoid that wait()ing for a
process in one thread blocks the whole Haskell runtime:

    waitForExitCode pid = do
      status <- do
		  status <- getProcessStatus False False pid
		  -- Note: With WNOHANG, waitpid() can have a return value of
		  --	   0 and still an `errno != 0'; the current
		  --	   implementation of `getProcessStatus' doesn't handle
		  --	   this case.
		  errno  <- getErrorCode
		  if errno == noChildProcess 
		    then do
		      return $ Just (Exited ExitSuccess)
		    else
		      return status
      case status of
        Nothing		      -> do
				   threadDelay 10000	-- wait 10ms
				   waitForExitCode pid vec
	Just (Exited ec     ) -> return ec
	Just (Terminated sig) -> return $ ExitFailure (128 + sig)
	Just _		      -> -- can't happen, as second argument = `False'
	  error "Processes.proc: Stopped?"

Cheers,
Manuel