Online Book Reader

Home Category

High Performance Computing - Charles Severance [83]

By Root 1392 0
executes, and you can see its output.[53]

The parent wakes up after a brief two-second sleep and notices that its copies of global and local variables have not been changed by the action of the child process. The parent then calls the wait( ) function to determine if any of its children exited. The wait( ) function returns which child has exited and the status code returned by that child process (in this case, process 19336).


User Space Multithreading

A thread is different from a process. When you add threads, they are added to the existing process rather than starting in a new process. Processes start with a single thread of execution and can add or remove threads throughout the duration of the program. Unlike processes, which operate in different memory spaces, all threads in a process share the same memory space. Figure 3.17 shows how the creation of a thread differs from the creation of a process. Not all of the memory space in a process is shared between all threads. In addition to the global area that is shared across all threads, each thread has a thread private area for its own local variables. It’s important for programmers to know when they are working with shared variables and when they are working with local variables.

When attempting to speed up high performance computing applications, threads have the advantage over processes in that multiple threads can cooperate and work on a shared data structure to hasten the computation. By dividing the work into smaller portions and assigning each smaller portion to a separate thread, the total work can be completed more quickly.

Multiple threads are also used in high performance database and Internet servers to improve the overall throughput of the server. With a single thread, the program can either be waiting for the next network request or reading the disk to satisfy the previous request. With multiple threads, one thread can be waiting for the next network transaction while several other threads are waiting for disk I/O to complete.

The following is an example of a simple multithreaded application.[54] It begins with a single master thread that creates three additional threads. Each thread prints some messages, accesses some global and local variables, and then terminates:

#define_REENTRANT /* basic lines for threads */

#include

#include

#define THREAD_COUNT 3

void *TestFunc(void *);

int globvar; /* A global variable */

int index[THREAD_COUNT] /* Local zero-based thread index */

pthread_t thread_id[THREAD_COUNT]; /* POSIX Thread IDs */

main() {

int i,retval;

pthread_t tid;

globvar = 0;

printf("Main - globvar=%d\n",globvar);

for(i=0;iindex[i] = i;

retval = pthread_create(&tid,NULL,TestFunc,(void *) index[i]);

printf("Main - creating i=%d tid=%d retval=%d\n",i,tid,retval);

thread_id[i] = tid;

}

printf("Main thread - threads started globvar=%d\n",globvar);

for(i=0;iprintf("Main - waiting for join %d\n",thread_id[i]);

retval = pthread_join( thread_id[i], NULL ) ;

printf("Main - back from join %d retval=%d\n",i,retval);

}

printf("Main thread - threads completed globvar=%d\n",globvar);

}

void *TestFunc(void *parm) {

int me,self;

me = (int) parm; /* My own assigned thread ordinal */

self = pthread_self(); /* The POSIX Thread library thread number */

printf("TestFunc me=%d - self=%d globvar=%d\n",me,self,globvar);

globvar = me + 15;

printf("TestFunc me=%d - sleeping globvar=%d\n",me,globvar);

sleep(2);

printf("TestFunc me=%d - done param=%d globvar=%d\n",me,self,globvar);

}

Figure 3.17. Creating a thread


The global shared areas in this case are those variables declared in the static area outside the main( ) code. The local variables are any variables declared within a routine. When threads are added, each thread gets its own function call stack. In C, the automatic variables that are declared at the beginning of each routine are allocated on the stack. As each thread enters a function, these variables are separately allocated on that particular

Return Main Page Previous Page Next Page

®Online Book Reader