Linux内核线程:如何将Linux模块写入功能作为线程必须执行的功能? [英] Linux Kernel Threads : How to pass the Linux module write function as the function that the thread has to execute?
问题描述
我正在开发与用户空间程序通信的 Linux内核模块.该模块等待从用户空间发送的消息,以便在内核模式下打印该消息.
I am developing Linux kernel module that communicate with user space program. This module waits for a message which is being sent from user space in order to print it in kernel mode.
这是模块:
#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;
}
这很好用,我从用户空间输入的消息被打印在内核空间中.
This works fine and the message that I enter from user-space is printed in kernel space.
现在,我正在使用此示例来学习内核线程编程.
Now, I am using this example in order to learn kernel thread programming.
我试图使LKM 写功能成为内核线程必须执行的功能,所以我修改了我的模块,如下所示:
I am trying to make the LKM write function as a function that kernel thread has to execute, so I modified my module like this :
#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);
这里的问题,当我编译模块时,发生错误:
The problem here, when I compile the module, an error occurs :
/exer_simple_char_drv.c:63:27:错误:传递的参数1为 指针类型不兼容的"kthread_create_on_node" [-Werror =不兼容的指针类型] thread1 = kthread_create(exer_write,NULL,our_thread);
/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);
搜索网络时,我发现该函数的调用创建了一个线程:
Searching the net, I find that a thread is created by the call to the function :
struct task_struct *kthread_create(int (*function)(void *data), void *data, const char name[], ...)
该函数具有第一个参数,即线程必须执行的函数,并且具有参数(void *data)
,但是,我的写入内核函数没有相同的参数:
This function has as first argument the function that the thread has to execute and has as argument (void *data)
, but, my write kernel function has not the same arguments :
ssize_t exer_write(struct file *pfile, const char __user *buffer, size_t length, loff_t *offset)
请问有办法解决这个问题吗?如何修改我的Linux内核以将write
函数作为线程执行?
Is there a way to solve this problem please ? How can I modify my Linux kernel in order to execute the write
function as a thread ?
谢谢.
推荐答案
您不能.您的线程函数必须使用以下格式:
You can't. Your thread function has to use this format:
int my_thread_function(void *data)
您可以随意调用它-不必调用my_thread_function
-不必调用参数data
,但它必须是void *
.
You can call it whatever you want - it doesn't have to be called my_thread_function
- and the parameter doesn't have to be called data
but it does have to be a void *
.
这不起作用:
ssize_t exer_write(struct file *pfile, const char __user *buffer, size_t length, loff_t *offset)
我建议编写一个新函数作为您的线程函数:
I suggest writing a new function to be your thread function:
int exer_write_in_thread(void *data) {
exer_write(???, ???, ???, ???);
return 0;
}
很显然,您必须弄清楚要使用exer_write
调用哪些参数.
Obviously you have to figure out what arguments you want to call exer_write
with.
这篇关于Linux内核线程:如何将Linux模块写入功能作为线程必须执行的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!