Postmaster主循环的大致流程
生活随笔
收集整理的這篇文章主要介紹了
Postmaster主循环的大致流程
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
postmaster.c 中,主循環(huán)的大致流程如下:
/* * 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語言的標(biāo)準(zhǔn)select函數(shù) 來監(jiān)聽是否有新的連接請求進來。如果有連接請求則調(diào)用BackendStartup 函數(shù),開啟新的backend 處理連接。
這里面比較令我困惑的是:for (i = 0; i < MAXLISTEN; i++) 循環(huán),對BackendStartup 函數(shù)的調(diào)用是發(fā)生在循環(huán)內(nèi)部。ListenSocket 數(shù)組如何理解。需要進一步的研究。
總結(jié)
以上是生活随笔為你收集整理的Postmaster主循环的大致流程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网站性能工具Yslow的使用方法
- 下一篇: IoAttachDevice源码