Re: race condition

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

 



On Wed, Sep 21, 2005 at 18:14:21 +0530, raja wrote:
> Hi,
>    I am trying to trace the race condition by creating 2 threads in

Trace *which* race-condition?

> kernel space.   One thread incriments a global value and other
> decrements that value.
> But I am unable to trace the race condition.
> I am listing my code.Will you please help me.
> thanks in advance

In addition to things already said by others:
1) There is no race-condition in your code in non-preemptive
   uniprocessor kernel. The firtst thread will run and then the second
   will. No race-condition, see?
2) For preemptive or smp (which are euqivalent) there may, or may not be
   a race condition. Actually, it probably generates the code so there
   is no race condition in this particular case. The thing is, that
   global variables must be saved before function calls and reloaded
   after (because the function may modify it and the compiler does not
   know whether it will). So the compiler may generate either of:
   	movl value, %ax
	incl %ax
	movl %ax, value
   (sorry if the syntax is incorrect)
   or
   	inc value
   And I'd expect it to generate the later. Since instructions can't be
   interrupted, there would be no race-condition on preempt and on smp
   it depends on hardware whether it would mess up without the lock
   prefix. And even then, it would have to execute the instruction on
   both CPUs in parallel, which is still extremely unlikely.

> #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>
> 
> MODULE_AUTHOR("RAJA");
> MODULE_DESCRIPTION("ATOMIC 1");
> MODULE_LICENSE("GPL");
> 
> static int __init init_module_atomic(void);
> static void __exit exit_module_atomic(void);
> 
> int value=0;
> 
> int thread_function_1(void * data)
> {
> 	printk("Entered into Thread 1\n");
> 	int i;
> 	for(i=0;i<1000;i++)
> 	{
> 		udelay(1000);
> 		value++;
> 		printk("Value Is : %d\t",value);
> 	}
> 	printk("Exited From Thread 1\n");
> 	return 0;
> }
> 
> int thread_function_2(void * data)
> {
> 	printk("Entered into Thread 2\n");
> 	int i;
> 	for(i=0;i<1000;i++)
> 	{
> 		udelay(100);
> 		value--;
> 		printk("Value Is : %d\t",value);
> 	}
> 	printk("Exited From Thread 2\n");
> 	return 0;
> }
> 
> static int __init init_module_atomic()
> {
> 	printk("Entered Into init function\n");
> 	pid_t thread1;
> 	pid_t thread2;
> 	thread1 = kernel_thread(thread_function_1,NULL,0);
> 	thread2 = kernel_thread(thread_function_2,NULL,0);
> 	printk("Exited From init function\n");
> 	return 0;
> }
> 
> static void __exit exit_module_atomic()
> {
> 	printk("Entered Into exit function\n");
> 	printk("Returned From exit function\n");
> }
> 
> module_init(init_module_atomic);
> module_exit(exit_module_atomic);
> 
> 
> 
> 
> --
> Kernelnewbies: Help each other learn about the Linux kernel.
> Archive:       http://mail.nl.linux.org/kernelnewbies/
> FAQ:           http://kernelnewbies.org/faq/
> 
--
						 Jan 'Bulb' Hudec <bulb@xxxxxx>

Attachment: signature.asc
Description: Digital signature


[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