小编lit*_*ead的帖子

从内核模块写入eventfd

我使用eventfd()在用户空间程序中创建了一个eventfd实例.有没有一种方法可以将一些引用(指向其struct或pid + fd对的指针)传递给这个创建的eventfd实例到内核模块,以便它可以更新计数器值?

这就是我想要做的:我正在开发一个用户空间程序,它需要与我编写的内核空间模块交换数据和信号.为了传输数据,我已经在使用ioctl了.但是我希望内核模块能够在新数据准备好通过ioctl消耗时发出信号通知用户空间程序.

为此,我的用户空间程序将在各种线程中创建一些eventfds.这些线程将使用select()等待这些eventfds,并且每当内核模块更新这些eventfds上的计数时,它们将通过ioctl请求它来继续使用数据.

问题是,如何从内核空间解析这些eventfds的"struct file*"指针?我可以将什么样的信息发送到内核模块,以便它可以获取指向eventfds的指针?我将在内核模块中使用哪些函数来获取这些指针?

是否有更好的方法从内核空间向用户空间发送事件信号?我不能放弃使用select().

linux linux-kernel

19
推荐指数
2
解决办法
7469
查看次数

Select() 对 eventfd 的行为不正确

我想使用 eventfd 作为在内核空间和用户空间之间发出简单事件信号的方式。eventfd 将用作信号发送方式,实际数据将使用 ioctl 进行传输。在继续实现此操作之前,我编写了一个简单的程序来查看 eventfd 在 select() 中的行为方式。看来,如果您使用 select 等待 eventfd,当您在单独的线程中写入它时,它不会返回。在我编写的代码中,写入线程从程序启动开始等待5秒,然后两次写入eventfd。我希望 select() 在这次写入之后立即在读取线程中返回,但这不会发生。select() 仅在 10 秒超时后返回并返回零。不管这个返回零如何,当我在 10 秒后尝试读取 eventfd 时,我得到了正确的值。

我使用 Ubuntu 12.04.1 (3.2.0-29-generic-pae) i386

知道为什么会这样吗?在我看来, select() 没有按应有的方式工作。

PS:这个问题类似于linux - Can't get eventfd to work with epoll Together

还有其他人面临类似的问题吗?

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>     //Definition of uint64_t

#include <pthread.h>    //One thread writes to fd, other waits on it and then reads it
#include <time.h>       //Writing thread uses delay before writing
#include <sys/eventfd.h>

int efd; …
Run Code Online (Sandbox Code Playgroud)

linux

5
推荐指数
1
解决办法
4340
查看次数

C函数返回一个数组

我一直认为,当你想从一个函数返回一个数组时,唯一的方法是使用像这样的指针: char * func();

但昨天,当我经历K&R时,我注意到 错误地认为char x()[]也是一个有效的结构.所以我继续测试了这个并编写了以下代码:

#include <stdio.h>
#include <stdlib.h>

char string1[10] = "123456789";


char x(void)[10];

int main(void) {
    printf("string returned by x() is %s",x());
    return EXIT_SUCCESS;
}

char x(void)[10] {
    return x;
}
Run Code Online (Sandbox Code Playgroud)

在Windows上使用GCC进行编译时,会产生以下错误:

..\src\07arrreturn.c:7:6: error: 'x' declared as function returning an array
..\src\07arrreturn.c: In function 'main':
..\src\07arrreturn.c:10:2: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat]
..\src\07arrreturn.c: At top level:
..\src\07arrreturn.c:14:6: error: 'x' declared as function …
Run Code Online (Sandbox Code Playgroud)

c

-1
推荐指数
2
解决办法
3421
查看次数

标签 统计

linux ×2

c ×1

linux-kernel ×1