Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SIGCHLD Replacement for Windows

I would like to have SIGCHLD functionality on Windows (i.e., notify the parent process when a child dies). I am aware that there is no equivalent to SIGCHLD in the Windows API, but I'd like to know what the common method is for implementing this with Windows. I'm sure that this is a problem that Windows developers encounter fairly often.

The only solution I can think of now involves polling the children to see if they are still alive.

Note: My application is single-threaded, and I would like to keep it that way if possible. The application has a non-blocking event loop (using select()).

like image 559
Matt Fichman Avatar asked Dec 27 '22 06:12

Matt Fichman


1 Answers

I don't know if this is the best way, but it's certainly a way.

I assume you're creating the process with CreateProcess. This returns a PROCESS_INFORMATION structure that contains member hProcess. This is a handle to the child process you've created.

From here, you can wait on this handle with WaitOnSingleObject, which will block until the given handle is signalled (although it does take a timeout if you'd prefer to do this non-blocking - it's the closest equivalent to select you'll get).

If you do go the multi-thread route, then you can wait on a separate thread then when WaitOnSingleObject passes, you can notify a worker thread within the parent process accordingly.

In single-threaded style, you'll just be polling the handle in a loop if you use select-style semantics.

If you have several child objects to wait on (given you're already using select), you might want to consider WaitForMultipleObjects - if that's a pertinent model for your code.

like image 137
Chris J Avatar answered Jan 06 '23 01:01

Chris J