LINUX TIPS AND TRICKS --- January 19, 2001

Published by ITworld.com -- changing the way you view IT
http://www.itworld.com/newsletters

________________________________________________________________________________

Spin locks

While many kernel code fragments can be executed simultaneously by 
different tasks, certain critical code sections in the kernel (e.g., 
adding a new entry to the list of processes) must be locked for 
exclusive execution -- executed by a single task at a time. For this 
purpose, the kernel sets a special flag to -1 just before entering such 
a critical code section and resets it to 0 once it leaves that critical 
code section. Any other task wishing to execute this code fragment must 
acquire a lock, i.e., check that flag first. If its value isn't 0, then 
the task waits until the flag clears. This synchronization mechanism is 
called a mutex or, in kernel parlance, a spin lock. There are two types 
of spin locks: adaptive and spin.

Spinning and Blocking

When a task attempts to acquire a lock held by another task, it can 
either block or spin. "Spinning" means executing a tight loop that 
attempts to acquire a lock on each iteration. "Blocking" means that the 
task waiting for the lock is put to sleep until the lock is released. 
Each option offers pros and cons. Putting a task to sleep removes its 
context from the processor's cache. When the task awakens, its context 
has to be retrieved from the RAM. RAM access is significantly slower 
than cache access. By contrast, spinning ensures the context of the 
task attempting to acquire the lock remains in the cache. Although 
precious CPU time is wasted iterating through the tight loop, the 
overhead of putting it to sleep and waking it later is avoided.

Adaptive spin locks

In some cases, predicting the more suitable approach is easy. For 
instance, an I/O lock is usually held for a long time; therefore, 
blocking a task trying to acquire an I/O lock is a reasonable decision. 
That said, the Linux kernel has thousands of critical code sections. 
Predicting the more suitable approach (i.e., blocking or spinning) at 
runtime is not always possible. Adaptive spin locks solve this dilemma. 
An adaptive spin lock selects the optimal approach dynamically. For 
instance, if a task is waiting for another task to release a lock, then 
the kernel first checks the lock owner's status. If it's running, then 
the task waiting for the lock will spin, assuming that the lock will be 
released soon. On the other hand, if the task holding the lock is 
sleeping, the task waiting for that lock will block because it's likely 
that releasing the lock will take much longer.

________________________________________________________________________________

ADDITIONAL RESOURCES

Kernel synchronization primitives

http://www.unixinsider.com/jsw/lintps_nl/swol-09-1999/
swol-09-insidesolaris.html

Unlocking Solaris 
The Solaris kernel implements several types of synchronization 
primitives to protect critical data in the kernel.

http://www.unixinsider.com/jsw/lintps_nl/swol-10-1999/
swol-10-insidesolaris.html

Unlocking the kernel 
How you can monitor mutex contention, and decide what to do when it 
becomes a problem

http://www.unixinsider.com/jsw/lintps_nl/swol-08-1998/swol-08-perf.html

Customizing the FreeBSD Kernel 
FreeBSD for the Linux administrator

http://www.linuxworld.com/jlw/lintps_nl/lw-2000-03/lw-03-freebsd.html

The story of the Linux kernel 
Linus Torvalds explains what makes the Linux kernel great

http://www.linuxworld.com/jlw/lintps_nl/lw-1999-03/lw-03-opensources.html

________________________________________________________________________________

COMMUNITY DISCUSSIONS

Hone your Linux development skills, share your expertise, and put out 
the occasional call for help in this discussion for programmers of all 
levels. Talk to Danny Kalev about the ins and outs of the new Linux 
kernel.

http://www.itworld.com/jump/lintps_nl/forums.itworld.com/
webx?14@@.ee6b652/213!skip=143

Linux is making its presence felt on the desktop, but is it stable and 
polished enough for prime time? Talk distributions, window managers, 
themes, and desktop politics here.

http://www.itworld.com/jump/lintps_nl/forums.itworld.com/
webx?14@@.ee6b663/335!skip=277

Got a question about Linux setup, tuning, security, or maintenance? 
Hackers, newbies, and all the geeks in between knock heads and devise 
solutions in this discussion moderated by LinuxWorld.com's resident geek

http://www.itworld.com/jump/lintps_nl/forums.itworld.com/
webx?14@@.ee6c981/281!skip=233

________________________________________________________________________________

CONTACTS

* For editorial comments, write Andrew Santosusso, Associate Editor, 
  Newsletters at: andrew_santosusso@itworld.com

* For advertising information, write Dan Chupka, Account Executive at:
  dan_chupka@itworld.com

* For recruitment advertising information, write Jamie Swartz, Eastern
  Regional Sales Manager at: jamie_swartz@itworld.com or Paul Duthie,
  Western Regional Sales Manager at: paul_duthie@itworld.com

* For all other inquiries, write Jodie Naze, Product Manager, 
  Newsletters at: jodie_naze@itworld.com

________________________________________________________________________________

PRIVACY POLICY
http://www2.itworld.com/CDA/ITW_Privacy_Policy 

Copyright 2001 ITworld.com, Inc., All Rights Reserved.

http://www.itworld.com
