概述
创建线程
允许信号
屏蔽指定信号
信号处理
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/kthread.h>
#include <linux/err.h>
#include <linux/delay.h>
static struct task_struct *test_thread;
static int test_thread_func(void *arg)
{
sigset_t hupmask;
siginitset(&hupmask, sigmask(SIGHUP));
allow_signal(SIGKILL);
allow_signal(SIGSTOP);
allow_signal(SIGCONT);
allow_signal(SIGHUP);
set_user_nice(current, 10);
while (!kthread_should_stop()) {
signed long timeout = 10 * HZ;
sigprocmask(SIG_UNBLOCK, &hupmask, NULL);
printk("1 jiffies: %ldn", jiffies);
ssleep(5);
printk("2 jiffies: %ldn", jiffies);
//while (timeout) {
timeout = schedule_timeout_interruptible(timeout);
//}
while (signal_pending(current)) {
siginfo_t info;
unsigned long signr;
signr = dequeue_signal_lock(current, ¤t->blocked, &info);
printk("%s, signr: %ld, %ldn", __func__, signr, jiffies);
switch(signr) {
case SIGSTOP:
printk("%s, %d, %ldn", __func__, __LINE__, jiffies);
set_current_state(TASK_STOPPED);
schedule();
break;
case SIGKILL:
printk("%s, %d, %ldn", __func__, __LINE__, jiffies);
goto die;
case SIGHUP:
printk("%s, %d, %ldn", __func__, __LINE__, jiffies);
break;
case SIGCONT:
printk("%s, %d, %ldn", __func__, __LINE__, jiffies);
break;
default:
printk("%s, %d, %ldn", __func__, __LINE__, jiffies);
}
}
sigprocmask(SIG_BLOCK, &hupmask, NULL);
printk("3 jiffies: %ldn", jiffies);
ssleep(5);
printk("4 jiffies: %ldn", jiffies);
}
die:
return 0;
}
static int __init test_init(void)
{
test_thread = kthread_run(test_thread_func, NULL, "test_thread");
if (IS_ERR(test_thread)) {
printk("Failed to create the test threadn");
test_thread = NULL;
}
return 0;
}
static void __exit test_exit(void)
{
if (test_thread) {
kthread_stop(test_thread);
test_thread = NULL;
}
}
module_init(test_init);
module_exit(test_exit);
MODULE_AUTHOR("tony");
MODULE_DESCRIPTION("test");
MODULE_LICENSE("GPL");
MODULE_ALIAS("test");
最后
以上就是欢喜牛排为你收集整理的Linux内核线程中信号处理的全部内容,希望文章能够帮你解决Linux内核线程中信号处理所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复