计算机网络之poll

来源:互联网 发布:大学生滚床单知乎 编辑:程序博客网 时间:2024/06/05 04:35

poll使用了一个pollfd的指针来实现,pollfd结构包含了要监视的event和要发生的event,且pollffd并没有最大数量限制。poll返回后,需要轮询pollfd来获取就绪的描述符。

代码

 1 #include<stdio.h>
  2 #include<poll.h>
  3 #include<string.h>
  4 
  5 int newpoll()
  6 {
  7   struct pollfd poll_set[1];
  8   poll_set[0].fd=0;
  9   poll_set[0].events = POLLIN;
 10   poll_set[0].revents = 0;
 11   int timeout = 30000;
 12   char buf[1024];
 13   while(1)
 14 {
 15    switch(poll(poll_set,1,timeout))
 16   {
 17    case -1:
 18          printf("poll error!\n");
 19          break;
 20  case 0:
 21      printf("poll time out!\n");
 22       break;
 23  default:
 24      {
 25         memset(buf,'\0',sizeof(buf));
 26         printf("poll return\n ");
 27         fgets(buf,sizeof(buf)-1,stdin);
 28        printf("msg is:%s\n",buf);
 29 }
 30 break;
 31 

 31 
 32 }
 33 }
 34 return 0;
 35 }
 36 int main()
 37 {
 38   newpoll();
 39   return 0;
 40 }

运行解果:

第一次的时候我们把时间设置为300


看到如上解果,下面我们把时间稍微设置的长一点