本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
1.允许多个进程在引用同一个监听套接字的描述符上调用 accept 这种做法并不具备兼容性。
解决的方法是让应用进程在调用 accept 前后安置某种形式的锁(lock),这样任意时刻只有一个子进程
阻塞在 accept 调用中,其他子进程则阻塞在试图获取用于保护 accept 的锁上
#include "unp.h"
static int nchildren;
static pid_t *pids;
int
main(int argc, char **argv)
{
int listenfd, i;
socklen_t addrlen;
void sig_int(int);
pid_t child_make(int, int, int);
if (argc == 3)
listenfd = Tcp_listen(NULL, argv[1], &addrlen);
else if (argc == 4)
listenfd = Tcp_listen(argv[1], argv[2], &addrlen);
else
err_quit("usage: serv03 [ <host> ] <port#> <#children>");
nchildren = atoi(argv[argc-1]);
pids = Calloc(nchildren, sizeof(pid_t));
//在派生子进程的循环之前调用 my_lock_init 函数
my_lock_init("/tmp/lock.XXXXXX"); /* one lock file for all children */
for (i = 0; i < nchildren; i++)
pids[i] = child_make(i, listenfd, addrlen); /* parent returns */
Signal(SIGINT, sig_int);
for ( ; ; )
pause(); /* everything done by children */
}
// 中断信号 SIGINT 处理函数
void
sig_int(int signo)
{
int i;
void pr_cpu_time(void);
/* terminate all children */
for (i = 0; i < nchildren; i++)
kill(pids[i], SIGTERM);
while (wait(NULL) > 0) /* wait for all children */
;
if (errno != ECHILD)
err_sys("wait error");
pr_cpu_time();
exit(0);
}
// SIGINT 信号处理函数
/* include sigint */
void
sig_int(int signo)
{
int i;
void pr_cpu_time(void);
//给每个子进程发送 SIGTERM 信号终止它们
for (i = 0; i < nchildren; i++)
kill(pids[i], SIGTERM);
//用 wait 回收所有子进程的资源
while (wait(NULL) > 0) /* wait for all children */
;
if (errno != ECHILD)
err_sys("wait error");
//调用 pr_cpu_time 统计已终止子进程的资源利用统计
pr_cpu_time();
exit(0);
}
/* end sigint */
/* include child_make */
pid_t
child_make(int i, int listenfd, int addrlen)
{
pid_t pid;
void child_main(int, int, int);
//调用 fork 派生子进程
if ( (pid = Fork()) > 0)
return(pid); //父进程返回子进程的 pid 给 main 函数,回到 main 函数里的循环继续派生其它子进程
child_main(i, listenfd, addrlen); /* 子进程调用 child_main 函数,它是无限循环 */
}
/* end child_make */
/* include child_main */
void
child_main(int i, int listenfd, int addrlen)
{
int connfd;
void web_child(int);
socklen_t clilen;
struct sockaddr *cliaddr;
cliaddr = Malloc(addrlen);
printf("child %ld starting\n", (long) getpid());
for ( ; ; ) {
clilen = addrlen;
//在调用 accept 之前获取文件锁
my_lock_wait();
//调用 accept 返回一个已连接套接字
connfd = Accept(listenfd, cliaddr, &clilen);
//在 accept 返回之后释放文件锁
my_lock_release();
//调用 web_child 处理用户请求
web_child(connfd); /* process the request */
//关闭已连接套接字
Close(connfd);
}
}
/* end child_main */
/* include my_lock_init */
static struct flock lock_it, unlock_it;
static int lock_fd = -1;
/* fcntl() will fail if my_lock_init() not called */
void
my_lock_init(char *pathname)
{
char lock_file[1024];
/* 4must copy caller's string, in case it's a constant */
strncpy(lock_file, pathname, sizeof(lock_file));
//1.创建一个唯一的路径名
lock_fd = Mkstemp(lock_file);
//2.创建一个具备该路径名的文件并从文件系统中删除该路径名
//这样,即使程序崩溃上,这个临时文件也能完全消失
Unlink(lock_file); /* but lock_fd remains open */
//3.初始化两个 flock 结构,一个用于上锁文件,一个用于解锁文件
lock_it.l_type = F_WRLCK;
lock_it.l_whence = SEEK_SET;
lock_it.l_start = 0;
lock_it.l_len = 0;
unlock_it.l_type = F_UNLCK;
unlock_it.l_whence = SEEK_SET;
unlock_it.l_start = 0;
unlock_it.l_len = 0;
}
/* end my_lock_init */
/* include my_lock_wait */
void
my_lock_wait()
{
int rc;
while ( (rc = fcntl(lock_fd, F_SETLKW, &lock_it)) < 0) {
if (errno == EINTR)
continue;
else
err_sys("fcntl error for my_lock_wait");
}
}
void
my_lock_release()
{
if (fcntl(lock_fd, F_SETLKW, &unlock_it) < 0)
err_sys("fcntl error for my_lock_release");
}
/* end my_lock_wait */