Re: confusion about locking [ Pls help ]

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

 



Hi!

On 19:42 Sat 11 Apr     , Er Krishna wrote:
> Hi All,
> 
> Can somebody help me here to understand  following basic scenario about
> locking. Sorry if I am wrong in putting my points in correct manner.
> 
> 1. Can we take semaphore (binary semaphore or mutex) if the critical
> section data is shared across two  different threads/processes which are
> running on two different processors simultaneously/parallely ? I know
> spin_lock() can handle this scenario, but I was trying to understand can
> semaphore also handle this scenario, assuming there is no interrupt and
> only process context is there ? I think yes.  Pls confirm me on this and if
> not then why not ? If any pross and cons are involved pls tell that also.

Please do not use semaphores for mutual exclusion. Mutexes ensures that only
one "thread" can enter a critical section at the same time.

> 2. If the critical section data is shared across process and interrupt (
> consider this scenario on uniprocessor machine  and I have not taken
> local_irq_save() or any spin_lock_irqsave )  and in the currently executing
> process  critical section is protected by  preempt_disable()  and suddenly
> an interrupt occurs which also wants to access same critical section data,
> so what will happen ? Will in  this case Interrupt handler  acquire the
> critical section data and corrupt the kernel or it will sleep and corrupt
> the kernel or it will keep doing busy waiting ? I think interrupt handler
> will keep doing busy waiting and machine will stuck, kind of deadlock. Pls
> confirm me.

The idea behind spinlocks is that the process obtaining the lock cannot be
interrupted. An interrupt on the same CPU will be delayed until the critical
section is finished. An interrupt on another CPU trying to obtain the same
spinlock will busywait. This should not take too long, because the task
executing it cannot be preempted ond should not be loo long either.

> 3. Does Preemption gets disable ( on local processor ) in case of binary
> semaphore or mutex ? I think no. Pls confirm.

Semophore: I am not sure, but I think preemption does get disabled.
Mutex: Preemption will not get disabled.

> Second,  Lets say critical
> section is shared across process and interrupt and it is locked by
> semaphore (binary semaphore or mutex) in process context on  uniprocessor
> machine.  If Interrupt comes then what will happen ? Will it preempt the
> process

Yes, it will.

> and acquire the critical section of data and corrupt it or
> interrupt will go into sleep mode and machine can  crash/stuck. Pls explain.

You cannot acquire a mutex in any context which cannot sleep (this includes
interrupts, softirq, tasklets, ...). Use spinlocks instead.

	-Michi
-- 
programing a layer 3+4 network protocol for mesh networks
see http://michaelblizek.twilightparadox.com

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@xxxxxxxxxxxxxxxxx
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies




[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