Re: How come two process simulteneously getting spin lock...
- From: "Kaz Kylheku" <kkylheku@xxxxxxxxx>
- Date: 22 Dec 2005 20:57:31 -0800
Mozis wrote:
> I've written a simple module in which I'm initializing a spinlock in
> init_module. Here I just want to see how come a processor spins when
> some other process have already got the spinlock and another process
> waits for it.
>
> In module open method, I'm getting a spinlock as spin_lock(&lock), and
> in release method it is spin_unlock(&lock). so at a time one process
> should get the lock while opening the related device file and in the
> mean time if another process tries to open the device file, it should
> wait.
Spinlocks are for locking PROCESSORS out against each other, not
processes. At least on a non-preemptable kernel. If there is only one
processor, then the non-preemptability of the kernel gives you a
critical section (as long as you don't call anything that yields the
CPU!).
> e.g. device_open(....)
> {
> spin_lock(&lock) ;
> }
> device_release(...)
> {
> spin_unlock(&lock) ;
> }
Ouch!
A much better idea is to return something like -EBUSY if the device can
only be opened by one process at a time, and someone already has it.
If you must implement a suspension, make sure it's an interruptible
sleep, so if the user gets tired of waiting for the process to open the
device, that process can at least be killed with a signal!
> But i dont know how, both files get the spinlock and wait for the user
> input. I'm confused how come another process gets the spinlock though
> first process already got the lock and have not unlocked it.
If you don't have a preemptable kernel, and there is only one
processor, these spinlock operations do nothing at all. All that holds
your critical section together is that you are not calling anything
that yields the CPU. They are needed for resolving data races when two
or more processors enter into the kernel. The spinlocks also have to do
something real in a kernel that allows preemption: they have to disable
preemption over the span of the critical section. That amounts to
nothing if your code returns from the system call or blocks.
.
- Follow-Ups:
- References:
- Prev by Date: Re: How come two process simulteneously getting spin lock...
- Next by Date: Re: What is the purpose of %fs?
- Previous by thread: Re: How come two process simulteneously getting spin lock...
- Next by thread: Re: How come two process simulteneously getting spin lock...
- Index(es):
Relevant Pages
|