博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
对ListenSocket 的研究(四)
阅读量:6982 次
发布时间:2019-06-27

本文共 1678 字,大约阅读时间需要 5 分钟。

对postmaster.c 中的 readmask,rmask,nsocket等进行分析,可以看到:它们之间有如下的关系(与细节无关的代码省略):
复制代码
/*                                
 * Initialise the masks for select() for the ports we are listening on. 
 * Return the number of sockets to listen on.                                
 */                                
static int                                
initMasks(fd_set *rmask)                                
{                                
    int            maxsock = -1;                
    int            i;          
    FD_ZERO(rmask);           
    for (i = 0; i < MAXLISTEN; i++)                            
    {                            
        int fd = ListenSocket[i];
        if (fd == PGINVALID_SOCKET)                        
            break; 
        FD_SET(fd, rmask);     
        if (fd > maxsock)                        
            maxsock = fd;                    
    }                        
    return maxsock + 1;                            
}
复制代码
复制代码
static int                                
ServerLoop(void)                                                    
{                                        
    ......                             
    nSockets = initMasks(&readmask);    
    for (;;)                                    
    {                                    
        fd_set        rmask;                        
        int            selres;                    
                                        
        /*                                
         * Wait for a connection request to arrive.  
         * We wait at most one minute, to ensure that the other background 
         * tasks handled below get done even when no requests are arriving. 
         * If we are in PM_WAIT_DEAD_END state, then we don't want to accept 
         * any new connections, so we don't call select() at all; just sleep  
         * for a little bit with signals unblocked.                                
         */                                
        memcpy((char *) &rmask, (char *) &readmask, sizeof(fd_set));   
                                        
        PG_SETMASK(&UnBlockSig);       
        if (pmState == PM_WAIT_DEAD_END)                                
        {                                
            ...                            
        }                                
        else                                
        {                                
            ...                         
            selres = select(nSockets, &rmask, NULL, NULL, &timeout); 
        }                                
                                     
        /*                                
         * Block all signals until we wait again.  (This makes it safe for our  
         * signal handlers to do nontrivial work.)                                
         */                                
        PG_SETMASK(&BlockSig);                                
                                        
        /* Now check the select() result */                                
        if (selres < 0)                                
        {                                
            ...                            
        }                                
                                        
        /*                                
         * New connection pending on any of our sockets? If so, fork a child 
         * process to deal with it.                                
         */                                
        if (selres > 0)                                
        {                                
            ...                            
        }                             
        ...                                
    }                                    
}
复制代码
可以看出来,nsocket就是用于监听网络通信的地 fd_set中的文件描述符最大值+1。
至于原始的文件描述符,就是来自于 ListenSocket数组。
为了进一步研究,还需要从源头上看ListenSocket是如何被赋值的。
 
本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/archive/2012/07/20/2601081.html,如需转载请自行联系原作者
你可能感兴趣的文章
思科4506E交换机系统升级那点事!
查看>>
linux-mysql
查看>>
如何在Exchange Server 2003中重置提供OWA、EAS和OMA服务所需的默认虚拟目录
查看>>
GIT分布式版本控制系统使用教程
查看>>
1、Nginx安装和配置文件
查看>>
Centos网络管理(二)-IP与子网掩码计算
查看>>
网媒亟待建立广告价值衡量体系
查看>>
mysql-5.7.16一键安装/配置优化
查看>>
SSL握手中的个别细节
查看>>
从Linux 2.6.8内核的一个TSO/NAT bug引出的网络问题排查观点(附一个skb的优化点)
查看>>
SpringBoot 获取当前登录用户IP
查看>>
sed用法
查看>>
Windows Phone 应用发布技巧汇总
查看>>
centos6.0下安装FTP客户端命令
查看>>
【No.7 C++对象的构造与析构时间】
查看>>
Zabbix如何监控Windows机器
查看>>
SAN存储方式之falconstor实施方案(2)
查看>>
Bokeh快速入门(1)
查看>>
第一天salt stack 笔记
查看>>
读取本机的Java运行环境和相关配置文件的内容
查看>>