简体   繁体   English

即使我包含了 unistd.h,在内核代码中也隐式声明了 getpid

[英]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->psem->headstruct 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.

相关问题 我没有包括<unistd.h>头文件,为什么我仍然可以成功调用getpid()函数? - I have not included the <unistd.h> header file, why can I still invoke the getpid() function successfully? 导入unistd.h之后,编译器指出sbrk()是隐式声明。 为什么是这样? - After importing unistd.h, compiler states that sbrk() is an implicit declaration. Why is this? 隐含的函数声明,即使我已经包含该文件? - implicit declaration of function even though i have included the file? 包含unistd.h的write()的包装程序会导致错误 - Wrapper routine for write() with unistd.h included results in error 内核编译但不会与unistd.h参考链接 - Kernel compiles but won't link with unistd.h reference 不能在 Linux kernel 模块中包含 unistd.h - Cannot include unistd.h in Linux kernel module 我在研究定义在中的read()函数代码时遇到了麻烦<unistd.h> - I have a trouble with looking into the read() function code defined in <unistd.h> 未定义的参考“睡眠”,但我确实包括<unistd.h> - Undefined reference to “sleep” but I did include <unistd.h> 为什么我可以在 windows 中访问 unistd.h? - Why can I access unistd.h in windows? 警告:function 'malloc' 的隐式声明,即使<stdlib.h>已经包括了</stdlib.h> - Warning: implicit declaration of function ‘malloc’, even if <stdlib.h> is included
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM