关于异步I/O的问题
小弟在看《unix/linux编程实践教程》这本书,第7章的习题15关于异步I/O的题我有些不懂,题目的大意是:在终端上有个"hello"字符串,用curses库,信号,异步I/O来使得该字符串自动左右移动。下面是代码:
- C/C++ code
#include <stdio.h>#include <curses.h>#include <signal.h>#include <fcntl.h>#include <string.h>/* some global settings main and the handler use */#define MESSAGE "hello"#define BLANK " "int row; /* current row */int col; /* current column */int dir; /* where we are going */int delay; /* bigger => slower */int done;void on_input(int); /* handler for keybd */void enable_kbd_signals();int main(){ initscr(); crmode(); noecho(); clear(); signal(SIGIO,on_input); /* install a handler */ //enable_kbd_signals(); /* turn on kbd signals */ //为什么注释掉这一句后,字符串就可以自动移动??为什么有这句的时候,必须要按"上下左右"键才可以使得字符串进行移动? row = 10; /* start here */ col = 0; dir = 1; /* add 1 to col number */ delay = 200; /* 200ms = 0.2 seconds */ done = 0; while(!done) { usleep(delay); move_msg(); } endwin(); return 0;}/** called after each sleep period*/move_msg(){ move( row, col ); addstr( BLANK ); col += dir; /* move to new column */ move( row, col ); /* then set cursor */ addstr( MESSAGE ); /* redo message */ move(0,COLS-1); refresh(); /* and show it */ /* * now handle borders */ if ( dir == -1 && col <= 0 ) dir = 1; else if ( dir == 1 && col+strlen(MESSAGE) >= COLS ) dir = -1;}/** called when a keystroke appears*/void on_input(int signum){ int c = getch(); /* grab the char */ switch ( c ){ case 'Q': case EOF: done = 1; break; case ' ': dir = -dir; break; case 'f': if ( delay > 2 ) delay >>= 1; break; case 's': delay <<= 1; break; }}/** install a handler, tell kernel who to notify on input, enable signals*/void enable_kbd_signals(){ int fd_flags; fcntl(0, F_SETOWN, getpid()); fd_flags = fcntl(0, F_GETFL); fcntl(0, F_SETFL, (fd_flags|O_ASYNC));}代码中有个问题,我完全不能理解,如果您知道原因,请告诉小弟我,非常感谢。
是不是我理解的异步I/O原理有问题?我是这样理解的:异步I/O实现了非阻塞的处理方式,可以同时处理多个信号。如果我的理解有问题,请您告诉我,非常感谢。
[解决办法]
异步和非阻塞是两个不同的概念。具体有异步非阻塞,同步非阻塞,异步阻塞,异步非阻塞四种形式。而上面代码只是实现的是非阻塞IO和通过信号实现异步操作。