zoukankan      html  css  js  c++  java
  • 多路转接之poll和select

    先看poll():

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 #include <sys/stat.h>
     5 #include <sys/types.h>
     6 #include <fcntl.h>
     7 #include <poll.h>
     8 
     9 #define oops(x, n) { perror(x); exit(n); }
    10 
    11 int main()
    12 {
    13     mkfifo("fifo1", 0644);    
    14     mkfifo("fifo2", 0644);    
    15     
    16     int fd1 = open("fifo1", O_RDWR);
    17     int fd2 = open("fifo2", O_RDWR);
    18     char buf[1000];
    19     for(;;)
    20     {
    21         struct pollfd pfd[] = {{0, POLLIN}, {fd1, POLLIN}, {fd2, POLLIN}};
    22         int ret = poll(pfd, 3, 1);
    23         if(ret > 0)
    24         {
    25             if(pfd[0].revents&POLLIN)
    26             {
    27                 scanf(" %[^
    ]", buf);    
    28                 printf("keypad:%s
    ", buf);
    29                 if(strcmp(buf, "quit") == 0) break;    
    30             }
    31             if(pfd[1].revents&POLLIN)
    32             {
    33                 int n = read(fd1, buf, sizeof(buf));    
    34                 buf[n] = '';
    35                 printf("fifo1:%s", buf);
    36             }
    37             if(pfd[2].revents&POLLIN)
    38             {
    39                 int n = read(fd2, buf, sizeof(buf));    
    40                 buf[n] = '';
    41                 printf("fifo2:%s", buf);
    42             }
    43         }
    44 
    45     }
    46     close(fd1);
    47     close(fd2);
    48     unlink("fifo1");
    49     unlink("fifo2");
    50 
    51     return 0;
    52 }
    View Code


    再来说说select():

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 #include <sys/stat.h>
     5 #include <sys/types.h>
     6 #include <fcntl.h>
     7 #include <poll.h>
     8 
     9 #define oops(x, n) { perror(x); exit(n); }
    10 
    11 int main()
    12 {
    13     mkfifo("fifo1", 0644);    
    14     mkfifo("fifo2", 0644);    
    15     
    16     int fd1 = open("fifo1", O_RDWR);
    17     int fd2 = open("fifo2", O_RDWR);
    18     char buf[1000];
    19     for(;;)
    20     {
    21         struct pollfd pfd[] = {{0, POLLIN}, {fd1, POLLIN}, {fd2, POLLIN}};
    22         int ret = poll(pfd, 3, 1);
    23         if(ret > 0)
    24         {
    25             if(pfd[0].revents&POLLIN)
    26             {
    27                 scanf(" %[^
    ]", buf);    
    28                 printf("keypad:%s
    ", buf);
    29                 if(strcmp(buf, "quit") == 0) break;    
    30             }
    31             if(pfd[1].revents&POLLIN)
    32             {
    33                 int n = read(fd1, buf, sizeof(buf));    
    34                 buf[n] = '';
    35                 printf("fifo1:%s", buf);
    36             }
    37             if(pfd[2].revents&POLLIN)
    38             {
    39                 int n = read(fd2, buf, sizeof(buf));    
    40                 buf[n] = '';
    41                 printf("fifo2:%s", buf);
    42             }
    43         }
    44 
    45     }
    46     close(fd1);
    47     close(fd2);
    48     unlink("fifo1");
    49     unlink("fifo2");
    50 
    51     return 0;
    52 }
    View Code
  • 相关阅读:
    linux 定时任务
    fastcgi_param 详解
    MVC 中 注册不成功 或其他操作不成功 提示办法
    关于MVC 中EF调用存储过程
    关于Mvc的分页写法
    关于Linq翻译Inner join ,Left join (本文为转载)
    GridView自定义自增长的 序号 列
    asp.net操作GridView添删改查的两种方法 及 光棒效果
    Jquery编历数组
    在客户与服务器之间传递二进制结构
  • 原文地址:https://www.cnblogs.com/takeaction/p/4376318.html
Copyright © 2011-2022 走看看