APUE2e Exercise 16.3 – Solution B
Code for client side is in Figure 16.14 in APUE2e, Page 568.
To compile the program, check this post: posix thread相关函数的编译(undefined reference to `pthread_create’)
?Download exercise16-3b.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | /** * apue-chap16: exercise16-3B.c * * Description: multiple threads, multiple endpoint to provide "ruptime" service * * Created On: Jan 11, 2012 * * @author: Huang Zhu * * @email: zhuhuang.zp@gmail.com */ #include <apueerr.h> #include <pthread.h> #include <netdb.h> #include <errno.h> #include <fcntl.h> #include <stdlib.h> #include <syslog.h> #include <sys/socket.h> #include <sys/resource.h> #include <sys/select.h> #define BUFLEN 128 #define QLEN 10 #ifndef HOST_NAME_MAX #define HOST_NAME_MAX 256 #endif typedef struct Server_FD { int fd; struct addrinfo addr; } Server_FD; int initserver(int type, const struct sockaddr *addr, socklen_t alen, int qlen) { int sockfd, err; int reuse = 1; if((sockfd = socket(addr->sa_family, type, 0)) < 0) return(-1); if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int)) < 0){ err = errno; goto errout; } if(bind(sockfd, addr, alen) < 0){ err = errno; goto errout; } if(type == SOCK_STREAM || type == SOCK_SEQPACKET){ if(listen(sockfd, qlen) < 0){ err = errno; goto errout; } } return(sockfd); errout: close(sockfd); errno = err; return(-1); } void daemonize(const char *cmd) { int i, fd0, fd1, fd2; pid_t pid; struct rlimit rl; struct sigaction sa; /* * Clear file creation mask. */ umask(0); /* * Get maximum number of file descriptors. */ if (getrlimit(RLIMIT_NOFILE, &rl) < 0) err_quit("%s: can't get file limit", cmd); /* * Become a session leader to lose controlling TTY. */ if ((pid = fork()) < 0) err_quit("%s: can't fork", cmd); else if (pid != 0) /* parent */ exit(0); setsid(); /* * Ensure future opens won't allocate controlling TTYs. */ sa.sa_handler = SIG_IGN; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; if (sigaction(SIGHUP, &sa, NULL) < 0) err_quit("%s: can't ignore SIGHUP"); if ((pid = fork()) < 0) err_quit("%s: can't fork", cmd); else if (pid != 0) /* parent */ exit(0); /* * Change the current working directory to the root so * we won't prevent file systems from being unmounted. */ if (chdir("/") < 0) err_quit("%s: can't change directory to /"); /* * Close all open file descriptors. */ if (rl.rlim_max == RLIM_INFINITY) rl.rlim_max = 1024; for (i = 0; i < rl.rlim_max; i++) close(i); /* * Attach file descriptors 0, 1, and 2 to /dev/null. */ fd0 = open("/dev/null", O_RDWR); fd1 = dup(0); fd2 = dup(0); /* * Initialize the log file. */ openlog(cmd, LOG_CONS, LOG_DAEMON); if (fd0 != 0 || fd1 != 1 || fd2 != 2) { syslog(LOG_ERR, "unexpected file descriptors %d %d %d", fd0, fd1, fd2); exit(1); } } void* serve_client(void* fd) { int clientfd; int sockfd; FILE *fp; char buf[BUFLEN]; pthread_t tid; tid = pthread_self(); sockfd = (int)fd; clientfd = accept(sockfd, NULL, NULL); //we don't care about client's identity if(clientfd < 0){ syslog(LOG_ERR, "ruptimed: accept error: %s", strerror(errno)); exit(1); } //popen, pclose: apue2e, page 503 if((fp = popen("/usr/bin/uptime", "r")) == NULL){ sprintf(buf, "error: %s\n", strerror(errno)); send(clientfd, buf, strlen(buf), 0); }else{ while(fgets(buf, BUFLEN, fp) != NULL) send(clientfd, buf, strlen(buf), 0); //prove that the thread works sprintf(buf, " thread id: %ld\n", tid); send(clientfd, buf, strlen(buf), 0); pclose(fp); } close(clientfd); return((void*)0); } void serve(fd_set *set, int maxfd, Server_FD *fdarray, int maxindex) { int serverfd; int ready; int i; pthread_t tid; int err; for(;;){ if((ready = select(maxfd+1, set, NULL, NULL, NULL)) > 0){ for(i = 0; i < maxindex; i++) { serverfd = fdarray[i].fd; if(FD_ISSET(serverfd, set)){ err = pthread_create(&tid, NULL, serve_client, (void *)serverfd); if(err != 0){ syslog(LOG_ERR, "ruptimed: pthread_create error: %s", strerror(err)); } } } } } } int main(int argc, char *argv[]) { struct addrinfo *ailist, *aip; struct addrinfo hint; int sockfd, err, n; char *host; fd_set sockset; //read sets int maxfd = -1; //maximum socket descriptor Server_FD fdarray[FD_SETSIZE]; //array to store server sockets waiting for connection requests int maxindex = 0; //the index past the last effective element in array fdarray if(argc != 1) err_quit("usage: ruptimed"); #ifdef _SC_HOST_NAME_MAX n = sysconf(_SC_HOST_NAME_MAX); if(n < 0) #endif n = HOST_NAME_MAX; host = malloc(n); if(host == NULL) err_sys("malloc error"); if(gethostname(host, n) < 0) err_sys("gethostname error"); printf("Host Name: %s\n", host); daemonize("ruptimed"); hint.ai_flags = AI_CANONNAME; hint.ai_family = 0; hint.ai_socktype = SOCK_STREAM; hint.ai_protocol = 0; hint.ai_addrlen = 0; hint.ai_canonname = NULL; hint.ai_addr = NULL; hint.ai_next = NULL; if((err = getaddrinfo(host, "ruptime", &hint, &ailist)) != 0){ syslog(LOG_ERR, "ruptimed: getaddrinfo error: %s", gai_strerror(err)); exit(1); } FD_ZERO(&sockset); for(aip = ailist; aip != NULL; aip = aip->ai_next){ if((sockfd = initserver(SOCK_STREAM, aip->ai_addr, aip->ai_addrlen, QLEN)) >= 0){ if(sockfd > maxfd) maxfd = sockfd; //add the socket to fd_set sockset FD_SET(sockfd, &sockset); //add the socket and corresponding address to array fdarray fdarray[maxindex].fd = sockfd; fdarray[maxindex].addr = *aip; maxindex++; } } if(maxfd >=0) serve(&sockset, maxfd, fdarray, maxindex); exit(1); } |




喜欢Linux C的话,《Linux C编程一站式学习》(http://learn.akae.cn/media/index.html)可以看看,目前国人所著最好的一本。用尽可能通俗易懂的语言叙述紧凑浓缩的内容,最难能可贵的是,有些内容是要看一堆docs才能提炼出来的,作者无私将它点拨出来,并且附上详细文档链接。
@Atom 翻了一下,这书的内容挺集中,涵盖了很多方面。不过我手上有好多书,基本上也包括了相同的范围,先把这些消化掉。这书印刷版的只有25章,等它出全了考虑买一本。