ECE 231 - Project 1

Due 1/22/02

Project submission instructions are at the bottom of this page.

Goal: Simple POSIX threads programming

Project Definition: Build a C program that spawns a number of threads, allows them to execute simultaneously, and then joins with them when they exit. The number of threads is specified via a command line argument. Each thread will execute for about 5 seconds and each second will print its thread number and which time interval has elapsed. See the example below.

% ./a.out 4
Thread 0 starting
Thread 1 starting
Thread 2 starting
Thread 3 starting
T3-1 T0-1 T2-1 T1-1 T0-2 T2-2 T1-2 T3-2 T0-3 T3-3 T2-3 T1-3 T0-4 T2-4 T3-4 T1-4 T0-5 
Thread 0 exiting
T3-5 
Thread 3 exiting
T2-5 
Thread 2 exiting
T1-5 
Thread 1 exiting

Create default, non-detached threads for this project. The textbook gives a very simple example (with a few mistakes) on page 140. Detailed Pthreads documentation can be found on the Sun Microsystems Documentation website.

A suggestion is that you build some simple, iterative time wasting function that takes the number of iterations as a parameter. You can pick a parameter that takes roughly a second on the system you are using (in a single thread) and have each thread run that 5 times. Make sure your algorithm is reentrant if you plan to use the same one for each thread.

Hints to make threads behave on the Suns (Solaris):

If you use cc, your command line must include -mt and -lpthread. My program compiles with:

cc -mt threads.c -lpthread -lm

if you use gcc, the -mt option does not work, but the rest is needed.

In addition, Solaris pthread default behavior is ugly, so to get the threads to actually spawn and run independently, add the following line after setting the default attributes:

pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); /* system-wide contention */

Type man pthread_create for more information.

Submission instructions:

You will turn in this assignment via email to the TA, Prasad. His email address is prasad@dream.eng.uci.edu

You MUST use an email subject line with the exact formatting below:

Subject: ECE231_Project1 student_id

where you put your student ID number in place of "student_id" - Extension students do not have to put a student ID number there.

Your submission should consist of two attachments to the email:

  1. Your C/C++ source code (not object code). If your program consists of more than one source file, include them all.
  2. A sample run of your program with 5 threads. You can either copy the text from a terminal window and put it in a file or use the script command. The script command gives you a new command prompt and stores all your activity into a text file called typescript. When you are done recording your typescript, the exit command stops recording. See man script for details.