Linux上的最大主机名长度 [英] Maximum hostname length on Linux
问题描述
我正在收集一个mpi应用程序中所有节点的主机名.我正在使用gethostname,我想确保我有足够的空间来存储生成的字符串.我特别避免使用MPI的getprocessorname,因为我想要机器的名称,并且我不想从那里解析它.
I am gathering the hostnames of all nodes in an mpi application. I'm using gethostname and I want to ensure I have enough space to store the resulting string. I'm specifically avoiding MPI's getprocessorname as I want the machine's name and I don't want to have to parse it from that.
似乎有两个选择,
bits/local_lim.h:#define HOST_NAME_MAX 64
bits/posix1_lim.h:#define _POSIX_HOST_NAME_MAX 255
每种产品都有哪些优势?我应该直接包含这些标头之一,还是应该在其他包含之前使用posix标志?
What are the advantages of each? Should I include one of these headers directly or is there a posix flag I should use before some other include?
推荐答案
要保持便携性,请务必坚持使用HOST_NAME_MAX
.
To stay portable you should defintitly stick to HOST_NAME_MAX
.
在 gethostname()
的Linux手册页中 (引用POSIX spex):
From the Linux man-page for gethostname()
(quoting the POSIX spex):
SUSv2保证主机名限制为255个字节". POSIX.1-2001保证主机名(不包括 终止为空字节)限制为 HOST_NAME_MAX 个字节". Linux HOST_NAME_MAX 定义为值64,该值已 从Linux 1.0开始限制(较早的内核限制为8个字节).
SUSv2 guarantees that "Host names are limited to 255 bytes". POSIX.1-2001 guarantees that "Host names (not including the terminating null byte) are limited to HOST_NAME_MAX bytes". On Linux, HOST_NAME_MAX is defined with the value 64, which has been the limit since Linux 1.0 (earlier kernels imposed a limit of 8 bytes).
请在此处阅读有关最新的POSIX规范,其中还提到HOST_NAME_MAX
作为限制. (这与POSIX的早期版本(其中255被定义为限制)不同.)
Please read here for the current POSIX specification, which also mentions HOST_NAME_MAX
as the limit. (This is different from earlier version of POSIX where 255 was defined as limit.)
这篇关于Linux上的最大主机名长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!