Solved

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

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now