简体   繁体   中英

Registering a callback function in C

I am some Linux kernel space code. I want an application in user space to be able to register a callback function in the kernel space code by calling a function in the kernel space code and passing the address of that callback function. The kernel space code would then execute the callback function at a later time when running. I believe the kernel space code should look something like this:

typedef void (*callback_func) (void);
callback_func callback;

static void registerCallBack(callback_func funct){callback = funct;}

//another kernel space method
funct();

However, I am a little unsure on the proper typedef and if this will work properly. Can anyone confirm the functionality of this or offer any advice in this area? I am unable to test this right now as I am waiting on the rest of the kernel space code to be finished.

你的typedef看起来很好,虽然我建议将callback_func重命名为callback_func_t ,否则它看起来像一个变量而不是一个类型。

You cannot directly call to a userspace function from the kernel. One possible solution would be to use signals . From the kernel code you could send a signal to a given process. The signal handler would work then as a sort of callback function.

If you simply want to run a user-space application from the kernel, then this describes how to do that in detail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM