创建线程
允许信号
屏蔽指定信号
信号处理
#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: %ld\n", jiffies);
ssleep(5);
printk("2 jiffies: %ld\n", 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, %ld\n", __func__, signr, jiffies);
switch(signr) {
case SIGSTOP:
printk("%s, %d, %ld\n", __func__, __LINE__, jiffies);
set_current_state(TASK_STOPPED);
schedule();
break;
case SIGKILL:
printk("%s, %d, %ld\n", __func__, __LINE__, jiffies);
goto die;
case SIGHUP:
printk("%s, %d, %ld\n", __func__, __LINE__, jiffies);
break;
case SIGCONT:
printk("%s, %d, %ld\n", __func__, __LINE__, jiffies);
break;
default:
printk("%s, %d, %ld\n", __func__, __LINE__, jiffies);
}
}
sigprocmask(SIG_BLOCK, &hupmask, NULL);
printk("3 jiffies: %ld\n", jiffies);
ssleep(5);
printk("4 jiffies: %ld\n", 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 thread\n");
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");