LINUX TIPS AND TRICKS --- February 09, 2001

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

Processes and Threads
By Danny Kalev

This week, I will present the fundamentals of processes and threads in 
Linux, compare them to other operating systems, and draw some practical 
lessons.

In the Beginning
GUI-based systems advent (around 10 years ago) provided the impetus to 
switch from multiprocessing to multithreading. Classic operating 
systems, e.g. Solaris, associated a process with a private table of 
file descriptors, a unique PID, a table of signal handlers, and a 
private address space. Obviously, launching a new process (e.g., by 
calling fork() or a similar function) required considerable overhead. 
The child process would get its own copy of the parent's addressable 
region, table of file descriptors, and so on. From this limitation 
arose threads.

Unlike processes, threads run within the same address space and share 
their process' data. In such environments, the thread creation and 
destruction takes place considerably faster compared to a full-blown 
process' creation or destruction. Under Solaris, for example, launching 
a new thread is about 70 times faster than launching a new process. 
Linux, however, is radically different.

The Linux Approach
Under Linux, threads and processes are almost indistinguishable except 
for one thing: threads run within the same address space whereas 
processes have distinct address spaces. However, no differences exist 
between the two from a scheduler point-of-view. Thus, a context switch 
between two threads of the same process essentially jumps from one code 
location to another, plus setting a few CPU registers.

What happens when you launch a new process? Linux implements the copy-
on-write model, which leaves the mapped memory shared between a parent 
process and its child as long as the child doesn't alter the shared 
addressable region. Only when the child writes to the shared address 
space does the kernel allocate new storage. Hence, launching a new 
process in Linux involves significantly lower overhead compared to 
Solaris and other OSs.

Some Unix systems support the vfork() function to implement copy-on-
write, as opposed to plain fork(), which copies the entire parent's 
address space to a new region. However, under Linux fork()uses copy-on-
write anyway minimalizing the difference between the two functions. In 
fact, vfork() merely serves as a wrapper function that calls fork().

Practical Lessons
In this regard, Linux newcomers often are unaware of the substantial 
differences between Linux and other operating systems. To implement 
concurrency, they use multithreading exclusively, mistakenly assuming 
as high an overhead associated with Linux multiprocessing as on other 
platforms. However, this is not the case. In fact, many multithreaded 
applications ported from other platforms to Linux can benefit from 
replacing multithreading with multiprocessing; this will eliminate the 
overhead of critical sections and other locking mechanisms used in 
multithreaded applications.

About the author(s)
----------------
Danny Kalev is a system analyst and software engineer with more than 10 
years of experience, specializing in C++ and object-oriented analysis 
and design on various platforms including VMS, DOS, Windows, Unix, and 
Linux. His technical interests involve code optimization, networking, 
and distributed computing. He is also a member of the ANSI C++ 
standardization committee and the author of ANSI/ISO C++ Professional 
Programmer's Handbook (Que, 1999). Contact him at linuxnl@excite.com.

________________________________________________________________________________

ADDITIONAL RESOURCES

Threads libraries in Solaris 
New features for developing multi-threaded code

http://www.unixinsider.com/jsw/lintps_nl/swol-06-2000/
swol-06-insidesolaris.html

Peeling back the process layers, Part 1 
This month we'll take a look at the Solaris implementation 
of processes, threads, and lightweight processes

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

The Solaris process model, Part 2 
We peel back the next layer in our examination of Solaris processes and 
threads 
http://www.unixinsider.com/jsw/lintps_nl/swol-09-1998/swol-09-insidesolaris.html

It's time to wake up! 
Into another dimension of kernel thread support -- 
the mechanics of sleep and wakeup in Solaris

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

Tcl adds multithreading in bid for Enterprise Scripting 
language now ready for server-class applications, 
says Ousterhout

http://www.linuxworld.com/jlw/lintps_nl/lw-1999-05/lw-05-tcl.html

________________________________________________________________________________

COMMUNITY DISCUSSIONS

Linux Software Development
Hone your Linux development skills, share your expertise, and put out 
the occasional call for help in this discussion for programmers of all 
levels. Moderated by Danny Kalev.

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

Desktop Linux
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/391!skip=330

________________________________________________________________________________

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
