标签:
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]),argv[2],&addrlen);
else
err_quit("usage:server02");
nchildren=atoi(argv[argc-1]);
pids=Calloc(nchildren,sizeof(pid_t));
for(i=0;i<nchildren,sizeof(pid_t));
pids[i]=child_make(i,listenfd,addrlen); //这里有父进程和子进程的
Signal(SIGINT,sig_int);
while(1)
pause();
}
void sig_int(int signo){ //wait总是返回第一个终止的子进程
int i;
void pr_cpu_time(void);
for(i=0;i<nchildren;i++)
kill(pids[i],SIGTERM);
while(wait(NULL)>0)//retunr child pid
{}
if(errno!=ECHILD)
err_sys("wait error");
pr_cpu_time();
exit(0);
}
pid_t child_make(int i,int listenfd,int addrlen){
pid_t pid;
void child_main(int,int,int);
if((pid=fork()>0)
return pid;
child_main(i,listenfd,addrlen);
}
void child_main(int i,int listenfd,int addrlen)
{ int connfd;
void web_child(int);
socklen_t clilen;
struct sockaddr*cliaddr;
cliaddr=Malloc(addrlen);
while(1){
clien=addrlen;
connfd=accept(listenfd,cliaddr,&clilen);
web_child(connfd);
close(connfd);
}
}
标签:
原文地址:http://www.cnblogs.com/kkshaq/p/4458534.html