IOCTL函数说明

当在ioctl里使用FIONREAD时,除了获得所指定的读缓存大小以外,还有清除设备准备就绪状态的作用.

当38行注释以后, 由于各个设备的状态未被清除,所以循环一直处于非阻塞的状态.不停的打印一个状态(即未清除状态)的信息.

如果不注释ioctl,那么select会自动清除未准备好的设备状态. 此时阻塞是有效地.

 

同样的,在socket当中使用select和ioctl时测试结果也是如此:

View Code
   
     
1 // 《linux程序设计》第三版--第15章套接字--select系统调用
2   #include < sys / types.h >
3 #include < sys / time.h >
4 #include < stdio.h >
5 #include < fcntl.h >
6 #include < sys / ioctl.h >
7 #include < unistd.h >
8
9   int main()
10 {
11 char buffer[ 128 ];
12 int result,nread;
13 fd_set inputs,testfds;
14 struct timeval timeout;
15
16 FD_ZERO( & inputs);
17 FD_SET( 0 , & inputs);
18
19 while ( 1 )
20 {
21 testfds = inputs;
22 timeout.tv_sec = 2 ;
23 timeout.tv_usec = 500000 ;
24
25 result = select(FD_SETSIZE, & testfds, (fd_set * )NULL, (fd_set * )NULL, & timeout);
26
27 switch (result)
28 {
29 case 0 :
30 printf( " timeout\n " );
31 break ;
32 case - 1 :
33 perror( " select " );
34 exit( 1 );
35 default :
36 if (FD_ISSET( 0 , & testfds))
37 {
38 ioctl( 0 ,FIONREAD, & nread);
39 if (nread == 0 )
40 {
41 printf( " keyboard done\n " );
42 exit( 0 );
43 }
44 nread = read( 0 ,buffer,nread);
45 buffer[nread] = 0 ;
46 printf( " read %d from keyboard: %s " ,nread,buffer);
47 }
48 break ;
49 }
50 }
51 }

参考:

关于FIONREAD参数作用:

http://linchunai1212.blog.163.com/blog/static/351121432011117113658376/

从缓冲上看阻塞与非阻塞socket在发送接收上的区别:

http://linchunai1212.blog.163.com/blog/static/35112143201111885252175/


你可能感兴趣的:(IOC)