Solved

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

Posted on 2003-12-03
7
584 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
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 109
Modify a small python script 19 116
Java Loop 6 67
what is the best portable software where i can test codes written in different languages 1 52
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

856 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