The task consists of a memory (code, globals, heap), OS info, and threads. Each thread is a unit of execution, which consists of a stack and CPU state (i.e. registers). Multiple threads resemble multiple processes, except that multiple threads within a task use the same code, globals and heap. Thus, while two processes in Unix can only communicate through the operating system (e.g. through files, pipes, or sockets), two threads in a task can communicate through memory.
When you program with threads, you assume that they execute simultaneously. In other words, it should appear to you as if each thread is executing on its own CPU, and that all the threads share the same memory.
There are various primitives that a thread system must provide. Let's start with two basic ones. In this initial discussion, I am talking about a generic thread system. We'll talk about specific ones (such as POSIX and Solaris threads) later.
This says to create a new thread which runs the given procedure with the given arguments. Sometimes the arguments are omitted, and sometimes only one argument (a (void *)) is allowed. It returns a pointer to the new thread (which I'll call a thread control block or TCB).
This says to wait for the thread represented by tcb to finish executing. Often thread_join() returns an integer or a (void *) as its exit value. You can think of thread_join() as analogous to wait() in Unix --- it waits for the specified thread to complete, and gathers information about the thread's exit status.
To make use of Posix threads in your program, you need to have the following include directive:
#include < pthread.h >And you have to link libpthread.a to your object files. (i.e. if your program is in main.c, you need to do the following to make your thread executable):
UNIX> cc -c main.c UNIX> cc -o main main.o -lpthreadYou can use gcc too. There's a lot of junk in the pthread library. You can read about it in the various man pages. Start with ``man pthreads''. The two basic primitives defined above are the following in Posix threads:
int pthread_create(pthread_t *new_thread_ID, const pthread_attr_t *attr, void * (*start_func)(void *), void *arg); int pthread_join(pthread_t target_thread, void **status);This isn't too bad, and not too far off from my generic description above. Instead of returning a pointer to a thread control block, pthread_create() has you pass the address of one, and it fills it in. Don't worry about the attr argument -- just use NULL. Then func is the function, and arg is the argument to the function, which is a (void *). When pthread_create returns, the TCB is in *new_thread_ID, and the new thread is running func(arg).
pthread_join() has you specify a thread, and give a pointer to a (void *). When the specified thread exits, the pthread_join() call will return, and *status will be the return or exit value of a thread.
In all the Posix thread calls, an integer is returned. If zero, everything went ok. Otherwise, an error has occurred. As with system calls, it is always good to check the return values of these calls to see if there has been an error. In my code here in the lecture notes, I'll omit error checking, but it is in the files, and you should do it.
How does a thread exit? By calling return or pthread_exit().
Ok, so check out the following program (in hw.c):
#include < pthread.h > #include < stdio.h > void *printme() { printf("Hello world\n"); return NULL; } main() { pthread_t tcb; void *status; if (pthread_create(&tcb, NULL, printme, NULL) != 0) { perror("pthread_create"); exit(1); } if (pthread_join(tcb, &status) != 0) { perror("pthread_join"); exit(1); } }Try copying hw.c to your home area, compiling it, and running it. It should print out ``Hello world''.
Here's the output of print4.c:
Trying to join with tid 0 Hi. I'm thread 0 Hi. I'm thread 1 Hi. I'm thread 2 Hi. I'm thread 3 Joined with tid 0 Trying to join with tid 1 Joined with tid 1 Trying to join with tid 2 Joined with tid 2 Trying to join with tid 3 Joined with tid 3So what happened is the following. The main() program got control after forking off the four threads. It called pthread_join for thread zero and blocked. Then thread zero got control, printed its line, and exited. Next came threads one, two and three. When they finished, the main() thread got control again and since thread zero was done, its pthread_join() call returned. Then it made the pthread_join() calls for threads one, two and three, all of which returned since these threads were done. When main() returns, all the threads are done, and the program exits.
Here, all threads, including the main() program exit with pthread_exit(). You'll see that the output is the same as print4.
Now, look at p4b.c. Here, we put a pthread_exit() call in main() before making the join calls. The output is:
Hi. I'm thread 0 Hi. I'm thread 1 Hi. I'm thread 2 Hi. I'm thread 3You'll note that none of the "Joining" lines were printed out because the main thread had exited. However, the other threads ran just fine, and the program terminated when all the threads had exited.
The second thing you need to know is that when a forked thread returns from its initial calling procedure (e.g. printme in print4.c, then that is the same as calling pthread_exit(). However, if the main() thread returns, then that is the same as calling exit(), and the task dies. That's why there is no output in p4c.c. Threads 0 through 3 have been forked when the main thread exits, but they haven't run yet. When the main thread returns, the task is terminated, and thus the threads do not run.
Finally, look at p4d.c. Here, the threads call exit() instead of pthread_exit(). You'll note that the output is:
Trying to join with tid 0 Hi. I'm thread 0This is because the task is terminated by thread 0's exit() call.
We will talk more about preemption later.
A non-preemptive thread system on a system with a single CPU (called a "uniprocessor") may seem useless, but in actuality it is extremely useful. On a multiprocessor system (i.e. multiple CPU's attached to one memory), it should be obvious that you can use threads to achieve parallel speedup on your programs.