gpt4 book ai didi

c - Linux内核线程: How to pass the Linux module write function as the function that the thread has to execute?

转载 作者:行者123 更新时间:2023-11-30 14:34:46 26 4
gpt4 key购买 nike

我正在开发与用户空间程序通信的Linux内核模块。该模块等待从用户空间发送的消息,以便在内核模式下打印它。

这是模块:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/uaccess.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Gaston");
MODULE_DESCRIPTION("A simple Linux char driver");
MODULE_VERSION("0.1");

#define MAX 256
static char message[MAX] =""; ///< Memory for the string that is passed from userspace

ssize_t exer_open(struct inode *pinode, struct file *pfile) {

printk(KERN_INFO "Device has been opened\n");
return 0;
}


ssize_t exer_read(struct file *pfile, char __user *buffer, size_t length, loff_t *offset) {

return 0;
}


ssize_t exer_write(struct file *pfile, const char __user *buffer, size_t length, loff_t *offset) {
if (length > MAX)
return -EINVAL;

if (copy_from_user(message, buffer, length) != 0)
return -EFAULT;

printk(KERN_INFO "Received %s characters from the user\n", message);
return 0;

}


ssize_t exer_close(struct inode *pinode, struct file *pfile) {

printk(KERN_INFO "Device successfully closed\n");
return 0;
}


struct file_operations exer_file_operations = {
.owner = THIS_MODULE,
.open = exer_open,
.read = exer_read,
.write = exer_write,
.release = exer_close,
};


int exer_simple_module_init(void) {

printk(KERN_INFO "Initializing the LKM\n");
register_chrdev(240, "Simple Char Drv", &exer_file_operations);
return 0;
}


void exer_simple_module_exit(void) {

unregister_chrdev(240, "Simple Char Drv");
}


module_init(exer_simple_module_init);
module_exit(exer_simple_module_exit);

这是用户空间程序:

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<fcntl.h>
#include<string.h>
#include<unistd.h>


#define BUFFER_LENGTH 256

int main()

{

int ret, fd;
char stringToSend[BUFFER_LENGTH];


fd = open("/dev/char_device", O_RDWR); // Open the device with read/write access

if (fd < 0)
{
perror("Failed to open the device...");
return errno;
}


printf("Type in a short string to send to the kernel module:\n");

scanf("%s", stringToSend); // Read in a string (with spaces)

printf("Writing message to the device [%s].\n", stringToSend);

ret = write(fd, stringToSend, strlen(stringToSend)); // Send the string to the LKM

if (ret < 0)
{
perror("Failed to write the message to the device.");
return errno;
}

return 0;

}

这工作正常,我从用户空间输入的消息会打印在内核空间中。

现在,我使用这个示例来学习内核线程编程

我试图使 LKM 写入函数作为内核线程必须执行的函数,所以我像这样修改了我的模块:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/uaccess.h>
#include <linux/kthread.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Gaston");
MODULE_DESCRIPTION("A simple Linux char driver");
MODULE_VERSION("0.1");


#define MAX 256
static struct task_struct *thread1;
static char message[MAX] =""; ///< Memory for the string that is passed from userspace


ssize_t exer_open(struct inode *pinode, struct file *pfile) {

printk(KERN_INFO "Device has been opened\n");
return 0;
}



ssize_t exer_write(struct file *pfile, const char __user *buffer, size_t length, loff_t *offset) {

if (length > MAX)
return -EINVAL;

printk("Thread_fuction is running ...\n");

if (copy_from_user(message, buffer, length) != 0)
return -EFAULT;

printk(KERN_INFO "Received this message : %s ,from the user\n", message);

return 0;

}




struct file_operations exer_file_operations = {
.owner = THIS_MODULE,
.open = exer_open,
.write = exer_write,
};




int exer_simple_module_init(void) {

char our_thread[8]="thread1";

printk(KERN_INFO "Initializing the LKM\n");
register_chrdev(240, "Simple Char Drv", &exer_file_operations);

thread1 = kthread_create(exer_write,NULL,our_thread);
if((thread1))
{
printk(KERN_INFO "Thread is created");
wake_up_process(thread1);
}

return 0;
}



void exer_simple_module_exit(void) {

int ret;

unregister_chrdev(240, "Simple Char Drv");

ret = kthread_stop(thread1);
if(!ret)
printk(KERN_INFO "Thread stopped");
}


module_init(exer_simple_module_init);
module_exit(exer_simple_module_exit);

这里的问题是,当我编译模块时,出现错误:

/exer_simple_char_drv.c:63:27: error: passing argument 1 of ‘kthread_create_on_node’ from incompatible pointer type [-Werror=incompatible-pointer-types] thread1 = kthread_create(exer_write,NULL,our_thread);

在网上搜索,我发现一个线程是通过调用该函数创建的:

struct task_struct *kthread_create(int (*function)(void *data), void *data, const char name[], ...)

该函数的第一个参数是线程必须执行的函数,并且具有参数(void *data),但是,我的写入内核函数没有相同的参数:

ssize_t exer_write(struct file *pfile, const char __user *buffer, size_t length, loff_t *offset)

请问有什么办法可以解决这个问题吗?如何修改 Linux 内核以便将 write 函数作为线程执行?

谢谢。

最佳答案

你不能。您的线程函数必须使用以下格式:

int my_thread_function(void *data)

您可以随意调用它 - 它不必称为 my_thread_function - 并且参数不必称为 data 但它确实有成为 void *

这不会起作用:

ssize_t exer_write(struct file *pfile, const char __user *buffer, size_t length, loff_t *offset)

我建议编写一个新函数作为您的线程函数:

int exer_write_in_thread(void *data) {
exer_write(???, ???, ???, ???);
return 0;
}

显然,您必须弄清楚要使用哪些参数来调用 exer_write

关于c - Linux内核线程: How to pass the Linux module write function as the function that the thread has to execute?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58839420/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com