1) The purpose of this project is to put into practice the thread synchronization concepts discussed in Chapter 6. You will apply it to a shared data structure between the two pthreads of this project. The shared data structure will be a stack that is implemented with an array as shown in Chapter 6 Homework problem 7. The array will consist of integers. Pthread 1 will push all integers from 1 to 120 on to the stack while pthread 2 pops the integers from the stack in a prescribed order discussed further below.
2) You will implement the push(), pop(), and is_empty() functions for the stack from Chapter 6 Homework problem 7 with the ERROR statements replaced by sched_yield().
The stack array size is 20. As the stack is a shared data structure between the two pthreads, you have to ensure mutual exclusion access to the stack with entry and exit sections. The entry section to acquire a (mutex) lock will be implemented with the pthread_mutex_lock() function. The exit section (to release the lock) will be implemented with the pthread_mutex_unlock() function.
As an example of mutex usage, you define and initialize a pthread mutex as follows (error checking not shown):
pthread_mutex_t myLock; // define a mutex variable
pthread_mutex_init (&myLock, NULL); // initialize a mutex variable
You can acquire and release the locks as follows (error checking not shown):
pthread_mutex_lock (&myLock);
pthread_mutex_unlock (&mylock);
3) The Pthread 1 algorithm pushes the integer numbers from 1 to 120 in increasing order on to the stack. It will call sched_yield() after every 10 consecutive invocations of push(). It will also call sched_yield if the stack is full. Pthread 1 will also have its own integer variable (pushSum) to hold a running sum of the integers pushed on to the stack. No output to screen or to a file will occur in pthread 1.
4) The Pthread 2 algorithm pops the integers from the stack. If the stack is full, it will pop all integers from the stack and then call sched_yield(). If the stack is not full, it will pop just five integers and call sched_yield(). If there are less than five integers on the stack, it will pop all of them and then call sched_yield(). Pthread 2 will also have its own integer variable (popSum) to compute a running sum of integers popped from the stack. Pthread 2 will also have its own 120-element array (popOrder) to record the order in which integers were popped from the stack. No output to screen or to a file will occur in Pthread 2.
5) Your program will make use of the pthreads library functions discussed in lectures. You ARE permitted to use any C++ or C standard I/O library functions or cin or cout for the file output from the main-line thread in item (6) below.
6) OUTPUT: After pthreads 1 and 2 terminate, the main-line thread will write the following output to the filename given in the command line argument.
All 120 elements of Pthread 2 popOrder array
Value of pthread 1 pushSum
Value of pthread 2 popSum