tags:

views:

167

answers:

3

Executive summary: I need a way to determine whether a Windows process I've spawned via _spawnl and am communicating with using FDs from _pipe has died.

Details:

I'm using the low-level CRT function in Windows (_eof, _read) to communicate with a process that was spawned via a call to _spawnl (with the P_NOWAIT) flag. I'm using _pipe to create file descriptors to communicate with this spawned process and passing those descriptors (the FD #) to it on the command line.

It is worth mentioning that I don't control the spawned process. It's a black box to me.

It turns out that the process we are spawning occasionally crashes. I'm trying to make my code robust to this by detecting the crash. Unfortunately, I can't see a way to do this. It seems reasonable to me to expect that a call to _eof or _read on one of those descriptors would return an error status (-1) if the process had died.

Unfortunately, that isn't the case. It appears that the descriptors have a life of their own independent of the spawned process. So even though the process on the other end is dead, I get no error status on the file descriptor I'm using to communicate with it.

I've got the PID for the nested process (returned from the _spanwnl call) but I don't see anything I can do with that. My code works really well except for one thing. I can't detect whether the spawned process is simply busy computing me an answer or has died.

If I can use the information from _pipe and _spawnl to determine if the spawned process is dead, I'll be golden.

Suggestions very welcome.

Thanks in advance.

UPDATE: I found a fairly simple solution and added it as the selected answer.

+1  A: 

You'll have to move up the food chain if you want to be able to detect that the process exited. The native CreateProcess() Win32 API can give you back a HANDLE, you can use it in the WaitForSingleObject() API to wait or test if the process has exited. If you can get the PID, you can use OpenProcess() to get the HANDLE you need.

Another approach is to use _P_WAIT to make it a synchronous call. That will require a thread.

Hans Passant
Unfortunately, this would involve completely rewriting a fair amount of code. If I have to do that, I guess I will. One thing that occurred to me since I posted the question is whether I can access the stdin and stdout file descriptors of the child process created via spawnl. If so, I could check *those* to see if they return error codes (since they are more closely tied to the spawned process). I just haven't figured out how to get those (yet).
Michael Tiller
My first cut at this used P_WAIT but the problem there is that it blocks when the spawned process dies and it never comes back. So even if I implemented in thread (which isn't really a requirement for me, I can wait for the answer), the thread will just hang.
Michael Tiller
Well, it is cut-and-paste code: http://msdn.microsoft.com/en-us/library/ms682499%28VS.85%29.aspx
Hans Passant
One other thing I didn't really mention because I wasn't really thinking along these lines. OpenProcess, etc. all require you to bring in "windows.h" but the program I'm trying to connect to is not a Windows program, it is a console app. Furthermore, so is the program that is calling it. Is it still possible to use all these windows.h functions? My initial attempt failed but that could be related to my own lack of experience with this the Windows platform.
Michael Tiller
Yes, that's not a problem.
Hans Passant
I've started trying to use this API. One issue I'm up against now is handling of the pipes. I tried using _pipe (as I had been) and that doesn't seem to allow the spawned process (spawned via CreateProcess that is) to "inherit" (in the terminology of this API) the pipes I generated with _pipe. So I looked at CreatePipe but there I can't figure out how to get the file descriptors (just a pointer to some PHANDLE data structure that doesn't seem to have any documentation). This is important because when I spawn the nested process (black box, remember) I have to pass those FDs on the command line.
Michael Tiller
Note that I set the "inherit handles" in CreateProcess to TRUE. But still the FDs created with _pipe don't seem to get passed through.
Michael Tiller
The sample code I linked doesn't require passing handles to the target process.
Hans Passant
I'm not sure my last comment was clear, so let me try and restate. I need to communicate with the spawned process using named file descriptors (not stdin and stdout). I create those with _pipe. This works with _spawnl. However, if I use CreateProcess the same pipes that worked with _spawnl no longer work (presumably because they are not accessible in the spawned process, although they are accessible using _spawnl). How do I make sure those file descriptors are present in the spawned process using CreateProcess? To be clear, I'm not talking about the example code, but my real problem.
Michael Tiller
A: 

Thanks to nobugz for lots of good advice. I didn't end up taking his route of "moving up the food chain" so I'm writing this up as a separate answer but it is very much the result of his feedback.

I used my existing call to spawnl. It turns out this was possible because when called with the P_NOWAIT mode the return value is actually the same as OpenProcess. This is confusing because spawnl has a return type of intptr_t whereas OpenProcess has a return type of HANDLE. But they are equivalent (at least based on my testing).

Knowing this, I still wasn't out of the woods. I needed a reliable way to determine if the process was still running. All the various calls I made using the process handle (e.g. GetModuleBaseName) succeeded even when the process was dead.

Ultimately, I resorted to calling GetProcessId (with my process handle) to get the process ID and then calling EnumerateProcesses and digging through the results returned from it to see if any of those running processes shared the same process ID.

I would acknowledge that using higher level APIs (as nobugz suggested) is probably the way to go in general. In my case, I already had all the code working and only needed to determine if the nested process was still running so I took a minimalist approach.

Thanks again to nobugz for all the helpful suggestions.

Michael Tiller
+1  A: 

See http://stackoverflow.com/questions/870253/how-to-know-child-process-status-and-resource-usage-on-windows

You can just use WaitForSingleObject on the handle returned from spawnl; it's working fine for me. It sounds like you might have already tried this, but maybe I'm misunderstanding your responses.

Sample code that spawns a process and waits for it to finish without using _P_WAIT:

HANDLE hProcess = (HANDLE) _spawnl(_P_NOWAIT, "slave.exe", "slave.exe", NULL);
while(1)
{
  Sleep(100);
  if (WaitForSingleObject(hProcess, 0) == WAIT_OBJECT_0)
  {
    break;
  }
}
Dan Mitchell