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

Re: semaphore and spin locks




On Sun, 5 Aug 2001, Ralph Corderoy wrote:

>
> Hi Werner,
>
> > > A spin lock is a primitive mechanism to prevent two or more
> > > processes accessing a certain data structure at the same time. The
> > > lock is an integer that's initially set to 0. A process which wants
> > > to enter the critical area has to set the lock to 1 using an atomic
> > > "if 0 then set 1"-operation. If the lock is in use, the process has
> > > to busy-wait, spinning in a tight loop. ==> CPU-time-consuming if
> > > you have to wait long, but very efficient if you don't have to
> > > wait.
> >
> > Spin locks only work in a multiprocessor environment.  If you use it
> > in a unprocessor environment, then the waiting process would keep
> > running in a spin and have no chance to release it and the whole
> > kernel would hang.
>
> When you say `process' do you mean a normal Unix process?  If so, the
> kernel will pre-empt it whilst it's in its busy loop and the other,
> spin-lock holding, process will get a chance to release it.
>
> Or does `process' mean something running as part of the kernel and not
> pre-emptable in this case?

Hi Ralph,

I should have said "thread" which runs in behalf of a user process. Or
kernel thread which runs in behalf of the kernel.
Spinlocks only work on a muli-processor system because a kernel code can
never be pre-empted. But on a SMP machine, 2 CPUs can access a code
simultaneously.
And when you look at the kernel code, you will see that spinlocks are a
no-ops function on a uni-processor system.

Werner


-
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
IRC Channel:   irc.openprojects.net / #kernelnewbies
Web Page:      http://www.kernelnewbies.org/