Linux 内核态 与 用户态传递 实时信号

http://juckyjhon.blog.sohu.com/156355213.html

 

kernel:

#include <linux/config.h>

#include <linux/modversions.h>

#include <linux/init.h>

#include <linux/module.h>

#include <linux/kernel.h>

#include <linux/signal.h>

#include <linux/sched.h>



#define SIG_MYINT 33

#define PS_PID 30 //the user process id



static int mysig_init(void)

{

siginfo_t info;

struct task_struct *p;



info.si_signo = SIG_MYINT;

info.si_code = -1;//__SI_RT;

info.si_int = 0x8;



read_lock(&tasklist_lock);

for_each_task(p)

{

if (p->pid == PS_PID)

{

read_unlock(&tasklist_lock);

goto find_ps;

}

}

read_unlock(&tasklist_lock);

printk("can't find the process <%d>, you should run it first!
", PS_PID);

return -1;



find_ps:

printk("send signal to user space.
");

send_sig_info(SIG_MYINT, &info, p);



return 0;

}



static void mysig_exit(void)

{

printk("my signal test exit.
");

}



MODULE_LICENSE("GPL");

MODULE_AUTHOR("xxxx");



module_init(mysig_init);

module_exit(mysig_exit);










user:
#include <stdio.h>
#include <signal.h>

#define SIG_MYINT	33

void my_handler(int signo, siginfo_t *info, void *context)
{
	printf("signal number: signo = %d, info->si_signo = %d
", signo,
info->si_signo);
	printf("info->si_int = %d
", info->si_int);
}

int main(int argc, char **argv)
{
	struct sigaction new_sa;

	new_sa.sa_handler = my_handler;
	sigemptyset(&new_sa.sa_mask);
	new_sa.sa_flags = SA_RESTART | SA_SIGINFO;
	new_sa.sa_restorer = NULL;
	sigaction(SIG_MYINT, &new_sa, NULL);

	while (1)
	{
		pause();
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值