July 12, 2011, 1:35 a.m.
posted by ersatz
In the traditional Unix model, when a process needs something performed by another entity, it forks a child process and lets the child perform the processing. Most network servers under Unix are written this way, as we have seen in our concurrent server examples: The parent accepts the connection, forks a child, and the child handles the client.
While this paradigm has served well for many years, there are problems with fork:
Threads help with both problems. Threads are sometimes called lightweight processes since a thread is "lighter weight" than a process. That is, thread creation can be 10–100 times faster than process creation.
All threads within a process share the same global memory. This makes the sharing of information easy between the threads, but along with this simplicity comes the problem of synchronization.
More than just the global variables are shared. All threads within a process share the following:
But each thread has its own
In this text, we cover POSIX threads, also called Pthreads. These were standardized in 1995 as part of the POSIX.1c standard and most versions of Unix will support them in the future. We will see that all the Pthread functions begin with pthread_. This chapter is an introduction to threads, so that we can use threads in our network programs. For additional details see [Butenhof 1997].