在对 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]没有被处理,尚需进一步分析。
本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/archive/2012/07/20/2600780.html,如需转载请自行联系原作者