/proc/[pid]/cmdline文件大小 [英] /proc/[pid]/cmdline file size

查看:205
本文介绍了/proc/[pid]/cmdline文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在proc/[pid]中获取cmdline文件的文件大小.例如,porc/1/cmdline.该文件不为空,它包含"/sbin/init".但是我得到的是file_size = 0.

i'm trying to get the filesize of the cmdline file in proc/[pid]. For example porc/1/cmdline. The file is not empty, it contains "/sbin/init". But i get file_size = 0.

int main(int argc, char **argv) {
    int file_size;
    FILE *file_cmd;
    file_cmd = fopen("/proc/1/cmdline", "r");
    if(file_cmd == NULL) {
        perror("proc/1/cmdline");
        exit(1);
    }else {
        if(fseek(file_cmd, 0L, SEEK_END)!=0) {
            perror("proc/1/cmdline");
            exit(1);
        }
        file_size = ftell(file_cmd);
    }
    printf("fs: %d\n",file_size);
    fclose(file_cmd);
    }

致谢

推荐答案

这很正常.从您读取/proc文件时(它们中的大多数,有一些例外),它们是由内核生成的.这意味着在从文件读取之前不可能知道大小.可以将其视为文件上的量子力学.除非您阅读了该信息,否则您将不会获得状态,但不能保证再次阅读将给您两次相同的信息;-)

That's normal. /proc files (most of them, there are a few exceptions) are generated by the kernel at the moment you read from them. That means it's impossible to know the size before reading from the file. Think of it as Quantum Mechanics on files. You won't get a state unless you read the information, but there's no guarantee that reading again will give you the same information twice ;-)

换句话说,仅当您尝试读取EOF时,才会生成EOF.在此之前不存在,因此无法确定文件大小.

In other words, the EOF is only generated when you try to read it. It's not there before that, so there's no way a file size can be determined.

这实际上只是伪装成文件I/O的内核通讯.

This is really just communication with the kernel disguised as file I/O.

这篇关于/proc/[pid]/cmdline文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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