1 /* 2 * echoservert.c - A concurrent echo server using threads 3 */ 4 /* $begin echoservertmain */ 5 #include "csapp.h" 6 7 void echo(int connfd); 8 void *thread(void *vargp); 9 10 int main(int argc, char **argv) 11 { 12 int listenfd, *connfdp, port; 13 socklen_t clientlen=sizeof(struct sockaddr_in); 14 struct sockaddr_in clientaddr; 15 pthread_t tid; 16 17 if (argc != 2) { 18 fprintf(stderr, "usage: %s <port>\n", argv[0]); 19 exit(0); 20 } 21 port = atoi(argv[1]); 22 23 listenfd = Open_listenfd(port); 24 while (1) { 25 connfdp = Malloc(sizeof(int)); //line:conc:echoservert:beginmalloc 26 *connfdp = Accept(listenfd, (SA *) &clientaddr, &clientlen); //line:conc:echoservert:endmalloc 27 Pthread_create(&tid, NULL, thread, connfdp); 28 } 29 } 30 31 /* thread routine */ 32 void *thread(void *vargp) 33 { 34 int connfd = *((int *)vargp); 35 Pthread_detach(pthread_self()); //line:conc:echoservert:detach 36 Free(vargp); //line:conc:echoservert:free 37 echo(connfd); 38 Close(connfd); 39 return NULL; 40 } 41 /* $end echoservertmain */