c 中的读取函数仅读取 3072 字节

read function in c only reading 3072 bytes

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
int main(){
        int fd;
        char bf[4096];
        int buf_size=4096;
        fd = open("/proc/18022/cmdline", O_RDONLY);
        int bytes = read(fd, bf, buf_size-1);
        printf("%d %s\n\n",bytes,bf);
        close(fd);
}

以上代码始终只读取 3072 个字节,而 cmdline 的字符数多于 3072 个。 如果我将 cmdline 的内容复制到 gedit,然后将 运行 上面的代码复制到这个新创建的文件中,那么它将读取文件的所有字节。 我用谷歌搜索它,发现它最多读取 SSIZE_MAX 个字节,但我怀疑为什么它在第二种情况下读取所有字节。

您不应该依赖于第一次尝试就读取整个文件,即使您知道您已经为读取分配了足够的 space。相反,您应该分块读取并逐块处理字节:

char buff[4096];
while((cnt = read(fd, bf, buf_size-1)) > 0) {
    // process the bytes just read, or append them to
    // a larger buffer
}

引用 read() 的手册页:

It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because read() was interrupted by a signal.

对于/proc个文件,我们可以看到here

The most distinctive thing about files in this directory is the fact that all of them have a file size of 0, with the exception of kcore, mtrr and self.

You might wonder how you can see details of a process that has a file size of 0. It makes more sense if you think of it as a window into the kernel. The file doesn't actually contain any data; it just acts as a pointer to where the actual process information resides.

这意味着那些伪文件的内容是由内核发送的,内核需要多少就分多少批。这看起来非常类似于管道,生产者写入数据,消费者读取数据,每个人都以不同的速度运行。