Solved

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

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
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…

786 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