关于异步I/O的问题
小弟在看《unix/linux编程实践教程》这本书,第7章的习题15关于异步I/O的题我有些不懂,题目的大意是:在终端上有个"hello"字符串,用curses库,信号,异步I/O来使得该字符串自动左右移动。下面是代码:
#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));}