[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: semaphore and spin locks
atul wrote (Dienstag, 10. Oktober 2000 03:25):
> Afa i know semaphores provide a very good mechanism for
> synchronization so why spin locks are needed n how they differ from
> semaphores..??
I'm just a kernelnewbie, so correct me if I'm wrong:
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.
A semaphore on the other hand is a more complicated mechanism consiting
of a count, a wait-queue and a lock. Processes which wait for a
semaphore don't do busy waiting, but have to do a context switch, which
takes some time. So a simple spin lock can be more efficient if you
expect that there is a good chance that no process has to wait long for
the lock. But you have to use it with care.
Thomas.
-
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/