<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
   <TITLE>Linux Threads Home Page: How is threading different from tasks using shared memory?</TITLE>
   <META NAME="Author" CONTENT="Sean Walton">
   <META NAME="GENERATOR" CONTENT="Mozilla/3.0Gold (WinNT; U) [Netscape]">
</HEAD>
<BODY Background="GraySea.gif">

<P><FONT SIZE=+3>How is threading different from tasks using shared memory?</FONT></P>

<P>The obvious question many ask is: what is the difference between threading
and forks with shared memory. Well, these tables hopefully help see the
difference.</P>

<TABLE BORDER=4 CELLSPACING=2 >
<CAPTION>
<P><FONT SIZE=+2>Threading</FONT></P>
</CAPTION>

<TR>
<TD>Common Memory</TD>

<TD>Threads naturally have shared data regions.</TD>
</TR>

<TR>
<TD>Locks</TD>

<TD>Not inherent. No protection. This is the real power: you only lock
what you want to own.</TD>
</TR>

<TR>
<TD>Switching</TD>

<TD>Fast. Since the data is inherently shared, the context switches don't
have to flush all the memory management buffers. On other systems (non-linux),
this can be very fast. However, I must point out that the benchmarks done
on Linux vs. NT and others have clearly placed Linux in the context-switching
lead. Private data - No such thing. If you clone() with data and stack
shared, all subsequent memory allocations can be seen by other threads.
I well-behaved program will naturally shadow this, making it a non-issue.</TD>
</TR>

<TR>
<TD>SMP</TD>

<TD>Threads will ensure 100% tight-coupled SMP. Your program will always
work from platform to platform.</TD>
</TR>
</TABLE>

<P></P><P></P>
<TABLE BORDER=4 CELLSPACING=2 >
<CAPTION>
<P><FONT SIZE=+2>Forks with Shared Memory</FONT></P>
</CAPTION>

<TR>
<TD>Common Memory</TD>

<TD>Have to be specially created and initialized.</TD>
</TR>

<TR>
<TD>Locks</TD>

<TD>Somewhat inherent. You have to give/take permission to write to a block.
This is done via &quot;whole blocks&quot;--you can't have one shared memory
block and lock only a piece of it.</TD>
</TR>

<TR>
<TD>Switching</TD>

<TD>Full context reload. This is bad. Private Data - Inherent. </TD>
</TR>

<TR>
<TD>SMP</TD>

<TD>May cause problems if not 100% tight-coupled SMP. You can actually
have your program run on a distributed system (via network). From thence,
shared memory calls WILL FAIL, because the tasks will not be talking about
the share memory IDs. On a distributed system you cannot 100% ensure same-memory
processing (your process may migrate to an unused CPU across the net).</TD>
</TR>
</TABLE>

<P>
<HR SIZE=4 WIDTH="100%"></P>

<CENTER><TABLE CELLSPACING=0 CELLPADDING=0 >
<TR>
<TD>
<CENTER><P><A HREF="./AvoidZombies.html"><IMG SRC="ArrowLeft.gif" ALT="[Left Arrow]" BORDER=0 HEIGHT=40 WIDTH=40></A></P></CENTER>
</TD>

<TD>
<CENTER><P><A HREF="./index.html"><IMG SRC="House.gif" ALT="[Home]" BORDER=0 HEIGHT=40 WIDTH=40></A></P></CENTER>
</TD>

<TD>
<CENTER><P><A HREF="./Dictionary.html"><IMG SRC="BookSearch.gif" ALT="[Book Search]" BORDER=0 HEIGHT=40 WIDTH=40></A></P></CENTER>
</TD>

<TD>
<CENTER><P><A HREF="mailto:walton@oclc.org"><IMG SRC="MailBox.gif" ALT="[Mailbox]" BORDER=0 HEIGHT=40 WIDTH=40></A></P></CENTER>
</TD>

<TD>
<CENTER><P><A HREF="./ThreadingApps.html"><IMG SRC="ArrowRight.gif" ALT="[Right Arrow]" BORDER=0 HEIGHT=40 WIDTH=40></A></P></CENTER>
</TD>
</TR>

<TR>
<TD><A HREF="./AvoidZombies.html">[Previous Page]</A></TD>

<TD><A HREF="./index.html">[First Page]</A></TD>

<TD><A HREF="./Dictionary.html">[Dictionary]</A></TD>

<TD><A HREF="mailto:walton@oclc.org">[Email Author]</A></TD>

<TD><A HREF="./ThreadingApps.html">[Next Page]</A></TD>
</TR>
</TABLE></CENTER>

</BODY>
</HTML>
