正确使用prctl()的方法

prctl的原型是

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

在man page中,而在头文件中,它被声明为可变参数函数:

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

  1. 当我只需要2个参数时,是否需要用5个参数来调用?
  2. 我需要将参数投给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; }