2010年2月2日 星期二

Nonblock I/O 處理技巧 in C

可以利用Signal來通知主程序I/O有資料進來,這樣可以解決主程序需要花時間polling來檢查I/O是否有資料

Signal使用方式:
1.Fill the address of the desired handler
指定Signal發生時的處理函式
2.Specify signals to be blocked during handler
當進入handler時,指定哪些signal我們要block住,避免影響handler
3.Specify how to handle the SIGIO signal
將以上的設定,跟系統註冊我們將要攔下哪一個signal
4.Arrange for SIGIO to go to this process
指定哪一個I/O發生SIGIO的信號時,是我們關心的
5.Set flags for nonblocking and asynchonous
將socket設定成nonblocking因為我們不想handler在處理的時候因為recvfrom() block住