bind源碼解析(二)
do {
result = isc_app_run();
if (result == ISC_R_RELOAD) {
ns_server_reloadwanted(ns_g_server);
} else if (result != ISC_R_SUCCESS) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_app_run(): %s",
isc_result_totext(result));
/*
* Force exit.
*/
result = ISC_R_SUCCESS;
}
} while (result != ISC_R_SUCCESS);
這個do while循環,主要是result = isc_app_run();這個函數實際上等待各種結束程序信號,如果需要重啟服務,就調用ns_server_reloadwanted。
在setup函數里中的create_managers(void)里,有兩個函數要注意
isc_taskmgr_create、isc_socketmgr_create2這兩個函數分別起兩個線程,分別是run和watch。
下面是watch中的關鍵代碼就是
cc = epoll_wait(manager->epoll_fd, manager->events,
done = process_fds(manager, manager->events, cc)這兩句代碼一看就知道是處理epool的。它是在isc_socketmgr_create2中的
if (isc_thread_create(watcher, manager, &manager->watcher) !=ISC_R_SUCCESS)中起的線程;process_fds執行到最后就是isc_task_send(ev->ev_sender, (isc_event_t **)&iev);這個函數執行完就會轉到下面的run線程函數中。process_fd是具體的業務處理,process_ctlfd這個函數雖然不起眼但是很重要,在這個函數中有 wakeup_socket,里面最重要的就是result = watch_fd(manager, fd, msg);,在watch_fd中,關鍵代碼就是epoll_ctl(manager->epoll_fd, EPOLL_CTL_ADD, fd, &event),這里重新注冊時間,然后epool_wait再等待,而后再處理,如此反復。
result = isc_taskmgr_create(ns_g_mctx, ns_g_cpus, 0, &ns_g_taskmgr),#define isc_taskmgr_create isc__taskmgr_create,所以這里實際調用的是isc__taskmgr_create,這個函數里if (isc_thread_create(run, manager,&manager->threads[manager->workers])這里啟動run線程函數,run主要執行的代碼就是dispatch(manager),dispath主要執行的是(event->ev_action)( (isc_task_t *)task,event);ev->ev_action其實就是個函數指針;從這里可以看出,bind的處理模式和windows的消息處理機制很相似。
bind的epoll采用的是ET模式,邊沿觸發;只對新到的數據進行通知,而內核緩沖區中如果是舊數據則不進行通知,每次epoll_wait后,處理完畢后再調用epoll_ctl;這里實際是個循環處理過程,epoll_wait等待,然后加入到events數組中,然后處理,在調用cpoll_ctrl重新設置事件類型,再等待,如此循環。在bind中,sokcet.c中的線程函數watcher中有epooll_wait函數,在啟動這個線程函數前在setup_watcher(isc_mem_t *mctx, isc__socketmgr_t *manager) 這個函數中已經調用了epoll_create。
posted on 2011-03-10 21:10 Benjamin 閱讀(3209) 評論(2) 編輯 收藏 引用 所屬分類: linux