Re: How come two process simulteneously getting spin lock...



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.

.



Relevant Pages

  • Re: SpinLock/Mutex : Difference ?
    ... you have to go to kernel anyway. ... I never once said that user mode code should in any way ... spinlock owner is guaranteed to never get preempted by another thread. ... thread/process may have to "spin" for a time, ...
    (microsoft.public.win32.programmer.kernel)
  • Re: SpinLock/Mutex : Difference ?
    ... you have to go to kernel anyway. ... I never once said that user mode code should in any way ... spinlock owner is guaranteed to never get preempted by another thread. ... the kernel level synchronization objects from the app level, ...
    (microsoft.public.win32.programmer.kernel)
  • Re: SpinLock/Mutex : Difference ?
    ... You seem to think that a spinlock is is something that only the kernel can ... But anyone can write an equivalent API for user mode, all you need to do is ...
    (microsoft.public.win32.programmer.kernel)
  • Re: SpinLock/Mutex : Difference ?
    ... A spinlock is NOT a "kernel mode mechanism" period. ... Is it not better to simply better to define a tny struct typedef "SpinLock" ...
    (microsoft.public.win32.programmer.kernel)
  • Re: disabling interrupts/scheduling in ioctl() handler
    ... >> That depends on the kernel version. ... > I believe a simple spin_lock will turn off preemption. ... Otherwise it could be modified to take a spinlock. ... Kasper Dupont -- der bruger for meget tid paa usenet. ...
    (comp.os.linux.development.system)