prctl() 的正确使用方法

标签 c linux system-calls glibc variadic

prctl的原型(prototype)是

int prctl(int option, unsigned long arg2, unsigned long arg3,
          unsigned long arg4, unsigned long arg5);

man page而在 header它被声明为可变参数函数:

extern int prctl (int __option, ...) __THROW;

  1. 当我只需要 2 个参数时,是否必须使用 5 个参数来调用它?
  2. 是否需要将 args 转换为 unsigned long

最佳答案

只需传递您必须传递的内容并在其余参数中写入 0 转换为 unsigned long 或完全跳过它们。由于 prctl 被声明为可变参数函数,它将相应地处理这种情况。

const char* name = "The user";
if (prctl(PR_SET_NAME, (unsigned long) name,
         (unsigned long)0, (unsigned long)0, (unsigned long)0) == -1)
{
    // handle error
    perror("prctl failed");
    return -1;
}

const char* name = "The user";
if (prctl(PR_SET_NAME, (unsigned long) name) == -1)
{
    // handle error
    perror("prctl failed");
    return -1;
}

关于prctl() 的正确使用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36551394/

相关文章:

c - 适用于 NetBeans 和 Windows 的最常用 C/C++ 编译器

c++ - const char* 与字符串文字的使用

sockets - 当 read() 速率低于到达的 TCP 数据包速率时

c - srand 与 rand 函数有什么关系?

C 语言 : Children don't exit, 也没有终止,也没有杀死我的父进程

python - 可以在没有 Visual Studio 的情况下使用 MinGw 编译 python.exe

来自旧硬盘系统的 mysql datadir 无法启动

linux - 在 Bash 中使用 $RANDOM 重复数字

c - 如何确保数据写入物理介质?

c - wait 和 waitpid 在 Linux 中返回时会阻塞 SIGCHLD 并解除阻塞吗?