简体   繁体   English

系统命令行perl中的最大长度

[英]system command line max length in perl

To reduce the number of commands issued, I need to put as many characters as I can in the perl statement 为了减少发出的命令数量,我需要在perl语句中添加尽可能多的字符

system("$cmd_and_parameters")

What is the (safe) max line length I can assume, both under Windows and Linux? 在Windows和Linux下,我可以假设(安全)最大线路长度是多少?

My question is due to the need to issue commands of the Oetiker "rrdtool suite", without using the proper perl modules (please, don't blame me for this). 我的问题是由于需要发出Oetiker“rrdtool套件”的命令, 而不使用正确的perl模块(请不要因此而责怪我)。

My first attempt was using the before mentioned function, setting 我的第一次尝试是使用前面提到的功能,设置

$cmd_and_parameters = "path_to_rrdtool/rrdtool update $db $timestamp:$value" 

It works, but it's slow, considering the number of updates I have to do. 考虑到我必须做的更新次数,它可以工作,但速度很慢。

So, as "rrdtool update" allows it, I decided to increase the number of arguments up to the maximum allowed, reducing the number of "system" calls. 因此,当“rrdtool update”允许时,我决定将参数数量增加到允许的最大值,从而减少“系统”调用的数量。

Using a reasonable small number of arguments, it works much better, and anyway it's good enough to my needs, but still remains the question "How many $timestamp:$value couples can I put in the system function? 使用合理的少量参数,它可以更好地工作,无论如何它对我的需求来说已经足够好了,但仍然是一个问题“我可以在系统函数中添加多少$ timestamp:$值对夫妻?

That is to say: how many characters can be in a perl system function? 也就是说:perl系统函数中可以有多少个字符?

On Linux, the limit is ARG_MAX (which you could query with getconf ARG_MAX ). 在Linux上,限制是ARG_MAX (您可以使用getconf ARG_MAX查询)。 I have th habit of editing my kernel's limit (eg in linux-3.7.1/include/uapi/linux/limits.h to increase it). 我有编辑内核限制的习惯(例如在linux-3.7.1/include/uapi/linux/limits.h中增加它)。

(Notice that setrlimit(2) RLIMIT_STACK may in practice lower the maximal size of program arguments) (请注意, setrlimit(2) RLIMIT_STACK可能会降低程序参数的最大大小)

Some Linux kernels (notably old, or embedded) have a lower limit. 某些Linux内核(特别是旧内核或嵌入式内核)具有下限。 You should be prepared to have it as low as perhaps 32768 (on my own kernel I'm raising it to 2097152). 你应该准备好它可能低至32768(在我自己的内核上我将它提升到2097152)。

The reason I am raising my ARG_MAX is that I have plenty of RAM (16Gbytes), my shell is zsh and I love typing **/*.c when I want to (thus avoiding a find ) 我提高ARG_MAX的原因是我有足够的RAM(16Gbytes),我的shell是zsh而且我喜欢在我想要的时候键入**/*.c (从而避免find

Read the execve(2) Linux man page and the Posix exec documentation. 阅读execve(2) Linux手册页和Posix exec文档。 The Linux man page has several paragraphs about Limits on size of arguments and environment . Linux手册页有几个关于参数大小和环境限制的段落。

On Windows (or at least on MS-DOS) it might be as low as 128 (before expansion). 在Windows上(或至少在MS-DOS上)它可能低至128(在扩展之前)。

And you should understand that argument expansion is (on Posix & Linux system) done by the shell, and that system(3) and Perl's function of that name probably is starting a /bin/sh -c which will do the expansion. 你应该明白,参数扩展是(在Posix和Linux系统上)由shell完成的,而系统(3)和Perl这个名称的功能可能正在启动一个/bin/sh -c ,它将进行扩展。 What matters is the size of the expanded argument list: so if you call system("ls */*/*.c") and if */*/*.c expands to a million arguments you are in trouble (at least on Posix and Linux) even if the unexpanded command line ls */*/*.c is very short. 重要的是扩展参数列表的大小:所以如果你调用system("ls */*/*.c")并且如果*/*/*.c扩展到一百万个参数你就遇到了麻烦(至少在Posix和Linux)即使未展开的命令行ls */*/*.c非常短。

On Windows and MS-DOS the runtime library is rumored to expand the command line into an argument list. 在Windows和MS-DOS上,传闻运行时库将命令行扩展为参数列表。 I heard that the program starts with the terminal command uninterpreted, and some runtime startup thing is doing the expansion. 我听说程序以未解释的终端命令开始,并且一些运行时启动事情正在进行扩展。

So to answer your question: on Posix systems, what matters is the expanded argument list, and if that expanded argument list is smaller than 32K bytes you are very probably very safe (In practice 128Kbyte is good enough). 所以回答你的问题:在Posix系统上,重要的是扩展参数列表,如果扩展参数列表小于32K字节,你很可能非常安全(实际上128Kbyte已经足够了)。 On Windows or MS-DOS (I am guessing that) you probably need to limit the unexpanded command line to 128 chars and its expansion to 64Kbytes (in practice bigger thresholds would often work). 在Windows或MS-DOS上(我猜这个)你可能需要将未扩展的命令行限制为128个字符,并将其扩展到64K字节(实际上更大的阈值通常会起作用)。 I really think you should always test the result of system 我真的认为你应该总是测试system的结果

Basile has pretty much conclusively answered the Linux answer. Basile几乎已经对Linux的答案做出了最终的回答。

Windows NT and related versions (That means NOT Windows 95, 98 or ME - they are, I think limited to 128 charcters including command name) has a command line of either 2047 or 8191 characters. Windows NT和相关版本(这意味着不是Windows 95,98或ME - 它们,我认为仅限于128个字符,包括命令名称)具有2047或8191个字符的命令行。 http://support.microsoft.com/kb/830473 http://support.microsoft.com/kb/830473

Unless you really need to support ancient (12+ year old) versions of Windows, 2KB minus a tiny bit should be fine. 除非你真的需要支持古老的(12岁以上)版本的Windows,否则2KB减去一点就应该没问题。

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

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