Postmaster主循环的大致流程
生活随笔
收集整理的這篇文章主要介紹了
Postmaster主循环的大致流程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
postmaster.c 中,主循環的大致流程如下:
/* * Main idle loop of postmaster */ static int ServerLoop(void) { ...... nSockets = initMasks(&readmask); for (;;) { ... if (pmState == PM_WAIT_DEAD_END) {...
} else { ... selres = select(nSockets, &rmask, NULL, NULL, &timeout); } ... /* Now check the select() result */ if (selres < 0) { if (errno != EINTR && errno != EWOULDBLOCK) { ...... return STATUS_ERROR; } } /* * 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)) { Port *port; port = ConnCreate(ListenSocket[i]); if (port) { BackendStartup(port); /*To fork a new backend */ StreamClose(port->sock); ConnFree(port); } } } } ...... } }
/* * Main idle loop of postmaster */ static int ServerLoop(void) { ...... nSockets = initMasks(&readmask); for (;;) { ... if (pmState == PM_WAIT_DEAD_END) {...
} else { ... selres = select(nSockets, &rmask, NULL, NULL, &timeout); } ... /* Now check the select() result */ if (selres < 0) { if (errno != EINTR && errno != EWOULDBLOCK) { ...... return STATUS_ERROR; } } /* * 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)) { Port *port; port = ConnCreate(ListenSocket[i]); if (port) { BackendStartup(port); /*To fork a new backend */ StreamClose(port->sock); ConnFree(port); } } } } ...... } }
從上面可以看出,基本上是以 C語言的標準select函數 來監聽是否有新的連接請求進來。如果有連接請求則調用BackendStartup 函數,開啟新的backend 處理連接。
這里面比較令我困惑的是:for (i = 0; i < MAXLISTEN; i++) 循環,對BackendStartup 函數的調用是發生在循環內部。ListenSocket 數組如何理解。需要進一步的研究。
總結
以上是生活随笔為你收集整理的Postmaster主循环的大致流程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网站性能工具Yslow的使用方法
- 下一篇: IoAttachDevice源码