select例子

来源:互联网 发布:淘宝客微信转发软件 编辑:程序博客网 时间:2024/04/28 09:27

/******************************************************************************************************************
参考:1 http://blog.csdn.net/sunxx1986/article/details/6883909
说明:linux socket的select函数例)。

******************************************************************************************************************/ 


1.只做定时器

[cpp] view plaincopy
  1. #include <stdio.h>  
  2. #include <unistd.h>  
  3. #include <sys/select.h>  
  4. #include <sys/time.h>  
  5.   
  6. int main()  
  7. {  
  8.     int rc = 0;  
  9.     struct timeval tv;  
  10.     tv.tv_sec = 5;  
  11.     tv.tv_usec = 0;  
  12.     while(1)  
  13.     {  
  14.         tv.tv_sec = 5;  
  15.         tv.tv_usec = 0;  
  16.         printf("a\n");  
  17.         rc = select(0, NULL, NULL, NULL, &tv);  
  18.         printf("rc = %d\n", rc);  
  19.           
  20.         if(rc == 0)  
  21.             printf("b\n");  
  22.         else  
  23.             printf("error !\n");  
  24.     }  
  25.     return 0;  
  26. }  

这样可以省去考虑的东西很多,就最后一个&tv起作用了,其它一概不考虑,这就是一个比sleep精度稍高的定时器。
0 0