开发者

socket pairs, perl, KEEPALIVE, and polling

开发者 https://www.devze.com 2022-12-14 06:00 出处:网络
I\'ve starting using socketpairs on linux and windows in order to capture the output of subprocesses on both platforms.I do this by copying STD* onto one of the sockets in the socketpair (I\'m using W

I've starting using socketpairs on linux and windows in order to capture the output of subprocesses on both platforms. I do this by copying STD* onto one of the sockets in the socketpair (I'm using Win32::SocketPair in perl for socketpair's on windows). The main reason I am doing this is so that read do NOT block on the output file handles.

The problem I have is that kill(0,...) doesnt work on windows, so I need another way to detect the process as down. I looked into SO_KEEPALIVE, but that doesn't seem to work on socketpairs... I used setsockopt(...) then getsockopt(...) and the SO_KEEPALIVE option was off before and after the call to setsockopt().

Then I started looking into polling for events on the socket. The POLLHUP event looked promising, but I'm not sure it works when they're used like this.

I'm looking to automate interactive programs (no, I can't use Except as it doesn't work on windows platforms... unless you found one that does?). I've been testing with "cat" on windows (I 开发者_C百科have cat.exe installed). It has looked good so far, but there are 2 issues:

  • can't detect if process just dies (kill(0,...) doesnt work)
  • can't detect EOF from subprocess (sysread seems to always return undef or bytes read, never 0, even after I shutdown($sock, 1))

I may be doing something extremely dumb, but any advice/help would be appreciated.

UPDATE: I started using waitpid($pid, WNOHANG) to detect the "still running" condition which helps as it seems the process always dies after everything has been read. waitpid returns 0 if the pid is still running. However, it's not an EOF, its better than nothing, but I'm still looking for other input. This obviously isn't ideal.

UPDATE2: This Q/A helped with the EOF part of my question, not perfect, but better: How epoll detect clientside close in Python?


If it's supported in the WIN32 environment, and your process is spawning the child processes, you could try setting up a signal handler to catch a SIGCHLD signal from the spawned processes in the parent process... something like:

$SIG{CHLD} = sub { print "caught sigchld from child process"; };

That should tell you if one of the subprocesses exited.


In Linux, you can set a socket to be non-blocking (since at least 5.8):

 $socket->blocking(0);   # or (*SOCKET)->blocking(0);

That doesn't work on Windows (well, maybe it does in 5.10), but you can use the 4-arg select call to see if there is input on a socket before you attempt to read it (and in Windows, 4-arg select only works on sockethandles, not on filehandles or pipes).

If you still need to tell whether a Windows process (with a known PID) is active, checkout the Win32::Process::Open method, which will try to open a handle to a windows process and return zero on failure.


EDIT: I didn't notice before that you said the sockets come from subprocesses. In that case, you can just call

waitpid $pid, WNOHANG;   # and    use POSIX ':sys_wait_h'

at any time. When waitpid doesn't return -1, you know the process is dead.

0

精彩评论

暂无评论...
验证码 换一张
取 消