简体   繁体   English

在/ proc / pid / stat中使用文件名的奇怪行为

[英]Weird behavior with filename in /proc/pid/stat

I have a weird behavior with the value of filename in /proc/pid/stat. 我在/ proc / pid / stat中的filename值有一个奇怪的行为。

My program's name is "test_dev", and when I execute it with "./test_dev" and i look on /proc/pid/stat, i see "pid (test) ....". 我程序的名称是“ test_dev”,当我用“ ./test_dev”执行它时,我在/ proc / pid / stat上查看,我看到“ pid(test)....”。 Same in /proc/pid/status. 在/ proc / pid / status中相同。

I change "test_dev" to "testdev" to see if the underscore is the culprit of this mess, but the same thing appear again. 我将“ test_dev”更改为“ testdev”,以查看下划线是否是造成这种混乱的原因,但同一件事再次出现。

I printf argv[0], and I correctly see "test_dev" (or "testdev"). 我打印了argv [0],并且正确看到了“ test_dev”(或“ testdev”)。

I wonder how the field in stat is set, and why it's incomplet, because the man of /proc say that is the filename of the executable. 我想知道stat中的字段是如何设置的,为什么它不完整,因为/ proc的人说这是可执行文件的文件名。

I think a little, and I wonder if Eclipse can be the culprit. 我想了一下,我想知道Eclipse是否可以成为罪魁祸首。 This EDI gave me some surprise sometimes, and I won't be surprised if this is the case again, even if it really bug me that argv[0] and /proc/pid/stat have not the same value. 这个EDI有时使我感到有些惊讶,即使再次出现这种情况,即使argv [0]和/ proc / pid / stat的值都不相同,我也不会感到惊讶。

Is somebody have an explanation ? 有人解释吗?

thanks. 谢谢。

However, the real filename without "_dev" is 15 character long, and the "comm" field seem to be limited by the kernel to 15 character long ... So, the filename is truncated in /proc/pid/stat, … Where can I find this kind of documentation ? 但是,不带“ _dev”的实际文件名长度为15个字符,内核似乎将“ comm”字段限制为15个字符长。因此,文件名在/ proc / pid / stat中被截断了……我可以找到这种文档吗?

Unfortunately, this seems to be not well documented where one would expect it, but there are some hints here and there on the proc man page. 不幸的是,这似乎并没有得到很好的证明,但是在proc手册页上却有一些提示。

/proc/[pid]/comm (since Linux 2.6.33)
              …  Strings longer than TASK_COMM_LEN (16) characters are silently truncated.
/proc/[pid]/stat
              Status information about the process.  This is used by ps(1).
              It is defined in the kernel source file fs/proc/array.c.

TASK_COMM_LEN and the task_struct are defined in include/linux/sched.h : TASK_COMM_LENtask_structinclude / linux / sched.h中定义:

#define TASK_COMM_LEN           16
…
struct task_struct {
…
    char                comm[TASK_COMM_LEN];

The difference between the 15 characters you observe and the 16 here is due to the terminating '\\0' . 您观察到的15个字符与此处的16个字符之间的差异是由于终止符'\\0'

The field in /proc/pid/stat you mention does not show argv[0], but the executed command. 您提到的/ proc / pid / stat中的字段未显示argv [0],但显示了已执行的命令。 Probably, eclipse compiles the file to an executable named "test" and executes it. eclipse可能将文件编译为名为“ test”的可执行文件并执行该文件。

This example: 这个例子:

execl("foo", "bar", "baz", NULL);

has a comm of "foo" (the thing in /proc/pid/stat), an argv[0] of "bar" and "baz" as argv[1] 逗号为“ foo”(/ proc / pid / stat中的内容),argv [0]为“ bar”,“ baz”为argv [1]

To double-check that, you might try to call your program "xxxxx" and see, if it is still named "test" in /proc/pid/stat 要再次确认,您可以尝试调用程序“ xxxxx”,看看是否在/ proc / pid / stat中仍将其命名为“ test”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM