当前位置:   article > 正文

linux下的阻塞和非阻塞_linux read非阻塞接受两条报文,合并在一起了

linux read非阻塞接受两条报文,合并在一起了
        读常规文件是不会阻塞的,不管读多少字节,read 一定会在有限的时间内返回。从终
端设备或网络读则不一定,如果从终端输入的数据没有换行符,调用 read 读终端设备就会
阻塞,如果网络上没有接收到数据包,调用 read 从网络读就会阻塞,至于会阻塞多长时间
也是不确定的,如果一直没有数据到达就一直阻塞在那里。同样,写常规文件是不会阻塞的,
而向终端设备或网络写则不一定。
        现在明确一下阻塞(Block)这个概念。当进程调用一个阻塞的系统函数时,该进程被
置于睡眠(Sleep)状态,这时内核调度其它进程运行,直到该进程等待的事件发生了(比
如网络上接收到数据包,或者调用 sleep 指定的睡眠时间到了)它才有可能继续运行。与睡
眠状态相对的是运行(Running)状态,在 Linux 内核中,处于运行状态的进程分为两种情况:
        正在被调度执行。CPU 处于该进程的上下文环境中,程序计数器(eip)里保存着该进
程的指令地址,通用寄存器里保存着该进程运算过程的中间结果,正在执行该进程的指令,
正在读写该进程的地址空间。
        就绪状态。该进程不需要等待什么事件发生,随时都可以执行,但 CPU 暂时还在执行
另一个进程,所以该进程在一个就绪队列中等待被内核调度。系统中可能同时有多个就绪的
进程,那么该调度谁执行呢?内核的调度算法是基于优先级和时间片的,而且会根据每个进
程的运行情况动态调整它的优先级和时间片,让每个进程都能比较公平地得到机会执行,同
时要兼顾用户体验,不能让和用户交互的进程响应太慢。

阻塞、非阻塞:  是设备文件、网络文件的属性。(文件)

 阻塞读终端:默认情况下设备文件和网络文件是阻塞的。  

  1. #include <unistd.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. int main(void)
  5. {
  6. char buf[10];
  7. int n;
  8. n = read(STDIN_FILENO, buf, 10); // #define STDIN_FILENO 0 STDOUT_FILENO 1 STDERR_FILENO 2
  9. if(n < 0){
  10. perror("read STDIN_FILENO");
  11. exit(1);
  12. }
  13. write(STDOUT_FILENO, buf, n);
  14. return 0;
  15. }

 非阻塞读终端 :一直读取,终端一直占用。

        当读取设备文件(网络文件)的时候,返回值是-1: 并且 errno = EAGIN 或 EWOULDBLOCK, 说明不是read失败,而是read在以非阻塞方式读一个设备文件(网络文件),并且文件无数据。

  1. #include <unistd.h>
  2. #include <fcntl.h>
  3. #include <errno.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. int main(void)
  8. {
  9. char buf[10];
  10. int fd, n;
  11. fd = open("/dev/tty", O_RDONLY|O_NONBLOCK);
  12. if (fd < 0) {
  13. perror("open /dev/tty");
  14. exit(1);
  15. }
  16. tryagain:
  17. n = read(fd, buf, 10);
  18. if (n < 0) {
  19. if (errno != EAGAIN) { // if(errno != EWOULDBLOCK)
  20. perror("read /dev/tty");
  21. exit(1);
  22. } else {
  23. write(STDOUT_FILENO, "try again\n", strlen("try again\n"));
  24. sleep(2);
  25. goto tryagain;
  26. }
  27. }
  28. write(STDOUT_FILENO, buf, n);
  29. close(fd);
  30. return 0;
  31. }

非阻塞读终端和等待超时:设置一个超时。

  1. #include <unistd.h>
  2. #include <fcntl.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <errno.h>
  6. #include <string.h>
  7. #define MSG_TRY "try again\n"
  8. #define MSG_TIMEOUT "time out\n"
  9. int main(void)
  10. {
  11. char buf[10];
  12. int fd, n, i;
  13. fd = open("/dev/tty", O_RDONLY|O_NONBLOCK);
  14. if(fd < 0){
  15. perror("open /dev/tty");
  16. exit(1);
  17. }
  18. printf("open /dev/tty ok... %d\n", fd);
  19. for (i = 0; i < 5; i++){
  20. n = read(fd, buf, 10);
  21. if (n > 0) { //说明读到了东西
  22. break;
  23. }
  24. if (errno != EAGAIN) { //EWOULDBLOCK
  25. perror("read /dev/tty");
  26. exit(1);
  27. } else {
  28. write(STDOUT_FILENO, MSG_TRY, strlen(MSG_TRY));
  29. sleep(2);
  30. }
  31. }
  32. if (i == 5) {
  33. write(STDOUT_FILENO, MSG_TIMEOUT, strlen(MSG_TIMEOUT));
  34. } else {
  35. write(STDOUT_FILENO, buf, n);
  36. }
  37. close(fd);
  38. return 0;
  39. }
注意,阻塞与非阻塞是对于文件而言的。而不是 read、write 等的属性。read 终端,默认阻
塞读。

产生阻塞的场景。 读设备文件。读网络文件。(读常规文件无阻塞概念。)

/dev/tty -- 终端文件。

总结 read 函数返回值:
1. 返回非零值: 实际 read 到的字节数
2. 返回-1: 1):
errno != EAGAIN (或!= EWOULDBLOCK) read 出错
2):
errno == EAGAIN (或== EWOULDBLOCK) 设置了非阻塞读,并且没有
数据到达。
3. 返回 0:读到文件末尾

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/343369
推荐阅读
相关标签
  

闽ICP备14008679号