chuck@morgan.com (Chuck Ocheret) (02/03/89)
Here is a short test program that uses socketpair(), fork(), and execvp() to create a child process that communicate with its parent (standard stuff). What I want to do is force the child to see an eof but I still want to read from the child. The proper call (to my knowledge) is shutdown(2) which can close one end of a duplex connection. However, the child process never sees an eof. I have used sort as the child here because it produces no output until it sees eof in its input. Is this a sun bug or am I misusing shutdown(). There is no error checking in this demo to save space but my real application does error checking and receives no error results. /*--------------Cut here---------------*/ #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> main(argc, argv) int argc; char *argv[]; { int fildes[2], len; char buf[256]; static char *sortargv[] = { "sort", NULL }; /* Open pair of full duplex sockets */ (void)socketpair(PF_UNIX, SOCK_STREAM, 0, fildes); /* Fork new process */ switch (fork()) { case 0: /* Child process */ (void)close(fildes[0]); /* Close unneeded file descriptor */ (void)close(0); /* Make socket the stdin of the child */ (void)dup(fildes[1]); (void)close(1); /* Make socket the stdout of the child */ (void)dup(fildes[1]); (void)close(fildes[1]); (void)execvp(sortargv[0], sortargv); /* Exec the new program */ exit(1); break; default: /* Parent process */ break; } (void)close(fildes[1]); (void)write(fildes[0], "this\n", strlen("this\n")); (void)write(fildes[0], "is\n", strlen("is\n")); (void)write(fildes[0], "a\n", strlen("a\n")); (void)write(fildes[0], "test\n", strlen("test\n")); /* Disable further sends on this socket */ if (shutdown(fildes[0], 1)) { (void)perror("shutdown"); exit(1); } while ((len = read(fildes[0], buf, 256)) > 0) (void)write(1, buf, len); exit(0); } /* end of file */ /*--------------Cut here---------------*/ If sort is changed to cat as the child process, then it is clear that the connection is set up properly, but cat never exits (never sees eof). Thanks, Chuck Ocheret Sr. Staff Engineer Morgan Stanley & Co., Inc. 1251 Avenue of the Americas New York, N.Y. 10020 (212)703-4474 chuck@morgan.com