Mail Archives: cygwin/2002/05/03/17:52:42
Hello,
So I was trying to create a process, which does something
difficult that may crash and have this process spawn another process to
cleanup just in case. The way it works is that they share a socketpair
and when the dangerous process dies the safe process gets an EOF from the
socket. Unfortunately there seems to be a problem with cygwin in that
this behavior totally depends on whether the second process is a child or
a parent.
Dig this:
#include <unistd.h>
#include <sys/socket.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
void
recvdata (int fd)
{
fd_set fds;
char buf[1024];
int n;
FD_ZERO (&fds);
for (;;) {
FD_SET (fd, &fds);
printf("Selecting");
select (fd + 1, &fds, NULL, NULL, NULL);
printf("Selected");
if (FD_ISSET (fd, &fds)) {
if ((n = read (fd, buf, sizeof (buf))) < 0) {
printf ("%s, read (net)", strerror(errno));
return;
}
if (!n) {
FD_ZERO (&fds);
shutdown (fd, 0);
return;
}
write (1, buf, n);
}
}
}
int main() {
int fds[2];
int pid;
int xfd;
if (socketpair (AF_INET, SOCK_STREAM, 0, fds) < 0) {
printf ("socketpair: %s\n", strerror(errno));
return 0;
}
pid = fork ();
if (pid == -1) {
printf("Error fork");
exit(0);
}
else if (pid == 0) {
// child
xfd = fds[0];
close (fds[1]);
printf("Child: Exiting\n");
} else {
// parent
xfd = fds[1];
close (fds[0]);
recvdata(xfd);
printf("Parent: Exiting");
}
}
When the parent receives the data. (As in the code above) this is the
output.
$ ./a.exe
Selecting
Child: Exiting
Selected
Connection reset by peer, read (net)Parent: Exiting
When the child receives the data. (We move the recvdata(xfd) call to
right before Child:Exiting we get the following
$ ./a.exe
Parent: ExitingSelecting
and ps shows that the child process is still running.
1640 1 704 1640 con 500 17:50:45
/home/davie/unixsock/a
Let me know if there's some trick that I can do. I tried it with AF_INET
also and it didn't work either. Or if it's something that cygwin should
fix.
Thanks,
David Euresti
--
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Bug reporting: http://cygwin.com/bugs.html
Documentation: http://cygwin.com/docs.html
FAQ: http://cygwin.com/faq/
- Raw text -