c - 如何获取 PID_MAX 宏?

标签 c freebsd

我想获取文件 /sys/sys/proc.h 中存在的 PID_MAX 宏的值。

我当前的代码(main.c):

#include <stdio.h>
#include <stdlib.h>

#include <sys/types.h> /* type pid_t */
#include <sys/proc.h> /* macro PID_MAX */
#include <sys/unistd.h> /* function getpid, getppid */

/*
gcc -Wall -Wextra main.c -o main
./main
*/

int main ()
{
    pid_t pidmax = PID_MAX;

    printf ( "Value = %d\n", pidmax );

    return 0;
}

返回以下错误:

error: 'PID_MAX' undeclared (first use in this function); did you mean 'UID_MAX'?

这怎么可能?

恢复 PID_MAX 的另一种方法?

最佳答案

没有独立于平台的方法来检索最大 pid 值。 例如在linux下,可以通过/proc/sys接口(interface)确定该值

$ sysctl kernel/pid_max
32768

在 FreeBSD 下,该值为 99999。如果您仔细查看 sys/proc.h,您会注意到宏 PID_MAX

保护
#ifdef _KERNEL
...
#define PID_MAX 99999
...
#endif

这就是为什么你不能(也不应该)在用户空间程序中使用它。

关于c - 如何获取 PID_MAX 宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58627561/

相关文章:

c - 如何获取已死亡子进程的 PID 并在父进程中使用它?

c - 如何将变量的值从c中的循环中传递出去

shell - 如何关闭ZSH shell中的颜色?通过 unicode 支持将其设为黑白!

c++ - GDB 回溯消息 "0x0000000000000000 in ?? ()"是什么意思?

c - 静态编译 SDL2 程序时的问题

linux - 无法在 freeBSD 上安装 Linux::Inotify2

macos - OS X上的grep -f产生段错误

c++ - CURL SSH key 密码安全

C GtkTreeModel/Store内存释放

c - 扫描输入字符串并将单词存储在 C 数组中