Solved

Equivalent of waitpid( -1, *stat_loc, WNOHANG ); in windows. Thread Safe

Posted on 2003-12-03
7
594 Views
Last Modified: 2008-01-16
Hi,

I want to implement an equivalent of wait/waitpid in Windows. The problem that I am facing here is that I have to wait for all process that have been created till this point in time. There are two threads which are running, one thread keeps on creating processes and the other thread waits for all the process that have been created. Windows provides WaitForMultipleObjects() methods but that will not suffice as I cannot add more processes to the set of handles for which the second thread is waiting. Also there is a question of thread safety. A direct equivalent or a strategy to get similar function is required.
0
Comment
Question by:supersnoop
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
7 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 9865227
Please describe what do you want exactly. One thread creates some number of processes. Another thread waits when all these processes finished.
Why WaitForMultipleObjects is not solves this problem?
0
 

Author Comment

by:supersnoop
ID: 9865263
There are two threads which are running parallely. One thread (Say T1) responsibilty is to spawn new processes and the other thread (say T2) is waiting for signals from these threads. As sonn as T2 gets a signal from any of the process being waited on, it will report the status of the process, however it will continue to read signals from the processes. I will not know before hand the number of processes I want to create, as the creation of processes is dynamic on user request. WaitForMultipleObjects does not solve my problem as I would like to add new processes to the list of processes being waited for after I call the WaitForMultiple objects.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9865361
Still don't understand exactly what you need: what is "signal from any of the process". Second thread reports about finishing of each process?

WaitForMultipleObjects has bWaitAll parameter. If it is FALSE, function returns if at least one of the processes finished. Notice that next time you can call WaitForMultipleObjects with another (updated) list of project handles.

List of processes launched by thread1 may be kept in global container (like std::list) protected by critical section. Thread 1 adds new elements to it. Thread 2 removes them.

One of synchronization objects on which WaitForMultipleObjects waits in thread 2 should be event set in thread 1 when new process is launched. When this happens, WaitForMultipleObjects should exit and called again with updated list of processes.

Pseudo-code.
Thread 1:

while (...)
{
    do something...
    create process
    add process handle to global list
    set event "Process is created"
}


Thread 2:

while (...)
{
     WaitForMultipleObjects(<all process handles from list + event "Process is created">, bWaitAll = FALSE)

     if ( one of processes is signaled )
     {
          report
          remove process handle from list
          continue (wait again)
      }

     if ( "Process is created" event is signaled )
     {
           continue (wait on refreshed list of processes)
     }
}
0
 

Author Comment

by:supersnoop
ID: 10319126
Hi,

I am sorry about not adding comments before hand. Actually I found the answer myself about the equivalent functionality. There is a function in the new SDK called RegisterWaitForSingleObject wich does exactly what was required. Anyways thanks for the comments on WaitForMultipleObjects, only after a lot of deliberation we found that WaitForMultipleObjects was not suficing our requirement,
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 10969878
PAQed, with points refunded (125)

ee_ai_construct - (re-order part number #xm34)
Community Support Moderator
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Difference between List and Array in Python 5 117
Delphi: barcode reading on android platform 1 73
Java array 10 61
Error building VS2105 solution from repository 1 37
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
A short article about a problem I had getting the GPS LocationListener working.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question