Thread Programming

Laboratory MP7: Linux Pthreads      January 25, 2010

Read section 4.3.1 (Pthreads) of your Silberschatz textbook and study the example in Figure 4.6.  An explanation of Linux NPTL pthreads is given in "man 7 pthreads". The syntax of pthread_attr_init(), pthread_create(), and pthread_join are explained in their Section 3 man pages. That is, give the command

man 3 pthread_create

to get the correct manpage of the function pthread_create().

Write a C program that creates four (4) threads, which we will call thread-0 to thread-3. Thread-0 will compute terms 0, 4, 8, etc of the arctan() function.  Thread-1 will compute terms 1, 5, 9, etc. Thread-2 will compute terms 2, 6, 10, etc. Thread-3 will compute terms 3, 7, 11, etc.  The arctan() function is given by the infinite series:
 
Term no.    0      1       2      3     etc

                  x^3     x^5    x^7
arctan(x) = x -   ___  +  ___ -  ___ +  etc
                   3       5      7

So thread-0 will compute the terms with x, x^9, x^17, etc.  Each thread will use the type "long double" for all computations. Each thread will compute enough terms assigned to them until the term last computed has absolute value less than 10^(-18). Do not compute the x^n by using pow(), since you do not know how accurate pow() is. Instead it is best to compute iteratively from the previous product.For example compute x^17 from x^9 as follows:

v = value of x^9;
y=x*x; z=y*y; w=z*z;
value of x^17 = v * w;

The parent program will open a file /tmp/threads.out for writing. In this file, thread-0, thread-1, thread-2, thread-3, will print their thread IDs, as follows:

thread-0   THREAD0_ID
thread-1   THREAD1_ID
etc

For each term computed, each thread will print a line to this file containing: thread-k (0,1,2,3), the term number, and the value of the term, expressed to 20 decimal places.
 
There will be a global variable

long double sum[4];

so that thread-0 can save its computed sum in sum[0], thread-1 in sum[1], etc.  The parent process (main thread) will take care of forming the sum:

arctan(x) = sum[0] + sum[1] + sum[2] + sum[3];

and outputting the value of arctan(x) to stdout. 

The value of x will be input to the program as command line input argv[1].

The reason we are using threads to compute the infinite series for arctan(x) is that this infinite series converges very slowly, so that if we split the work into different threads, the OS may assign the computation for a threads to different processors in multi-processor architectures like Intel Core2 Duo or Quad. This way, computation is done much more quickly.

Deadline for submission:

January 25, 2010, 4:30 PM, by email to prmanalastas@gmail.com.
Submit the usual file lastnameFirstname-mp7-threads.zip as an email attachment.
 

Sign in  |  Recent Site Activity  |  Terms  |  Report Abuse  |  Print page  |  Powered by Google Sites