Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

Port Windows IPC Apps to Linux, Part 1: Processes and Threads

By Srinivasan S. Muthuswamy, Kavitha Varadarajan
2005-06-16


Using wait functions

In cases when the child process is dependent on the parent process, you can use wait functions in parent process to wait for the child process termination. In Windows, you can use the WaitForSingleObject() function call to achieve this.

You can use the WaitForMultipleObject() function to wait for more than one object.



DWORD WaitForMultipleObjects(
DWORD nCount, // number of handles in array
CONST HANDLE *lpHandles, // object-handle array
BOOL bWaitAll, // wait option
DWORD dwMilliseconds // time-out interval
);

You can populate the object-handle array with the number of objects to wait for. Based on the bWaitALL option, you can either wait for all the objects to be signaled or wait for any of them to be signaled.

In both of these functions, if you want to wait for a finite time, you can specify the time interval in the second parameter. If you want to wait infinitely, use INFINITE as the value for dwMilliseconds. Setting dwMilliseconds to 0 will just test the state of the object and return.

You can use waitpid() in Linux if you want to just wait infinitely for the process to die. In Linux, there is no way to do a timed wait on a waitpid() call.

In this code: pid_t waitpid(pid_t pid, int *status, int options), waitpid() infinitely waits for the child process to terminate. Wait functions, in both Windows and Linux, suspend the execution of the current process until it completes, but in Windows there is an option to exit by specifying a time value. You can implement a timed wait or NO WAIT functionality similar to WaitForSingleObject() and WaitForMultipleObject() using System V semaphores, which is discussed in Part 2 of this series. Part 3 of this series further discusses wait functions.



Tutorial Pages:
» A mapping guide for complex, multithreaded, multiprocess applications
» Processes
» Creating a process
» Terminating a process
» Using wait functions
» Exiting a process
» Environment variables
» Examples
» Threads
» Examples of processes and threads
» Next in the series
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» How to Install PHP 5 on Linux
» How to Install Apache 2 on Linux
» How to Install MySQL 5.0 on Linux
» SMB Caching
» Mound --Bind
» Tar Wild Card Interpretation

Advertise with Us!


Tutorials Scripts Web Hosting Developer Manuals
Resources