#include <linux/fs.h>
#include <linux/sched.h>
#include <linux/kthread.h>
#include <linux/module.h>
#include <linux/delay.h>
static int i=0,j=100;
struct Task_struct *mythread1=null;
struct Task_struct *mythread2=null;
static int myVar = 0;
static int count = 0;
spinlock_t lock;
static void Setmyvar (int input)
{
Spin_lock (&lock);
if (count)
{
PRINTK ("Busy setmyvar\n");
}
count++;
MyVar = input;
PRINTK ("Setmyvar is%d\n", MyVar);
Spin_unlock (&lock);
count--;
}
static int Getmyvar (void)
{
int res = 0;
Spin_lock (&lock);
if (count)
{
PRINTK ("Busy setmyvar\n");
}
count++;
res = MyVar;
PRINTK ("Getmyvar is%d\n", res);
Spin_unlock (&lock);
count--;
return 0;
}
static int print1 (void *data)
{
while (!kthread_should_stop ())
{
PRINTK ("This is thread1......\n");
Getmyvar ();
Setmyvar (i);
Ssleep (1);
i++;
}
return 0;
}
static int print2 (void *data)
{
while (!kthread_should_stop ())
{
PRINTK ("This is thread2......\n");
Getmyvar ();
Setmyvar (j);
Ssleep (1);
j + +;
}
return 0;
}
static int __init hello_init (void) {
Spin_lock_init (&lock);
MyThread1 = Kthread_run (Print1,null, "mythread1");
MyThread2 = Kthread_run (Print2,null, "mythread2");
return 0;
}
static void __exit
Hello_exit (void) {
if (MYTHREAD1)
{
PRINTK ("Kthread1 stop....\n");
Kthread_stop (MYTHREAD1);
Mythread1=null;
}
if (MyThread2)
{
PRINTK ("Kthread2 stop....\n");
Kthread_stop (MYTHREAD2);
Mythread2=null;
}
}
Module_init (Hello_init);
Module_exit (Hello_exit);
Module_license ("GPL");
Module_author ("Valerie Henson [email protected]");
Module_description ("\" Hello, world!\ "minimal module");
Module_version ("PRINTK");
Linux spin Lock