spin locks

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,
   I am writing a module to test the spin locks.In that i am creating 2
threads.In that 2 threads i am taking a value and incrimenting upto some
value and printing.
But with out using spin lock the code is properly working.(i.e)the first
thread is executed for some time and after that the 2nd thread preempts
the first and it is also executing for some time.

but if i use spinlock and printing part i locked.
Here actually after the thread1 teriminated then only the 2nd thread has
to execute.But it is not happening.Simply the system hangs.

I am listing the code below.
Any one help me.
thanks in  Advance



 
#ifndef __KERNEL__
	#define __KERNEL__
#endif

#ifndef MODULE
	#define MODULE
#endif

#include <linux/kernel.h> 
#include <linux/init.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <asm/atomic.h>
#include <asm/processor.h>
#include <linux/sched.h>
#include <linux/spinlock.h>
MODULE_AUTHOR("RAJA");
MODULE_DESCRIPTION("SPIN LOCK 1");
MODULE_LICENSE("GPL");

static int __init init_module_spinlock(void);
static void __exit exit_module_spinlock(void);

//unsigned long value=0;

spinlock_t my_lock = SPIN_LOCK_UNLOCKED;

int thread_fun1(void * data)
{
	printk("Entered Into Thread Fun1\n\n");
	spin_lock(&my_lock);
	int i=0;
	for(i=1;i<1000000;i++)
	{
		schedule();
		if(i%1000 == 0)
		printk("%d\t",i);
	}
	spin_unlock(&my_lock);
	printk("returned from Thread Fun1\n\n");
	return 0;
}

int thread_fun2(void * data)
{
	printk("Entered Into Thread Fun2\n\n");
	spin_lock(&my_lock);
	int i=0;
	for(i=1;i<1000000;i++)
	{
		schedule();
		if(i%1000 == 0)
		printk("%d\t",i);
	}
	spin_unlock(&my_lock);
	printk("returned from Thread Fun2\n\n");
	return 0;
}

static int __init init_module_spinlock()
{
	
	printk("Entered Into init function\n");
	pid_t thread1 = kernel_thread(thread_fun1,NULL,0);	
	pid_t thread2 = kernel_thread(thread_fun2,NULL,0);	
	printk("Exited From init function\n");
	return 0;
}

static void __exit exit_module_spinlock()
{
	printk("Entered Into exit function\n");
	printk("Returned From exit function\n");
}

module_init(init_module_spinlock);
module_exit(exit_module_spinlock);



--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux