Linux kernel 将数据复制到缓冲区以在标准输出时打印

Linux kernel 将数据复制到缓冲区以在标准输出时打印,linux-kernel,linux-device-driver,stdout,kernel-module,Linux Kernel,Linux Device Driver,Stdout,Kernel Module,我正在编写一个linux内核模块,它创建一个块设备,列出进程列表 我能够在内核日志文件中显示该列表,但现在我正试图将输出显示到stdout。我的目标是在dev_open中创建一个包含所有进程的大字符串,然后将该字符串复制到缓冲区中,缓冲区将进入dev_read函数 然而,我正在尝试下面的代码,但是我收到一条消息,告诉stdout我被杀了 这是什么意思?我怎样才能解决这个问题 代码:: #include <linux/module.h> #include<linux/sched.

我正在编写一个linux内核模块,它创建一个块设备,列出进程列表

我能够在内核日志文件中显示该列表,但现在我正试图将输出显示到stdout。我的目标是在dev_open中创建一个包含所有进程的大字符串,然后将该字符串复制到缓冲区中,缓冲区将进入dev_read函数

然而,我正在尝试下面的代码,但是我收到一条消息,告诉stdout我被杀了

这是什么意思?我怎样才能解决这个问题

代码::

#include <linux/module.h>
#include<linux/sched.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/proc_fs.h>
#include <linux/kernel.h>

static int dev_open(struct inode *, struct file *);
static int dev_rls(struct inode *, struct file *);
static ssize_t dev_read(struct file *, char *, size_t, loff_t *);

int len,temp;
char msg[1000];
int tem;
static struct file_operations fops =
{
    .read = dev_read,
    .open = dev_open,
    .release = dev_rls,
};

int init_module(void)
{
    int t = register_chrdev(81,"tempo",&fops);

    if(t<0) printk(KERN_ALERT "Device failed to register!");
    else printk(KERN_ALERT "Registered device...\n");
    return t;
}

static int dev_open(struct inode *inod, struct file *fil)
{
    struct task_struct *task;   
    for_each_process(task)
    {
        printk("%s [%d]\n",task->comm, task->pid);
        strcat(&msg[0],task->comm);
        len=strlen(msg);
        temp=len;
        tem++;
    }
        printk("%s [%d]\n",task->comm , task->pid);

   return 0;
 }

void cleanup_module(void)
{
    unregister_chrdev(81,"tempo");
}

static ssize_t dev_read(struct file *filp,char *buf,size_t count,loff_t *offp)
{
    printk("Hi\n");
     if(count>temp)
   {
    count=temp;
   }
    temp=temp-count;
    copy_to_user(buf,msg, count);
    if(count==0)
       temp=len;

   return count;

   //return 0;
} 


static int dev_rls(struct inode *inod, struct file *fil)
{
    printk(KERN_ALERT"Done with device\n");
   return 0;
}

您完全有可能在msg的结尾之外写入,因为它只有1000字节长,并且您对长度没有限制。您是在dmesg/syslog中还是在控制台上获得内核堆栈跟踪?另外,您在dev_read中的逻辑对我来说没有意义。可能您打算最多复制个字节,但不超过msg中提供的字节数,但我认为它实际上并不是这样做的。例如,msg中有800个字节,但用户读取了5个字节,然后又读取了5个字节,以此类推。观察你的状态变量发生了什么。