[英]Implicit declaration of getpid in kernel code even though I included unistd.h
I'm currently working on a project that includes using PIDs.我目前正在从事一个包括使用 PID 的项目。 When I compile my program in a linux VM, I get the following message:
当我在 linux VM 中编译我的程序时,我收到以下消息:
kernel/sys.c:2405: error: implicit declaration of function ‘getpid’
kernel/sys.c:2407: error: implicit declaration of function ‘getpriority’
even though I have included:即使我已经包括:
#include <asm/unistd.h>
in my code.在我的代码中。
I have also tried to use types.h
, but that does not work either.我也尝试使用
types.h
,但这也不起作用。
Here is a snippet of the erroneous code, if it might help:这是错误代码的片段,如果它可能有帮助:
if(new_process != NULL){
pid = getpid(new_process->p);
pid2 = getpid(sem->head);
p = new_process->p;
if(getpriority(which, pid) > getpriority(which, pid2)){
new_process->next = p;
sem->head = p;
Any suggestions are welcome, and thanks for your help.欢迎任何建议,并感谢您的帮助。
As I understand, you're working with kernel code.据我了解,您正在使用内核代码。 There's no C standard library in the kernel!
内核中没有 C 标准库! You don't have
getpid()
, getpriority()
, or any other standard library function or header (some very common functions like strlen()
are re-defined in the kernel, but not all of them).您没有
getpid()
、 getpriority()
或任何其他标准库函数或头文件(一些非常常见的函数,如strlen()
在内核中重新定义,但不是全部)。 To achieve what you want, you'll have to use the appropriate equivalent kernel APIs, which are usually more low level.要实现您想要的功能,您必须使用适当的等效内核 API,它们通常级别较低。
Assuming that new_process->p
and sem->head
are struct task_struct
, you can get their priority with the task_nice()
function defined in linux/sched.h
(which is exactly what the getpriority
syscall does, if you take a look at the code ).假设
new_process->p
和sem->head
是struct task_struct
,你可以用得到他们的优先task_nice()
中定义的函数linux/sched.h
(这恰好是什么getpriority
系统调用呢,如果你看一看的代码)。
Your snippet could be then re-written like this:然后可以像这样重写您的代码段:
if(new_process != NULL) {
p = new_process->p;
if(task_nice(p) > task_nice(sem->head)) {
new_process->next = p;
sem->head = p;
If for some reason you also need the PID, you can access it directly through p->pid
, as it is a field of struct task_struct
.如果由于某种原因您还需要 PID,您可以直接通过
p->pid
访问它,因为它是struct task_struct
一个字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.