child05.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* include child_make */
  2. #include "unp.h"
  3. #include "child.h"
  4. pid_t
  5. child_make(int i, int listenfd, int addrlen)
  6. {
  7. int sockfd[2];
  8. pid_t pid;
  9. void child_main(int, int, int);
  10. Socketpair(AF_LOCAL, SOCK_STREAM, 0, sockfd);
  11. if ( (pid = Fork()) > 0) {
  12. Close(sockfd[1]);
  13. cptr[i].child_pid = pid;
  14. cptr[i].child_pipefd = sockfd[0];
  15. cptr[i].child_status = 0;
  16. return(pid); /* parent */
  17. }
  18. Dup2(sockfd[1], STDERR_FILENO); /* child's stream pipe to parent */
  19. Close(sockfd[0]);
  20. Close(sockfd[1]);
  21. Close(listenfd); /* child does not need this open */
  22. child_main(i, listenfd, addrlen); /* never returns */
  23. }
  24. /* end child_make */
  25. /* include child_main */
  26. void
  27. child_main(int i, int listenfd, int addrlen)
  28. {
  29. char c;
  30. int connfd;
  31. ssize_t n;
  32. void web_child(int);
  33. printf("child %ld starting\n", (long) getpid());
  34. for ( ; ; ) {
  35. if ( (n = Read_fd(STDERR_FILENO, &c, 1, &connfd)) == 0)
  36. err_quit("read_fd returned 0");
  37. if (connfd < 0)
  38. err_quit("no descriptor from read_fd");
  39. web_child(connfd); /* process request */
  40. Close(connfd);
  41. Write(STDERR_FILENO, "", 1); /* tell parent we're ready again */
  42. }
  43. }
  44. /* end child_main */