gpt4 book ai didi

c - 从 LKM 发送数据到用户空间

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:24:03 24 4
gpt4 key购买 nike

我有一个生成数据的 Linux 内核模块,我想将该数据发送到用户空间应用程序。有哪些选择,它们有哪些优点/缺点?

比方说,数据可能是每秒几个 char[] 和几个 int[] 的 100 个结构。

此外,如果可能(非强制),我希望这些结构按照它们在 LKM 中生成的相同顺序到达用户区。

最佳答案

您可以尝试传递包含缓冲区指针和缓冲区大小的结构。但同样的结构也应该在用户空间应用程序和内核中的系统调用代码中定义。

     struct new_struct
{
void *p; //set this pointer to your buffer...
int size;
};
//from user-application...

int main()
{
....
struct new_struct req_kernel;
your_system_call_function(...,(void *)&req_kernel,...);
// here u can get the data which is copied from kernel-space in req_kernel structure.
}


........................................................................................


//this is inside your kernel...
your_system_call(...,char __user optval,...)
{
.....
struct new_struct req;
// fill the data in the resq structure. which will be access in user-space.
if (copy_to_user(&req, optval, sizeof(req)))

return -EFAULT;
//now you have the address or pointer & size of the buffer
//which you want in kernel with struct req...
}

引用http://people.ee.ethz.ch/~arkeller/linux/kernel_user_space_howto.html

关于c - 从 LKM 发送数据到用户空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16230945/

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