欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

正确使用 Linux 中的 prctl 函数:我们简化使用 prctl 的技巧

最编程 2024-01-04 21:14:37
...

prctl的原型是

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;

>当我只需要2个参数时,是否必须用5个参数调用它?

>是否需要将args强制转换为unsigned long?

解决方法:

只需传递您必须传递的内容,然后在其余参数中将0强制转换为无符号数即可,或者完全跳过它们.由于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;

}

标签:variadic,system-calls,glibc,c-3,linux

来源: https://codeday.me/bug/20191118/2031037.html

推荐阅读