免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1318 | 回复: 0
打印 上一主题 下一主题

TCP并发服务器程序 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-05-30 11:38 |只看该作者 |倒序浏览
我把《UNIX网络编程第一卷》的示例代码改了一点点想用于生产环境(AIX),但是还没有想好怎么样监控进程以达到在有较多客户端请求时能再产生新进程和在客户端请求减少时销毁空闲的进程。这个在书里只是提到了却没有给出示例代码。

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <sys/mman.h>
  9. #include <pthread.h>

  10. extern int errno;

  11. pthread_mutex_t *mptr; /* actual mutex will be in shared memory */

  12. static int nchildren = 4;
  13. static pid_t *pids;

  14. static int g_iPort = 11014;

  15. /********************************************************************/
  16. int main( int argc, char ** argv )
  17. {
  18.     void worldcup( void );
  19.    
  20.     pids = calloc( nchildren, sizeof(pid_t) );
  21.         if (pids == NULL)
  22.         {
  23.                 printf( "分配内存失败!\n" );
  24.             exit( 0 );
  25.         }
  26.                
  27.         setpgrp();
  28.         if (fork() == 0)
  29.         {
  30.                 worldcup();
  31.         }
  32.         return( 0 );
  33. }

  34. /********************************************************************/
  35. void worldcup( void )
  36. {
  37.         int i, listenfd;
  38.         pthread_mutexattr_t mattr;
  39.        
  40.         void sig_int( int );
  41.         pid_t child_make( int, int );
  42.        
  43.         if ((listenfd = Listen( g_iPort, 5 )) < 0)
  44.         {
  45.                 printf( "\t--监听端口:%d --失败!\n", g_iPort );
  46.                 return;
  47.         }
  48.         printf( "\t--监听端口:%d --成功!\n", g_iPort );
  49.         HDB_Log( " -- 监听端口:%d 成功!", g_iPort );
  50.        
  51.         /* get shared memory for parent and children */
  52.         mptr = mmap( NULL, sizeof(pthread_mutex_t), PROT_READ|PROT_WRITE,
  53.                      MAP_ANON|MAP_SHARED, -1, 0 );
  54.         if (mptr == MAP_FAILED)
  55.         {
  56.                 printf( "error: mmap()\n" );
  57.                 exit( 0 );
  58.         }
  59.        
  60.         /* initialize the mutex and lock it */
  61.         pthread_mutexattr_init( &mattr );
  62.         pthread_mutexattr_setpshared( &mattr, PTHREAD_PROCESS_SHARED );
  63.         pthread_mutex_init( mptr, &mattr );
  64.         pthread_mutexattr_destroy( &mattr );
  65.        
  66.         /*--------------------------------------------------------------*/
  67.         for (i=0; i<nchildren; i++)
  68.         {
  69.                 pids[i] = child_make( i, listenfd );  /* parent returns */
  70.         }
  71.        
  72.         signal( SIGINT, sig_int );
  73.        
  74.         for (;;)
  75.         {
  76.                 pause();  /* everything done by children */
  77.         }
  78. }

  79. /********************************************************************/
  80. void sig_int( int signo )
  81. {
  82.         int i;
  83.        
  84.         /* terminate all children */
  85.         for (i=0; i<nchildren; i++)
  86.         {
  87.                 if (kill( pids[i], SIGTERM ) != 0)
  88.                 {
  89.                         printf( "%s", strerror(errno) );
  90.                 }
  91.         }
  92.         while(wait(NULL)>0);  /* wait for all children */
  93.        
  94.         if (errno != ECHILD)
  95.         {
  96.                 printf( "wait error" );
  97.         }
  98.         exit( 0 );
  99. }

  100. /********************************************************************/
  101. pid_t child_make( int i, int listenfd )
  102. {
  103.         pid_t pid;
  104.         void child_main( int, int );
  105.        
  106.         if ((pid = fork()) > 0)
  107.         {
  108.                 return( pid );  /* parent */
  109.         }
  110.         child_main( i, listenfd );  /* never returns */
  111. }

  112. /********************************************************************/
  113. void child_main( int i, int listenfd )
  114. {
  115.         int connfd;
  116.         socklen_t addrlen;
  117.         struct sockaddr cliaddr;
  118.         void child_deal( int sockfd );
  119.        
  120.         HDB_Log( " -- 子进程 [%d] 准备就绪", i );
  121.        
  122.         for (;;)
  123.         {
  124.                 pthread_mutex_lock( mptr );
  125.                 if ((connfd = accept( listenfd, &cliaddr, &addrlen )) < 0)
  126.                 {
  127.                         HDB_Log( "accept error : %s", strerror(errno) );
  128.                         pthread_mutex_unlock( mptr );
  129.                         continue;
  130.                 }
  131.                 pthread_mutex_unlock( mptr );
  132.                 child_deal( connfd );
  133.                 close( connfd );
  134.         }
  135. }

  136. /********************************************************************/
  137. void child_deal( int sockfd )
  138. {
  139.     /*从sockfd读取客户端的请求报文,处理,然后返回应答报文给客户端。*/
  140. }
复制代码

[ 本帖最后由 AllenYao 于 2006-5-31 08:54 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP