您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 销售管理 > unixc第七讲守护进程与其他网络服务器编程技术
1第七讲第七讲守护进程与其他网络守护进程与其他网络服务器编程技术服务器编程技术Networkprogramming‘032目目录录几种服务器技术的比较;\tcp并发服务器-每个客户一个子进程;\tcp预先派生子进程服务器程序,accept无上锁保护;\tcp预先派生子进程服务器程序,accept使用文件锁保护;\tcp预先派生子进程服务器程序,accept使用线程互斥锁保护;\tcp预先派生子进程服务器程序,传递描述字;\tcp并发服务器程序,每个客户一个线程;\tcp预先创建线程服务器程序,每个线程各自accept;\tcp预先创建线程服务器程序,主线程统一accept;守护进程和inetd超级服务器\概述\syslogd守护进程和syslog函数\创建守护进程\inetd守护进程2Networkprogramming‘033各种类型的服务器的耗时比较各种类型的服务器的耗时比较编服务器描述进程控制CPU时间(与基准之差)号solarisDUinxBSD/OS0迭代服务器(测量基准,无进程控制)0.00.00.01简单并发服务器,为每个客户请求fork一个进程504.2168.929.62预先派生子进程,每个子进程调用accept6.21.83预先派生子进程,用文件上锁方式保护accept25.210.02.74预先派生子进程,用线程互斥锁保护accept21,55预先派生子进程,由父进程向子进程传递套接字描述字36.710.96.16并发服务器,为每个客户请求创建一个线程18.74.77预先派生子进程,用互斥锁上锁方式保护accept8.63.58预先派生子进程,由主线程调用accept14.55.0Networkprogramming‘034#defineMAXN16384/*max#bytestorequestfromserver*/intmain(intargc,char**argv){inti,j,fd,nchildren,nloops,nbytes;pid_tpid;ssize_tn;charrequest[MAXLINE],reply[MAXN];if(argc!=6)err_quit(usage:clienthostnameorIPaddrport#children#loops/child#bytes/request);nchildren=atoi(argv[3]);nloops=atoi(argv[4]);nbytes=atoi(argv[5]);snprintf(request,sizeof(request),%d\n,nbytes);/*newlineatend*/for(i=0;inchildren;i++){if((pid=Fork())==0){/*child*/TCPTCP测试用客户程序测试用客户程序3Networkprogramming‘035TCPTCP测试用客户程序(续)测试用客户程序(续)for(j=0;jnloops;j++){fd=Tcp_connect(argv[1],argv[2]);Write(fd,request,strlen(request));if((n=Readn(fd,reply,nbytes))!=nbytes)err_quit(serverreturned%dbytes,n);Close(fd);/*TIME_WAITonclient,notserver*/}printf(child%ddone\n,i);exit(0);}/*parentloopsaroundtofork()again*/}while(wait(NULL)0)/*nowparentwaitsforallchildren*/;if(errno!=ECHILD)err_sys(waiterror);exit(0);}Networkprogramming‘036TCPTCP并发服务器:并发服务器:每个客户一个子进程每个客户一个子进程intmain(intargc,char**argv){intlistenfd,connfd;pid_tchildpid;voidsig_chld(int),sig_int(int),web_child(int);socklen_tclilen,addrlen;structsockaddr*cliaddr;if(argc==2)listenfd=Tcp_listen(NULL,argv[1],&addrlen);elseif(argc==3)listenfd=Tcp_listen(argv[1],argv[2],&addrlen);elseerr_quit(usage:serv01[host]port#);cliaddr=Malloc(addrlen);Signal(SIGCHLD,sig_chld);Signal(SIGINT,sig_int);4Networkprogramming‘037TCPTCP并发服务器:并发服务器:每个客户一个子进程(续)每个客户一个子进程(续)for(;;){clilen=addrlen;if((connfd=accept(listenfd,cliaddr,&clilen))0){if(errno==EINTR)continue;/*backtofor()*/elseerr_sys(accepterror);}if((childpid=Fork())==0){/*childprocess*/Close(listenfd);/*closelisteningsocket*/web_child(connfd);/*processtherequest*/exit(0);}Close(connfd);/*parentclosesconnectedsocket*/}}Networkprogramming‘038TCPTCP并发服务器:并发服务器:每个客户一个子进程(续)每个客户一个子进程(续)voidsig_int(intsigno){voidpr_cpu_time(void);pr_cpu_time();exit(0);}#includesys/resource.h#ifndefHAVE_GETRUSAGE_PROTOintgetrusage(int,structrusage*);#endifvoidpr_cpu_time(void){doubleuser,sys;structrusagemyusage,childusage;5Networkprogramming‘039TCPTCP并发服务器:并发服务器:每个客户一个子进程(续)每个客户一个子进程(续)if(getrusage(RUSAGE_SELF,&myusage)0)err_sys(getrusageerror);if(getrusage(RUSAGE_CHILDREN,&childusage)0)err_sys(getrusageerror);user=(double)myusage.ru_utime.tv_sec+myusage.ru_utime.tv_usec/1000000.0;user+=(double)childusage.ru_utime.tv_sec+childusage.ru_utime.tv_usec/1000000.0;sys=(double)myusage.ru_stime.tv_sec+myusage.ru_stime.tv_usec/1000000.0;sys+=(double)childusage.ru_stime.tv_sec+childusage.ru_stime.tv_usec/1000000.0;printf(\nusertime=%g,systime=%g\n,user,sys);}Networkprogramming‘0310TCPTCP并发服务器:并发服务器:每个客户一个子进程(续)每个客户一个子进程(续)#defineMAXN16384/*max#bytesthataclientcanrequest*/voidweb_child(intsockfd){intntowrite;ssize_tnread;charline[MAXLINE],result[MAXN];for(;;){if((nread=Readline(sockfd,line,MAXLINE))==0)return;/*connectionclosedbyotherend*//*4linefromclientspecifies#bytestowriteback*/ntowrite=atol(line);if((ntowrite=0)||(ntowriteMAXN))err_quit(clientrequestfor%dbytes,ntowrite);Writen(sockfd,result,ntowrite);}}6Networkprogramming‘0311TCPTCP预先派生子进程服务器程序预先派生子进程服务器程序客户1客户2子进程1子进程2父进程子进程3…子进程N可用子进程池forkforkforkforkNetworkprogramming‘0312TCPTCP预先派生子进程服务器程序:预先派生子进程服务器程序:acceptaccept无上锁保护无上锁保护staticintnchildren;staticpid_t*pids;intmain(intargc,char**argv){intlistenfd,i;socklen_taddrlen;voidsig_int(int);pid_tchild_make(int,int,int);if(argc==3)listenfd=Tcp_listen(NULL,argv[1],&addrlen);elseif(argc==4)listenfd=Tcp_listen(argv[1],argv[2],&addrlen);elseerr_quit(usage:serv02[host]port##children);7Networkprogramming‘0313TCPTCP预先派生子进程服务器程序:预先派生子进程服务器程序:acceptaccept无上锁保护(续)无上锁保护(续)nchildren=atoi(argv[argc-1]);pids=Calloc(nchildren,sizeof(pid_t));for(i=0;inchildren;i++)pids[i]=child_make(i,listenfd,addrlen);/*parentreturns*/Signal(SIGINT,sig_int);for(;;)pause();/*everythingdonebychildren*/}Networkprogramming‘0314TCPTCP预先派生子进程服务器程序:预先派生子进程服务器程序:acceptaccept无上锁保护(续)无上锁保护(续)voidsig_int(intsigno){inti;voidpr_cpu_time(void);/*4terminateallchildren*/for(i=0;inchildren;i++)kill(pids[i],SIGTERM);while(wait(NULL)0)/*waitforallchildren*/;if(errno!=ECHILD)err_sys(waiterror);pr_cpu_time();exit(0);}8Networkprogramming‘0315TCPTCP预先派生子进程服务器程序:预先派生子进程服务器程序:acceptaccept无上锁保护(续)无上锁保护(续)pid_tchild_make(inti,intlistenfd,intaddrlen){pid_tpid;voidchild_main(int,int,int);if((pid=Fork())0)return
本文标题:unixc第七讲守护进程与其他网络服务器编程技术
链接地址:https://www.777doc.com/doc-1580550 .html