示例#1
0
void createWorkerProcess( aeServer* serv )
{
    char* neterr;
    int ret,i;
    for(  i = 0; i < serv->workerNum; i++ )
    {
		//3缓冲区
		serv->workers[i].send_buffer = sdsempty();
			
		//init mutex
		pthread_mutex_init( &(serv->workers[i].r_mutex) ,NULL);
		pthread_mutex_init( &(serv->workers[i].w_mutex) ,NULL);
			
		ret = socketpair( PF_UNIX, SOCK_STREAM, 0, serv->workers[i].pipefd );
		assert( ret != -1 );

        serv->workers[i].pid = fork();
        if( serv->workers[i].pid < 0 )
        {
            continue;
        }
        else if( serv->workers[i].pid > 0 )
        {
            //父进程
            close( serv->workers[i].pipefd[1] );
            anetNonBlock( neterr , serv->workers[i].pipefd[0] );
            continue;
        }
        else
        {
            //子进程
            close( serv->workers[i].pipefd[0] );
            anetNonBlock( neterr, serv->workers[i].pipefd[1] );
            runWorkerProcess( i , serv->workers[i].pipefd[1]  );
            exit( 0 );
        }
    }
}
示例#2
0
void createWorkerProcess( appnetServer *serv )
{
	char *neterr;
	int ret,i,j,index;
	int total_worker = serv->worker_num + serv->task_worker_num;
	
	for (i = 0; i < total_worker; i++)
	{
		serv->workers[i].send_buffer = sdsempty();
		// init mutex
		pthread_mutex_init( &( serv->workers[i].r_mutex ) , NULL );
		pthread_mutex_init( &( serv->workers[i].w_mutex ) , NULL );
		
		for (j = 0; j < serv->reactor_num; j++)
		{
			
			index = i * serv->reactor_num + j;
			ret =
					socketpair( PF_UNIX , SOCK_STREAM , 0 , serv->worker_pipes[index].pipefd );
			assert( ret != -1 );
			printf( "woker_id=%d,pipe_index=%d,pipe0=%d,pipe1=%d \n" , i , index ,
					serv->worker_pipes[index].pipefd[0] ,
					serv->worker_pipes[index].pipefd[1] );
		}
	}
	
	for (i = 0; i < total_worker; i++)
	{
		serv->workers[i].pid = fork();
		if (serv->workers[i].pid < 0)
		{
			continue;
		}
		else if (serv->workers[i].pid > 0)
		{
			
			for (j = 0; j < serv->reactor_num; j++)
			{
				index = i * serv->reactor_num + j;
				close( serv->worker_pipes[index].pipefd[1] );
				
				anetSetSendBuffer( neterr , serv->worker_pipes[index].pipefd[0] , 
				SOCKET_SND_BUF_SIZE );
				
				anetNonBlock( neterr , serv->worker_pipes[index].pipefd[0] );
			}
			continue;
		}
		else
		{
			
			for (j = 0; j < serv->reactor_num; j++)
			{
				index = i * serv->reactor_num + j;
				close( serv->worker_pipes[index].pipefd[0] );
				anetNonBlock( neterr , serv->worker_pipes[index].pipefd[1] );
				anetSetSendBuffer( neterr , serv->worker_pipes[index].pipefd[1] , 
				SOCKET_SND_BUF_SIZE );
			}
			runWorkerProcess( i );
			exit( 0 );
		}
	}
}