Chapter 1 - Why Threads
Chapter 1 - Why Threads
Pthreads Programming Bradford Nichols, Dick Buttlar and Jacqueline Proulx Farrell Copyright 1996 O'Reilly & Associates, Inc.
for (j = 0; j < 10000; j++) x = x + i; (*pnum_times)++; } } void do_another_thing(int *pnum_times) { int i, j, x; for (i = 0; i < 4; i++) { printf("doing another \n"); for (j = 0; j < 10000; j++) x = x + i; (*pnum_times)++; } } void do_wrap_up(int one_times, int another_times) { int total; total = one_times + another_times; printf("wrap up: one thing %d, another %d, total %d\n", one_times, another_times, total); } Figure 1-1 shows the layout of this program in the virtual memory of a process, indicating how memory is assigned and which resources the process consumes. Several regions of memory exist: A read-only area for program instructions (or "text" in UNIX parlance) A read-write area for global data (such as the variables r1 and r2 in our program)
Figure 1-1: The simple program as a process A heap area for memory that is dynamically allocated through malloc system calls A stack on which the automatic variables of the current procedure are kept (along with function arguments and other information needed to link it to the procedure that called it), just below similar information for the procedure that called it, just below similar information for the procedure that called it, and so on and so on. Each of these procedure-specific areas is known as a stack frame, and one exists for each procedure in the program that remains active. In the stack area of this illustration you can see the stack frames of our procedures do_one_thing and main. To complete our inventory of system resources needed to sustain this process, notice: Machine registers, including a program counter (PC) to the currently executing instruction and a pointer (SP) to the current stack frame Process-specific include tables, maintained by the operating system, to track systemsupplied resources such as open files (each requiring a file descriptor), communication end points (sockets), locks, and signals Figure 1-2 shows the same C program as a process with a single thread. Here, the machine registers (program counter, stack pointer, and the rest) have become part of the thread, leaving the rest as the process. As far as the outside observer of the program is concerned, nothing much has changed. As a process with a single thread, this program executes in exactly the same way as it does when modeled as a nonthreaded process. It is only when we design our program to take advantage of multiple threads in the same process that the thread model really takes off.
Figure 1-2: The simple program as a process with a thread Figure 1-3 shows our program as it might execute if it were designed to operate in two threads in a single process. Here, each thread has its own copy of the machine registers. (It's certainly very handy for a thread to keep track of the instruction it is currently executing and where in the stack area it should be pushing and popping its procedure-context information.) This allows Thread 1 and Thread 2 to execute at different locations (or exactly the same location) in the program's text. Thread 1, the thread in which the program was started, is executing do_one_thing, while Thread 2 is executing do_another_thing. Each
converted by Web2PDFConvert.com
thread can refer to global variables in the same data area. (do_one_thing uses r1 as a counter; do_another_thing uses r2.) Both threads can refer to the same file descriptors and other resources the system maintains for the process.
converted by Web2PDFConvert.com