- 论坛徽章:
- 0
|
我把《UNIX网络编程第一卷》的示例代码改了一点点想用于生产环境(AIX),但是还没有想好怎么样监控进程以达到在有较多客户端请求时能再产生新进程和在客户端请求减少时销毁空闲的进程。这个在书里只是提到了却没有给出示例代码。
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/mman.h>
- #include <pthread.h>
- extern int errno;
- pthread_mutex_t *mptr; /* actual mutex will be in shared memory */
- static int nchildren = 4;
- static pid_t *pids;
- static int g_iPort = 11014;
- /********************************************************************/
- int main( int argc, char ** argv )
- {
- void worldcup( void );
-
- pids = calloc( nchildren, sizeof(pid_t) );
- if (pids == NULL)
- {
- printf( "分配内存失败!\n" );
- exit( 0 );
- }
-
- setpgrp();
- if (fork() == 0)
- {
- worldcup();
- }
- return( 0 );
- }
- /********************************************************************/
- void worldcup( void )
- {
- int i, listenfd;
- pthread_mutexattr_t mattr;
-
- void sig_int( int );
- pid_t child_make( int, int );
-
- if ((listenfd = Listen( g_iPort, 5 )) < 0)
- {
- printf( "\t--监听端口:%d --失败!\n", g_iPort );
- return;
- }
- printf( "\t--监听端口:%d --成功!\n", g_iPort );
- HDB_Log( " -- 监听端口:%d 成功!", g_iPort );
-
- /* get shared memory for parent and children */
- mptr = mmap( NULL, sizeof(pthread_mutex_t), PROT_READ|PROT_WRITE,
- MAP_ANON|MAP_SHARED, -1, 0 );
- if (mptr == MAP_FAILED)
- {
- printf( "error: mmap()\n" );
- exit( 0 );
- }
-
- /* initialize the mutex and lock it */
- pthread_mutexattr_init( &mattr );
- pthread_mutexattr_setpshared( &mattr, PTHREAD_PROCESS_SHARED );
- pthread_mutex_init( mptr, &mattr );
- pthread_mutexattr_destroy( &mattr );
-
- /*--------------------------------------------------------------*/
- for (i=0; i<nchildren; i++)
- {
- pids[i] = child_make( i, listenfd ); /* parent returns */
- }
-
- signal( SIGINT, sig_int );
-
- for (;;)
- {
- pause(); /* everything done by children */
- }
- }
- /********************************************************************/
- void sig_int( int signo )
- {
- int i;
-
- /* terminate all children */
- for (i=0; i<nchildren; i++)
- {
- if (kill( pids[i], SIGTERM ) != 0)
- {
- printf( "%s", strerror(errno) );
- }
- }
- while(wait(NULL)>0); /* wait for all children */
-
- if (errno != ECHILD)
- {
- printf( "wait error" );
- }
- exit( 0 );
- }
- /********************************************************************/
- pid_t child_make( int i, int listenfd )
- {
- pid_t pid;
- void child_main( int, int );
-
- if ((pid = fork()) > 0)
- {
- return( pid ); /* parent */
- }
- child_main( i, listenfd ); /* never returns */
- }
- /********************************************************************/
- void child_main( int i, int listenfd )
- {
- int connfd;
- socklen_t addrlen;
- struct sockaddr cliaddr;
- void child_deal( int sockfd );
-
- HDB_Log( " -- 子进程 [%d] 准备就绪", i );
-
- for (;;)
- {
- pthread_mutex_lock( mptr );
- if ((connfd = accept( listenfd, &cliaddr, &addrlen )) < 0)
- {
- HDB_Log( "accept error : %s", strerror(errno) );
- pthread_mutex_unlock( mptr );
- continue;
- }
- pthread_mutex_unlock( mptr );
- child_deal( connfd );
- close( connfd );
- }
- }
- /********************************************************************/
- void child_deal( int sockfd )
- {
- /*从sockfd读取客户端的请求报文,处理,然后返回应答报文给客户端。*/
- }
复制代码
[ 本帖最后由 AllenYao 于 2006-5-31 08:54 编辑 ] |
|