如何阻止读系统调用 [英] How to block the read system call

查看:127
本文介绍了如何阻止读系统调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何阅读()系统模块。我已经创建了一个空文件,并试图通过read()系统调用来读。它返回0。

I don't understand how read() system blocks. I have created an empty file and trying to read using the read() system call. It returns 0.

fd = open("Demo.txt",O_RDONLY);
n = read(fd,&ch,10); // returns 0 

我期待阅读()无限期阻塞。这是因为文件中没有数据。是否阅读()认为EOF作为一个有效的数据并立即返回?是我的理解是否正确?

I am expecting read() to block indefinitely as there is no data in the file. Does read() consider EOF as a valid data and return immediately ? Is my understanding correct ?

推荐答案

是的,EOF会造成阅读()立即返回,不阻塞。当你达到EOF阅读()不会等待更多的数据被写入到文件;它立即返回0字节。从通常不易察觉的延迟,从磁盘上的文件读取时,除了在磁盘上的数据读入内存堵不发挥作用。

Yes, EOF will cause read() to return immediately, not block. When you reach EOF read() doesn't wait for more data to be written to the file; it returns 0 bytes immediately. Blocking does not come into play when reading from on-disk files, aside from the usually imperceptible delay when data on disk is read into memory.

这与TTY中,插座和管道时更重要。例如,从标准输入读取时标准输入连接到终端将阻塞,直到用户键入的东西。如果我们没有从对方接收到的数据从一个套接字读将阻塞。从管道读取将阻塞,直到在管的另一侧的程序写入东西

It's more relevant when working with TTYs, sockets, and pipes. For instance, reading from stdin when stdin is connected to the terminal will block until the user types something. Reading from a socket will block if we haven't received data from the other side. Reading from a pipe will block until the program on the other side of the pipe writes something.

这篇关于如何阻止读系统调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆