標籤:linux aio
1:傳統的輸入訊號
傳統的輸入都是通過阻塞來實現,例如getchar一直等待使用者輸入。又或者是再curses庫中的getch都是通過阻塞的方式來等待使用者輸入。那麼想象一個情境要設計一個遊戲,這個遊戲可以讓玩家動態輸入一些值來動態調整遊戲參數。不可能通過getchar這樣的阻塞函數來擷取使用者輸入把。那麼這個該如何?呢,再想象一下另外一種情境作業系統的CPU不可能是一直等待網卡的輸入把。所以對於一些特別的情境阻塞輸入是無法滿足要求的。下面的這個例子就是一個阻塞輸入的例子。
#include<stdio.h>#include<stdlib.h>#include<curses.h>void init_setup ( );void init_end ( );void on_input ( );void do_main ( );int main ( int argc, char *argv[] ){ init_setup(); on_input(); do_main(); init_end(); return EXIT_SUCCESS;} /* ---------- end of function main ---------- */void init_setup ( ){ initscr(); crmode(); noecho(); clear();} /* ----- end of function init_setup ----- */void init_end ( ){ endwin();} /* ----- end of function init_end ----- */void on_input ( ){ char c; while((c = getch()) != ‘q‘){ if(c == ‘w‘) mvaddch(20,20,‘!‘); else if(c == ‘e‘) mvaddch(20,20,‘0‘); else if(c == ‘r‘) mvaddch(20,20,‘t‘); }} /* ----- end of function on_input ----- */void do_main ( ){ while(1){ move(50,50); addstr("do other thing"); }} /* ----- end of function do_main ----- */
從這個例子可以發現do_main沒有執行,因為on_input一直等待使用者輸入阻塞了下面的程式運行。所以在有些情境像getchar或者getch這類的阻塞函數無法滿足一些需求,那麼就需要使用非同步IO。非同步IO的實現有兩種方法:
1.設定輸入O_ASYNC位
2.使用aio_read()
2:非同步輸入一
步驟如下:
1.設定0描述符,當輸入就緒的時候發送訊號
2.設定0描述符O_ASYNC標誌位
2.設定訊號處理函數。
下面是一個例子:
#include<stdio.h>#include<stdlib.h>#include<curses.h>#include<signal.h>#include<fcntl.h>void init_setup ( );void init_end ( );void on_input ( );void do_main ( );void enable_kdb_signals();int main ( int argc, char *argv[] ){ init_setup(); enable_kdb_signals(); signal(SIGIO,on_input); do_main(); init_end(); return EXIT_SUCCESS;} /* ---------- end of function main ---------- */void init_setup ( ){ initscr(); crmode(); noecho(); clear();} /* ----- end of function init_setup ----- */void init_end ( ){ endwin();} /* ----- end of function init_end ----- */void on_input ( ){ char c; c = getch(); if(c == ‘w‘) mvaddch(20,20,‘!‘); else if(c == ‘e‘) mvaddch(20,20,‘0‘); else if(c == ‘r‘) mvaddch(20,20,‘t‘);} /* ----- end of function on_input ----- */void do_main ( ){ while(1){ sleep(1); move(50,50); addstr("do other thing"); refresh(); }} /* ----- end of function do_main ----- *///設定輸入時發送訊號,設定輸入為O_ASYNCvoid enable_kdb_signals(){ int fd_flags; fcntl(0,F_SETOWN,getpid()); fd_flags = fcntl(0,F_GETFL); fcntl(0,F_SETFL,(fd_flags|O_ASYNC));}
上面這個例子可以看到do other thing以及輸出,do_main正在運行,但是此時你輸入字元程式可以通過訊號處理函數on_input來接受輸入進行響應。達到了非同步效果。
3:非同步輸入二
非同步輸入的第二種方法是通過aio_read來實現的,使用aio_read更加靈活,但是設定起來也比較複雜。
設定步驟如下:
1.設定訊號處理函數,接受使用者輸入
2.設定aiocb結構體中的變數指明等待什麼類型的輸入,當輸入的時候產生什麼訊號。
3.將aiocb結構體傳遞給aio_read來遞交讀入請求。
aiocb結構體定義如下:
struct aiocb { /* The order of these fields is implementation-dependent */ int aio_fildes; /* File descriptor */ off_t aio_offset; /* File offset */ volatile void *aio_buf; /* Location of buffer */ size_t aio_nbytes; /* Length of transfer */ int aio_reqprio; /* Request priority */ struct sigevent aio_sigevent; /* Notification method */ int aio_lio_opcode; /* Operation to be performed; lio_listio() only */ /* Various implementation-internal fields not shown */ }; struct sigevent { int sigev_notify; /* Notification method */ int sigev_signo; /* Notification signal */ union sigval sigev_value; /* Data passed with notification */ void (*sigev_notify_function) (union sigval); /* Function used for thread notification (SIGEV_THREAD) */ void *sigev_notify_attributes; /* Attributes for notification thread (SIGEV_THREAD) */ pid_t sigev_notify_thread_id; /* ID of thread to signal (SIGEV_THREAD_ID) */ };
下面是一個簡單的例子:
#include<stdio.h>#include<stdlib.h>#include<curses.h>#include<signal.h>#include<fcntl.h>#include<aio.h>void init_setup ( );void init_end ( );void on_input ( );void do_main ( );void setup_aio_buffer();struct aiocb kbcbuf;int main ( int argc, char *argv[] ){ init_setup(); signal(SIGIO,on_input); setup_aio_buffer(); aio_read(&kbcbuf); do_main(); init_end(); return EXIT_SUCCESS;} /* ---------- end of function main ---------- */void init_setup ( ){ initscr(); crmode(); noecho(); clear();} /* ----- end of function init_setup ----- */void init_end ( ){ endwin();} /* ----- end of function init_end ----- */void on_input ( ){ char c; char *cp = (char *)kbcbuf.aio_buf; if(aio_error(&kbcbuf) != 0) perror("reading faild"); else if(aio_return(&kbcbuf) ==1){ c = *cp; if(c == ‘w‘) mvaddch(20,20,‘!‘); else if(c == ‘e‘) mvaddch(20,20,‘0‘); else if(c == ‘r‘) mvaddch(20,20,‘t‘); } aio_read(&kbcbuf);} /* ----- end of function on_input ----- */void do_main ( ){ while(1){ sleep(1); move(50,50); addstr("do other thing"); refresh(); }} /* ----- end of function do_main ----- */void setup_aio_buffer(){ static char input[1]; kbcbuf.aio_fildes = 0; #設定接受輸入的buf kbcbuf.aio_buf = input; #設定接受輸入的位元組大小 kbcbuf.aio_nbytes = 1; kbcbuf.aio_offset = 0; #設定處理輸入的方法 #SIGE_SIGNAL 代表通過發送訊號來處理 kbcbuf.aio_sigevent.sigev_notify =SIGEV_SIGNAL; #設定要發送的訊號 kbcbuf.aio_sigevent.sigev_signo = SIGIO;}
本文出自 “專註linux” 部落格,請務必保留此出處http://forlinux.blog.51cto.com/8001278/1533448