cse3_sol.c
The snippet can be accessed without any authentication.
Authored by
Laurenz Friedrich Grote
Ported MuLö of CSE Ex to Linux 5
cse3.c 1.93 KiB
#include <linux/module.h> /* modules */
#include <linux/kernel.h> /* KERN_INFO */
#include <linux/workqueue.h>
#include <linux/slab.h>
#include <linux/smp.h>
#include <linux/timer.h>
#include <linux/wait.h>
#include <linux/sched.h>
MODULE_AUTHOR("CSE");
MODULE_LICENSE("GPL");
int ret, i;
int flag = 0;
struct work_struct *work1;
struct work_struct *work2;
struct timer_list my_timer;
static DECLARE_WAIT_QUEUE_HEAD(wq);
void my_timer_callback(struct timer_list *t)
{
printk(KERN_DEBUG "CSE: my_timer_callback called (%ld).\n", jiffies);
}
void my_wq_function(struct work_struct *work)
{
printk(KERN_DEBUG "CSE: CPU id = %i, let's check condition\n", smp_processor_id());
ret = wait_event_interruptible(wq, flag != 0);
printk(KERN_DEBUG "CSE: res is %i\n", ret);
kfree((void *)work);
timer_setup(&my_timer, my_timer_callback, 0);
ret = mod_timer(&my_timer, jiffies + msecs_to_jiffies(1000));
printk(KERN_DEBUG "CSE: timer started at (%ld).\n", jiffies);
return;
}
void my_wq_function2(struct work_struct *work)
{
printk(KERN_DEBUG "CSE: CPU id = %i, let's wake up the other guy to start the timer\n", smp_processor_id());
flag = 1;
wake_up_interruptible(&wq);
kfree((void *)work);
return;
}
int init_cse(void)
{
printk(KERN_DEBUG "CSE: Hello world\n");
work1 = kmalloc(sizeof(struct work_struct), GFP_KERNEL);
work2 = kmalloc(sizeof(struct work_struct), GFP_KERNEL);
if (work1) {
INIT_WORK( (struct work_struct *)work1, my_wq_function);
ret = schedule_work((struct work_struct *)work1);
}
if (work2) {
INIT_WORK( (struct work_struct *)work2, my_wq_function2);
ret = schedule_work((struct work_struct *)work2);
}
return 0;
}
void cleanup_cse(void)
{
flush_work((struct work_struct *)work1);
flush_work((struct work_struct *)work2);
ret = del_timer(&my_timer);
printk(KERN_INFO "CSE: Goodbye world\n");
}
module_init(init_cse);
module_exit(cleanup_cse);
Please register or sign in to comment