This article is senlie original, reproduced Please retain this address: http://blog.csdn.net/zhengsenlie
1. Only let your process call accept, and then "pass" the accepted connected socket to a sub-process.
In this way, you do not need to provide lock protection because all sub-processes call accept.
2. The parent process must track the idle status of the child process to pass a new socket to the idle child process.
Typedef struct {pid_tchild_pid;/* the ID of the child process */intchild_pipefd;/* the byte stream pipeline descriptor */intchild_status that is connected to the child process in the parent process; /* sub-process status */longchild_count;/* customer count processed by the sub-process */} child; child * cptr;/* array of child structures; calloc 'ed * // * include serv05a */static intnchildren; intmain (INT argc, char ** argv) {intlistenfd, I, navail, maxfd, NSEL, connfd, RC; // navail indicates the number of available sub-processes voidsig_int (INT); pid_tchild_make (INT, Int, INT); s Size_tn; fd_setrset, masterset; socklen_taddrlen, clilen; struct sockaddr * cliaddr; // 0. create a listener socket If (argc = 3) listenfd = tcp_listen (null, argv [1], & addrlen); else if (argc = 4) listenfd = tcp_listen (argv [1], argv [2], & addrlen); elseerr_quit ("Usage: serv05 [
UNIX Network Programming volume 1 Server programming paradigm 5 preemptible child processes, where parent processes transmit socket descriptors to child Processes