对ListenSocket 的研究(二)
磨砺技术珠矶,践行数据之道,追求卓越价值
回到上一级页面:PostgreSQL内部结构与源代码研究索引页 回到顶级页面:PostgreSQL索引页
[作者:高健@博客园 luckyjackgao@gmail.com]
在对 PostmasterMain加入代码的基础上:
/*
* Postmaster main entry point
*/
int
PostmasterMain(int argc, char *argv[])
{
……
/*added by gaojian begin*/
for (i = 0; i < MAXLISTEN; i++)
{
if (ListenSocket[i] != PGINVALID_SOCKET){
frpintf(stderr, "ListenSocket[%d] is: %d. \n", i,ListenSocket[i]);
}else{ break; }
}
/*added by gaojian end*/
status = ServerLoop();
/*
* ServerLoop probably shouldn't ever return, but if it does, close down.
*/
ExitPostmaster(status != STATUS_OK);
return 0; /* not reached */
}
然后,再在ServerLoop函数中加入一小段调试代码:
/*
* New connection pending on any of our sockets? If so, fork a child
* process to deal with it.
*/
if (selres > 0)
{
int i;
for (i = 0; i < MAXLISTEN; i++)
{
if (ListenSocket[i] == PGINVALID_SOCKET)
break;
if (FD_ISSET(ListenSocket[i], &rmask))
{
/*added by gaojian begin*/
fprintf(stderr,
"In ServerLoop, ListenSocket[%d] is dealed ok by FD_ISSET.\n", i);
/*added by gaojian end*/
Port *port;
port = ConnCreate(ListenSocket[i]);
if (port)
{
BackendStartup(port);
/*
* We no longer need the open socket or port structure
* in this process
*/
StreamClose(port->sock);
ConnFree(port);
}
}
}
}
运行结果如下,服务器端运行结果:
启动后,显示:
ListenSocket[0]:3
ListenSocekt[1]:4
启动一个终端,运行psql后,服务器端显示:
ListenScoket[0] is dealed ok with FD_ISSET
再启动一个终端,运行psql后,服务器端显示:
ListenScoket[0] is dealed ok with FD_ISSET
再启动一个终端,运行psql后,服务器端还是显示:
ListenScoket[0] is dealed ok with FD_ISSET
为何ListenSocket[1]没有被处理,尚需进一步分析。
[作者:高健@博客园 luckyjackgao@gmail.com]
磨砺技术珠矶,践行数据之道,追求卓越价值
回到上一级页面:PostgreSQL内部结构与源代码研究索引页 回到顶级页面:PostgreSQL索引页