内核如何通知用户空间程序中断发生 [英] How kernel notify a user space program an interrupt occurrs

查看:233
本文介绍了内核如何通知用户空间程序中断发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个用户空间程序和一个内核空间设备驱动程序.

I'm writing a user space program and a kernel space device driver.

目标:一旦发生中断,用户空间程序就需要快速执行操作.

Goal: Once an interrupt occurs, user space program needs to do something quickly.

我的幼稚方法:用户空间程序使用ioctl调用wait_event_interruptible(),内核ISR调用awak_up_interruptible()唤醒用户空间程序.事实证明,从中断到用户空间需要花费太多时间.

My naive method: User space program uses ioctl to call wait_event_interruptible(), kernel ISR calls wake_up_interruptible() to wake up user space program. It turns out that it takes too much time from interrupt to user space.

还有什么更好的方法吗?

Is there any better way?

谢谢!

推荐答案

这里有一个类似的问题:

There is a similar question asked here:

向用户通知gpio中断内核模块中的空间

请检查上述问题.但是,我也可以提供我在那里建议的方法.

Please check above question. However, i can provide my approach which i suggested there as well.

您可以从内核API向用户空间线程发送信号,这可以帮助您运行非阻塞:

You can send a signal to user space thread from kernel API, which can help u run non-blocking:

send_sig(int sig, struct task_struct *p, int priv);

您需要了解内核中用户线程的pid.您可以通过/proc编写用户进程的pid,然后由内核读取pid来解决这个问题.通过这种安排,当发生中断时,内核可以向用户线程发送信号.如果您的进程重新启动或被杀死,则必须通过proc更新pid.仅用于状态通知,您可以使用此方法.但是,如果您想与状态一起传输数据,则Netlink或char驱动程序机制是个好方法.

You need to be aware of pid of user thread in Kernel. You can over come this by writing pid of user process via /proc and then kernel reading the pid. With this arrangement, when there is an interrupt, kernel can send signal to user thread. In case your process restarts or gets killed, you will have to update the pid via proc. Just for status notification you can use this method; however if you like to transfer data along with status than Netlink or char driver mechanism is good way.

这篇关于内核如何通知用户空间程序中断发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆