read什么时候阻塞?阻塞有什么表现?

本文探讨了如何改进在C语言中通过stdin读取数据的方式,包括使用非阻塞模式和设置文件描述符属性。详细解释了如何通过fcntl函数调整stdin的阻塞行为,并展示了在不同设置下read函数的行为变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

read(stdin, buf, size);

printf(“hello”);

如果read是阻塞的则会停止到read语句不会执行下一个语句;否则不会停止,会执行下一个语句。

#include    <stdio.h>
#include    <stdlib.h>
 
 #define     COUNT  10
 int
 main()
 {
    char    buf[COUNT];
    size_t  nbytes;
     int     n;
  
     for (n = 0; n< COUNT; n++)
         buf[n] = 0;
  
     nbytes = 10;
     read(stdin, buf, nbytes);
  
     printf("hello\n");
     printf("%s\n", buf);
 }


这个标准输入,我想应该是会阻塞的,结果是没有阻塞:


没有停止等待输入就直接输出了。这是有问题的,stdin是指针类型而read要求用int型的参数。

最后,改成:

#include    <stdio.h>
#include    <unistd.h>
#include    <stdlib.h>
#include    <fcntl.h>
 
#define     COUNT  10
int
main()
{
    char    buf[COUNT];
    size_t  nbytes;
    int     n, flags;


    for (n = 0; n< COUNT; n++)
        buf[n] = 0;


    if ( (flags = fcntl(STDIN_FILENO, F_GETFL, 0)) < 0)
    {
        printf("F_GETFL error");
        exit(0);
    }
    
    printf("flags:%d\n", flags);


    nbytes = 10;
    read(STDIN_FILENO, buf, nbytes); 
    printf("hello\n");
    printf("%s\n", buf);
}


结果:

设置nonblock选项后:

#include    <stdio.h>
#include    <unistd.h>
#include    <stdlib.h>
#include    <fcntl.h>
 
#define     COUNT  10
int
main()
{
    char    buf[COUNT];
    size_t  nbytes;
    int     n, flags;
    int     fd;


    fd = 0;


    for (n = 0; n< COUNT; n++)
        buf[n] = 0;


    if ( (flags = fcntl(STDIN_FILENO, F_GETFL, 0)) < 0)
    {
        printf("F_GETFL error");
        exit(0);
    }
    
    printf("flags old:%d\n", flags);


    flags |= O_NONBLOCK;
    if (fcntl(STDIN_FILENO, F_SETFL, flags) < 0)
    {
        printf("F_SETFL error");
        exit(0);
    }
    
    printf("flags new:%d\n", flags);




    nbytes = 10;
    read(STDIN_FILENO, buf, nbytes); 
    printf("hello\n");
    printf("%s\n", buf);
}


结果:



结果就像我想的那样。read阻塞的话会停止不进行,而非阻塞的话将不等待从stdin读取数据这个动作。

在Linux系统中,I2C(Inter-Integrated Circuit)通信是一种常用的串行通信协议,用于连接微控制器和外围设备。I2C通信的读取操作可以是阻塞的,也可以是非阻塞的,具体取决于使用的API和配置。 1. **阻塞读取**:默认情况下,I2C读取操作是阻塞的。这意味着当调用读取函数时,程序会一直等待,直到数据被成功读取或发生错误。如果设备没有及时响应,程序会一直阻塞在那里,直到超时或数据到达。 2. **非阻塞读取**:通过设置文件描述符为非阻塞模式,可以实现非阻塞读取。这样,读取操作会立即返回,如果数据没有准备好,函数会返回一个错误码,程序可以继续执行其他任务。 例如,使用标准的I2C用户空间接口(`/dev/i2c-*`),可以通过`ioctl`调用设置文件描述符为非阻塞模式: ```c #include <fcntl.h> #include <unistd.h> #include <sys/ioctl.h> #include <linux/i2c-dev.h> int fd = open("/dev/i2c-1", O_RDWR); if (fd < 0) { // 处理错误 } if (ioctl(fd, I2C_SLAVE, device_address) < 0) { // 处理错误 } // 设置为非阻塞模式 int flags = fcntl(fd, F_GETFL, 0); fcntl(fd, F_SETFL, flags | O_NONBLOCK); unsigned char buffer[10]; int ret = read(fd, buffer, sizeof(buffer)); if (ret < 0) { if (errno == EAGAIN) { // 数据未准备好,进行其他处理 } else { // 处理其他错误 } } else { // 处理读取到的数据 } ``` 在上述代码中,通过`fcntl`函数将文件描述符设置为非阻塞模式,然后使用`read`函数进行读取。如果数据未准备好,`read`函数会返回`-1`,并且`errno`会被设置为`EAGAIN`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值