pthread07.c 652 B

1234567891011121314151617181920212223242526272829303132333435
  1. #include "unpthread.h"
  2. #include "pthread07.h"
  3. void
  4. thread_make(int i)
  5. {
  6. void *thread_main(void *);
  7. Pthread_create(&tptr[i].thread_tid, NULL, &thread_main, (void *) i);
  8. return; /* main thread returns */
  9. }
  10. void *
  11. thread_main(void *arg)
  12. {
  13. int connfd;
  14. void web_child(int);
  15. socklen_t clilen;
  16. struct sockaddr *cliaddr;
  17. cliaddr = Malloc(addrlen);
  18. printf("thread %d starting\n", (int) arg);
  19. for ( ; ; ) {
  20. clilen = addrlen;
  21. Pthread_mutex_lock(&mlock);
  22. connfd = Accept(listenfd, cliaddr, &clilen);
  23. Pthread_mutex_unlock(&mlock);
  24. tptr[(int) arg].thread_count++;
  25. web_child(connfd); /* process request */
  26. Close(connfd);
  27. }
  28. }