Milind Arun Choudhary's Weblog

http://www.csl.mtu.edu/~machoudh/

kernel daemons & mutex

Posted by milindchoudhary on March 5, 2007


/********************************************************************
*Description :: A kernel module which creates a kernel thread &
* shares a dummy resource with it,using a mutex for
* synchronization.
*Author :: Milind A Choudhary
*Date ::07-Dec-2005
********************************************************************/


#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/config.h>
#include <linux/version.h>
#include <asm/semaphore.h>

struct data {
int num;
}data={11};
pid_t kthread_pid1 ;
pid_t kthread_pid2 ;

DECLARE_MUTEX(mutex);

int kthread_routine_2(void *kthread_num)
{
//int num=(int)(*(int*)kthread_num);
int num=2;
char kthread_name[15];

printk(KERN_INFO "Inside daemon_routine() \n");
sprintf(kthread_name,"kern_daemon_%d",num);
daemonize(kthread_name);
allow_signal(SIGKILL);
allow_signal(SIGTERM);

do{
if(!down_trylock(&mutex)){
printk(KERN_INFO "kernel_daemon [%d] accessing the shared data\n",num);
data.num=22;
up(&mutex);
printk(KERN_INFO "kernel_daemon [%d] released the mutex\n",num);
}

printk(KERN_INFO "kernel_daemon [%d] waiting to acquire mutex\n",num);
set_current_state(TASK_INTERRUPTIBLE);
schedule_timeout(10*HZ);

}while(!signal_pending(current));
return 0;
}

int kthread_routine_1(void *kthread_num)
{
//int num=(int)(*(int*)kthread_num);
int num=1;
char kthread_name[15];

printk(KERN_INFO "Inside daemon_routine() \n");
sprintf(kthread_name,"kern_daemon_%d",num);
daemonize(kthread_name);
allow_signal(SIGKILL);
allow_signal(SIGTERM);

do{
if(!down_trylock(&mutex)){
printk(KERN_INFO "kernel_daemon [%d] accessing the shared data\n",num);
data.num=22;
up(&mutex);
printk(KERN_INFO "kernel_daemon [%d] released the mutex\n",num);
}

printk(KERN_INFO "kernel_daemon [%d] waiting to acquire mutex\n",num);
set_current_state(TASK_INTERRUPTIBLE);
schedule_timeout(10*HZ);

}while(!signal_pending(current));
return 0;
}


int _init_(void)
{
int kthread_num =1;
printk(KERN_INFO"starting the first kernel thread\n");
kthread_pid1 = kernel_thread(kthread_routine_1,NULL,0);
if(kthread_pid1< 0 ){
printk(KERN_ALERT "Kernel thread [1] creation failed\n");
return -1;
}
kthread_num=2;
printk(KERN_INFO"starting the second kernel thread\n");
kthread_pid2 = kernel_thread(kthread_routine_2,NULL,0);
if(kthread_pid2 < 0 ){
printk(KERN_ALERT "Kernel thread [2] creation failed\n");
return -1;
}


return 0;
}

void _fini_(void)
{
printk(KERN_INFO"kernel daemon [1] terminating\n");
if(kill_proc(kthread_pid1,SIGTERM,1) == -ESRCH){
printk(KERN_ERR "kernel thread pid::%d already dead\n",kthread_pid1);
init_MUTEX(&mutex);
}
printk(KERN_INFO"kernel daemon [2] terminating\n");
if(kill_proc(kthread_pid2,SIGTERM,1) == -ESRCH){
printk(KERN_ERR "kernel thread pid::%d already dead\n",kthread_pid2);
init_MUTEX(&mutex);
}
}

module_init(_init_);
module_exit(_fini_);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Demonstrates use of mutex");
MODULE_AUTHOR("Milind A Choudhary");

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: