waitpid with a time limit

From: Frank J. Lhota (NOSPAM.lhota_at_adarose.com)
Date: 09/14/05


Date: Wed, 14 Sep 2005 14:58:47 GMT

My company may be getting a contract to port a Win32 application to
Linux (Yay!). In my off hours, I have been going through the source code
  in order to figure out how to port the Win32 API calls to Posix.

For the most part, the Win32 stuff I'm seeing have straightforward Posix
  equivalents. There is one Win32 trick, however, where I'm not quite what
the optimal Posix equivalent would be. At one point, the application
spawns another process, and waits up to X number of seconds to
terminate. If the spawned process does not terminate within X seconds,
the main application assumes that something has gone wrong and takes
corrective action.

The Linux / Posix waitpid function does wait for a given process to
terminate. But waitpid() basically waits forever, whereas we need
something with a time limit. Since this application uses lots of
threads, using an alarm signal seems ill-advised.

One approach would be to create a thread that would spawn the
child process and wait for the child to complete. This new thread could
indicate the completion of the child process by posting to a semaphore.
Then we simulate a timed waitpid() call by making a sem_timedwait() call
on this semaphore. This may work, but it seems sort of wasteful to
create a thread purely for the purpose of waiting for a child process to
complete.

Is there a better way to get the effect of a timed waitpid() call?

-- 
"All things extant in this world,
Gods of Heaven, gods of Earth,
Let everything be as it should be;
Thus shall it be!"
- Magical chant from "Magical Shopping Arcade Abenobashi"
"Drizzle, Drazzle, Drozzle, Drome,
Time for the this one to come home!"
- Mr. Lizard from "Tutor Turtle"


Relevant Pages

  • Re: EOF location?
    ... that will use Win32 semantics may show Win32 features rather than POSIX ... line is 11 characters long; 9 characters and two line-end characters. ...
    (comp.lang.cobol)
  • Re: Win32 condition variables redux
    ... Win32 is no natural ground for condition variables as ... defined by POSIX. ... it was wrong for me to try to lay a POSIX layer on top of ... My abstraction layer won't have condition variables, ...
    (comp.programming.threads)
  • Re: Confused about which way to go
    ... First versions of NT was supposed to run OS/2, posix and Win32 ... Every subsystem eventualy called native NT API. ... posix and OS/2 subsystems become absolete. ...
    (borland.public.delphi.non-technical)
  • Re: portable C code?
    ... Either ANSI or ISO. ...
    (comp.unix.programmer)
  • Re: bug in visual studio .net 2003 - breakpoints and memcpy
    ... Alexander Grigoriev wrote: ... Both Win32 and Posix define the conversion. ... Prev by Date: ...
    (microsoft.public.win32.programmer.kernel)