gpt4 book ai didi

c - `sysconf()` 和 `getrlimit()` 有什么关系和区别?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:04:18 26 4
gpt4 key购买 nike

sysconf()getrlimit()有什么联系和区别?

他们是否提供两种不同的方式来报告相同资源的限制?

我们什么时候使用哪个?

谢谢。

我发现关于这两个函数的两个相互矛盾的评论:

来自 http://forums.devshed.com/programming-42/whats-difference-sysconf-rlimit-955656.html

If I remember correctly, the sysconf() stuff cannot be changed by aprocess (i.e.) they are configured at a kernel level and need arecompile of the operating system kernel and/or C libraries to changethem. You can only read those values, but not alter them at runtime.

With setrlimit()/getrlimit(), you can read and change the values(within reason) of certain resources at runtime. For instance, you cancall setrlimit() to set the max. core dump size of a process, which isuseful if a process crashed often and you don't want to fill your harddisk up with large files.

The parameters you can read/set with rlimit are different from theparameters that you can read with sysconfig(). They deal withdifferent system knobs.

sysconf() and getrlimit() return the values of different resource settings (without overlap), so you call the appropriate one depending on what information your program seeks.

但来自 APUE:

For example, we can use the ulimit command built into the Bourne-againshell to change the maximum number of files our processes can have openat one time. This generally requires special (superuser)privileges if the limit is to be effectively unlimited. Butonce set to infinite, sysconf will report LONG_MAX as the limitfor OPEN_MAX. A program that relies on this value as the upper boundof file descriptors to close, as shown in Figure 2.17, will waste a lotof time trying to close 2,147,483,647 file descriptors, most of whicharen’t even in use.

Systems that support the XSI option in the SingleUNIX Specification will provide the getrlimit(2) function (Section7.11). It can be used to return the maximum number of descriptors that a process can have open. With it, we can detect thatthere is no configured upper bound to the number of open files ourprocesses can open, so we can avoid this problem.

最佳答案

sysconf 是一个通用函数,用于获取许多不同系统配置 变量的值,而不仅仅是资源限制。有些代表系统范围的限制。 sysconf 没有对应的设置函数(其中一些值不是动态的)。所有这些值都不应在同一进程中的两次调用之间发生变化(请参阅下面的异常(exception)情况)。

getrlimit 是一个获取资源限制值的函数。它的设置对应物是 setrlimit(可以有一些限制)。

两者都是由 POSIX 定义的,除了 OPEN_MAX 可能会在进程生命周期内发生变化外,标准没有提及重叠。

关于c - `sysconf()` 和 `getrlimit()` 有什么关系和区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50596153/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com