在 Linux 内核文件中使用系统调用

using system call in Linux kernel file

我正在 Linux 中实现自定义进程调度程序。而且我想使用系统调用来记录我的程序,以便我可以轻松调试。

我写的文件是

源代码:linux-x.x.x/kernel/sched_new_scheduler.c

sched_new_scheduler.c中我可以直接使用syscall(the id of the system call, parameter);吗? syscall();在C程序中好像是和#include<sys/syscalls.h>一起使用的,但是在kernel/中找不到“.h”。

我只是想通过记录一些东西来了解我的程序是如何执行的,那么我可以直接在sched_new_scheduler.c中写printk("something");吗?或者尝试使用系统调用的正确方法?

系统调用看起来像是其他内核函数的包装器如何在内核中使用系统调用的方法之一是查找精确系统调用的子函数。例如:

int open(const char *pathname, int flags, mode_t mode); -> filp_open

////////////////////////////////////////////////////////////////////////////////////////////////
struct file* file_open(const char* path, int flags, int rights)
{
    struct file* filp = NULL;
    mm_segment_t oldfs;
    int err = 0;

    oldfs = get_fs();
    set_fs(get_ds());
    filp = filp_open(path, flags, rights);
    set_fs(oldfs);

    if(IS_ERR(filp)) {
        err = PTR_ERR(filp);
        return NULL;
    }

    return filp;
}

ssize_t write(int fd, const void *buf, size_t count); -> vfs_write

////////////////////////////////////////////////////////////////////////////////////////////////
int file_write(struct file* file, unsigned long long offset, unsigned char* data, unsigned int size)
{
    mm_segment_t oldfs;
    int ret;

    oldfs = get_fs();
    set_fs(get_ds());

    ret = vfs_write(file, data, size, &offset);

    set_fs(oldfs);
    return ret;
}

应用程序应该使用系统调用来利用内核提供的服务。您可以在内核模块中实现系统调用,但应该从应用程序中调用。如果您只想将新调度程序的统计信息公开给用户空间进行调试,您可以使用 proc、sys、debugfs 等接口。这比实现系统调用并编写用户空间应用程序来使用它要容易得多.