进程名称长度的最大允许限制是多少? [英] What is the maximum allowed limit on the length of a process name?

查看:935
本文介绍了进程名称长度的最大允许限制是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

进程名称允许的最大长度是多少?我正在从/proc/[pid]/stat文件中读取进程名称,我想知道我需要的最大缓冲区.

What is the maximum length allowed for a process name? I am reading the process name from /proc/[pid]/stat file and i would like to know the maximum buffer that i would need.

我很确定有一个可配置的限制,但无法确定它在哪里.

I am pretty sure there is a limit which is configurable but just can't find out where this is.

推荐答案

根据

PR_SET_NAME(自Linux 2.6.9起)

PR_SET_NAME (since Linux 2.6.9)

使用(char *)arg2指向的位置中的值设置调用线程的名称. 名称的长度最多为16个字节,如果包含的字节较少,则应以空值结尾.

Set the name of the calling thread, using the value in the location pointed to by (char *) arg2. The name can be up to 16 bytes long, and should be null-terminated if it contains fewer bytes.

所以我要使用 16个字节的长缓冲区.

So I'd go for a 16 bytes long buffer.

让我再备份一点.

Linux中的每个进程对应于 在内核中,这是在include/linux/sched.h中定义的.

Each process in Linux corresponds to a struct task_struct in the kernel, which is defined in include/linux/sched.h.

在此定义中,存在一个字段 ,根据注释,它指的是可执行文件名,不包括路径:

In this definition, there's a field char comm[TASK_COMM_LEN], which according to the comment refers to the executable name excluding the path:

    char comm[TASK_COMM_LEN]; /* executable name excluding path
                                 - access with [gs]et_task_comm (which lock
                                   it with task_lock())
                                 - initialized normally by setup_new_exec */

它的大小TASK_COMM_LEN在上面的同一头文件此处 16个字节:

Its size, TASK_COMM_LEN, is defined above in the same header file, here, to be 16 bytes:

/* Task command name length */
#define TASK_COMM_LEN 16


此外,在第22页引用LDD3:


Furthermore, quoting LDD3 page 22:

...

...

以下语句显示当前的进程ID和命令名称 通过访问struct task_struct中的某些字段进行处理:

the following statement prints the process ID and the command name of the current process by accessing certain fields in struct task_struct :

printk(KERN_INFO "The process is \"%s\" (pid %i)\n",
        current->comm, current->pid);

current->comm中存储的命令名称是程序文件的基本名称 当前进程正在执行的操作(如果需要,可剪裁为15个字符).

The command name stored in current->comm is the base name of the program file (trimmed to 15 characters if need be) that is being executed by the current process.

这篇关于进程名称长度的最大允许限制是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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