1: #include <sys/types.h>
2: #include <sys/select.h>
3: #include <stdio.h>
4: #include <stdlib.h>
5: #include <string.h>
6:
7: #define BUFSIZE (256)
8:
9: int main(void){
10: fd_set read_fdset;
11: int maxfd=1;
12: char buff[BUFSIZE];
13: while (1) {
14: FD_ZERO(&read_fdset);
15: FD_SET(0, &read_fdset);
16: int result = select(maxfd + 1, &read_fdset, NULL, NULL, NULL);
17: if (0 > result){
18: printf("select error\n");
19: exit(1);
20: }
21:
22: if (FD_ISSET(0, &read_fdset)){
23: printf("data is ready\n");
24: int c=getc(stdin);
25: printf("%c",c);
26: }
27: }
28: return 0;
29: }
結果是?輸入test,按下兩次enter,得到的結果,如下圖如果可以一眼看出答案,表示對select()有深入了解。容我賣個關子,這裡問題出在於stdio對於buffer以及kernel buffer的認知上面
沒有留言:
張貼留言