并发式IO的解决方案---非阻塞式、多路复用和异步通知(异步IO)

---非阻塞式IO
阻塞式读取键盘和鼠标:
    
    
  1. // 读取鼠标
  2. int fd = -1;
  3. char buf[200];
  4. fd = open("/dev/input/mouse1", O_RDONLY);
  5. if (fd < 0)
  6. {
  7. perror("open:");
  8. return -1;
  9. }
  10. memset(buf, 0, sizeof(buf));
  11. printf("before 鼠标 read.\n");
  12. read(fd, buf, 50);
  13. printf("鼠标读出的内容是:[%s].\n", buf);
  14. // 读键盘
  15. memset(buf, 0, sizeof(buf));
  16. printf("before 键盘 read.\n");
  17. read(0, buf, 5);
  18. printf("键盘读出的内容是:[%s].\n", buf);
  19. return 0;

实现非阻塞IO访问:O_NONBLOCK和fcntl
    
    
  1. int fcntl(int fd, int cmd, ... /* arg */ );
F_GETFL (void)
              Get the file access mode and the file status flags; arg is ignored.
F_SETFL (int)
              Set the file status flags to  the  value  specified  by  arg.   File  access  mode
              (O_RDONLY,  O_WRONLY,  O_RDWR)  and  file  creation  flags (i.e., O_CREAT, O_EXCL,
              O_NOCTTY, O_TRUNC) in arg are ignored.  On Linux this command can change only  the
              O_APPEND, O_ASYNC, O_DIRECT, O_NOATIME, and O_NONBLOCK flags.
属性:
O_NONBLOCK //非阻塞
O_ASYNC //接收异步IO
    
    
  1. int fd = -1;
  2. int flag = -1;
  3. char buf[200];
  4. int ret = -1;
  5. fd = open("/dev/input/mouse1", O_RDONLY | O_NONBLOCK);
  6. if (fd < 0)
  7. {
  8. perror("open:");
  9. return -1;
  10. }
  11. // 把0号文件描述符(stdin)变成非阻塞式的
  12. flag = fcntl(0, F_GETFL); // 先获取原来的flag
  13. flag |= O_NONBLOCK; // 添加非阻塞属性
  14. fcntl(0, F_SETFL, flag); // 更新flag
  15. // 这3步之后,0就变成了非阻塞式的了
  16. while (1)
  17. {
  18. // 读鼠标
  19. memset(buf, 0, sizeof(buf));
  20. //printf("before 鼠标 read.\n");
  21. ret = read(fd, buf, 50);
  22. if (ret > 0)
  23. {
  24. printf("鼠标读出的内容是:[%s].\n", buf);
  25. }
  26. // 读键盘
  27. memset(buf, 0, sizeof(buf));
  28. //printf("before 键盘 read.\n");
  29. ret = read(0, buf, 5);
  30. if (ret > 0)
  31. {
  32. printf("键盘读出的内容是:[%s].\n", buf);
  33. }
  34. }

---多路复用IO
select和poll
外部阻塞式,内部非阻塞式自动轮询多路阻塞式IO
---select函数介绍
     
     
  1. int select(int nfds, fd_set *readfds, fd_set *writefds,
  2. fd_set *exceptfds, struct timeval *timeout);
  3. //nfds:多路阻塞式IO中最大的文件描述符+1
  4. //*readfds:要读的IO
  5. //*writefds:要写的IO
  6. //*exceptfds:出现错误的IO
  7. //*timeout:超时时间,在超时时间内,有IO激活函数,则立刻返回,没有IO激活,则在到达超时时间时返回
  8. //struct timeval { time_t tv_sec; /* seconds */ suseconds_t tv_usec; /* microseconds */ };
  9. //返回值:0-超时返回 >0-返回的是IO的返回路数 -1-返回错误
select的几个宏函数:
      
      
  1. void FD_CLR(int fd, fd_set *set); //将fd从set里面拿出去 clearO(∩_∩)O~
  2. int FD_ISSET(int fd, fd_set *set); //判断fd有没有被置位,返回值是正的,说明这个fd的IO发生了,负的就没有发生
  3. void FD_SET(int fd, fd_set *set); //将fd添加进set里面去
  4. void FD_ZERO(fd_set *set); //将所有的fd_set全部清除出去
代码实践:用select函数实现同时读取键盘鼠标
     
     
  1. int fd = -1, ret = -1;
  2. char buf[200];
  3. fd_set myset;
  4. struct timeval tm;
  5. fd = open("/dev/input/mouse1", O_RDONLY);
  6. if (fd < 0)
  7. {
  8. perror("open:");
  9. return -1;
  10. }
  11. // 当前有2个fd,一共是fd一个是0
  12. // 处理myset
  13. FD_ZERO(&myset);
  14. FD_SET(fd, &myset); //鼠标的
  15. FD_SET(0, &myset); //键盘的
  16. tm.tv_sec = 10;
  17. tm.tv_usec = 0;
  18. ret = select(fd+1, &myset, NULL, NULL, &tm); //如果两个都没有输入,程序就会在这阻塞住
  19. if (ret < 0)
  20. {
  21. perror("select: ");
  22. return -1;
  23. }
  24. else if (ret == 0)
  25. {
  26. printf("超时了\n");
  27. }
  28. else
  29. {
  30. // 等到了一路IO,然后去监测到底是哪个IO到了,处理之
  31. if (FD_ISSET(0, &myset))
  32. {
  33. // 这里处理键盘
  34. memset(buf, 0, sizeof(buf));
  35. read(0, buf, 5);
  36. printf("键盘读出的内容是:[%s].\n", buf);
  37. }
  38. if (FD_ISSET(fd, &myset))
  39. {
  40. // 这里处理鼠标
  41. memset(buf, 0, sizeof(buf));
  42. read(fd, buf, 50);
  43. printf("鼠标读出的内容是:[%s].\n", buf);
  44. }
  45. }
  46. return 0;


---poll函数介绍
    
    
  1. int poll(struct pollfd *fds, nfds_t nfds, int timeout);
//使用前必须先设置 struct pollfd * fds
    
    
  1. struct pollfd {
  2. int fd; /* file descriptor */
  3. short events; /* requested events */
  4. short revents; /* returned events */ //是内核设置的
  5. };
     
     
  1. events常用宏
  2.         POLLIN    There is data to read. //FD要读
  3. POLLOUT     Writing now will not block.//FD要写
如果有事件发生,要判断是哪个IO发生的,就去判断每个IO的 events和revents是否相等 ,若相等就说明此IO发生
返回值和select一样;
代码实践:用poll函数实现同时读取键盘鼠标
    
    
  1. // 读取鼠标
  2. int fd = -1, ret = -1;
  3. char buf[200];
  4. struct pollfd myfds[2] = {0};
  5. fd = open("/dev/input/mouse1", O_RDONLY);
  6. if (fd < 0)
  7. {
  8. perror("open:");
  9. return -1;
  10. }
  11. // 初始化我们的pollfd
  12. myfds[0].fd = 0; // 键盘
  13. myfds[0].events = POLLIN; // 等待读操作
  14. myfds[1].fd = fd; // 鼠标
  15. myfds[1].events = POLLIN; // 等待读操作
  16. ret = poll(myfds, fd+1, 10000);
  17. if (ret < 0)
  18. {
  19. perror("poll: ");
  20. return -1;
  21. }
  22. else if (ret == 0)
  23. {
  24. printf("超时了\n");
  25. }
  26. else
  27. {
  28. // 等到了一路IO,然后去监测到底是哪个IO到了,处理之
  29. if (myfds[0].events == myfds[0].revents)
  30. {
  31. // 这里处理键盘
  32. memset(buf, 0, sizeof(buf));
  33. read(0, buf, 5);
  34. printf("键盘读出的内容是:[%s].\n", buf);
  35. }
  36. if (myfds[1].events == myfds[1].revents)
  37. {
  38. // 这里处理鼠标
  39. memset(buf, 0, sizeof(buf));
  40. read(fd, buf, 50);
  41. printf("鼠标读出的内容是:[%s].\n", buf);
  42. }
  43. }
  44. return 0;

---异步通知(异步IO)
异步IO的工作方法是:我们当前进程注册一个异步IO事件(使用signal注册一个信号SIGIO的处理函数),然后当前进程可以正常处理自己的事情,当异步事件发生后当前进程会收到一个SIGIO信号从而执行绑定的处理函数去处理这个异步事件。
3.6.6.2、涉及的函数:
(1)fcntl(F_GETFL、F_SETFL、O_ASYNC、F_SETOWN)
(2)signal或者sigaction(SIGIO)
3.6.3.代码实践
    
    
  1. int mousefd = -1;
  2. // 绑定到SIGIO信号,在函数内处理异步通知事件
  3. void func(int sig)
  4. {
  5. char buf[200] = {0};
  6. if (sig != SIGIO)
  7. return;
  8. read(mousefd, buf, 50);
  9. printf("鼠标读出的内容是:[%s].\n", buf);
  10. }
  11. int main(void)
  12. {
  13. // 读取鼠标
  14. char buf[200];
  15. int flag = -1;
  16. mousefd = open("/dev/input/mouse1", O_RDONLY);
  17. if (mousefd < 0)
  18. {
  19. perror("open:");
  20. return -1;
  21. }
  22. // 把鼠标的文件描述符设置为可以接受异步IO
  23. flag = fcntl(mousefd, F_GETFL);
  24. flag |= O_ASYNC;        //接受异步IO
  25. fcntl(mousefd, F_SETFL, flag);
  26. // 把异步IO事件的接收进程设置为当前进程
  27. fcntl(mousefd, F_SETOWN, getpid());
  28. // 注册当前进程的SIGIO信号捕获函数
  29. signal(SIGIO, func);
  30. // 读键盘
  31. while (1)
  32. {
  33. memset(buf, 0, sizeof(buf));
  34. //printf("before 键盘 read.\n");
  35. read(0, buf, 5);
  36. printf("键盘读出的内容是:[%s].\n", buf);
  37. }
  38. return 0;
  39. }

---存储映射IO
mmap函数
LCD显示和IPC之共享内存
存储映射IO的特点
(1)共享而不是复制,减少内存操作
(2)处理大文件时效率高,小文件不划算






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值