Question: In this assignment, you will be writing a multithreaded program by modifying the file in the assignment tarfile assn3.tar.gz. You will demonstrate your knowledge of

In this assignment, you will be writing a multithreaded program by modifying the file in the assignment tarfile assn3.tar.gz. You will demonstrate your knowledge of the POSIX Pthreads library and how to create, destroy, and wait for threads. You will also demonstrate how to pass data between parent and child threads.

Details:

The program you will create will perform the calculation ( n1 * n2 ) / n3 on groups of three numbers. You only need to read a maximum count of 100 groups of 3 numbers, or 300 numbers, either from a file or from stdin. The numbers will not be passed on the command line. Your program will perform this calculation across the data using two threads: the main thread and a child thread. The first half of the numbers read will be calculated by the child thread. The second half of the numbers read will be calculated by the main thread after the child thread has started but before joining with the parent thread.

The main thread will read all the numbers into an integer array. The main thread will then create a child thread using the pthread_create(...) API, passing half of the integer array as a parameter to the thread. After the child thread has been launched, the parent thread will perform the calculation on the second half of the integer array, storing the results to be displayed after joining to the child thread.

The child thread will perform the calculation from three of the integers at a time from the first half of the array and store the result into a floating-point array. This floating-point array will be returned to the parent thread. This floating point array will not be allocated on the stack of the thread, but any other allocation method is fine. The child thread must not display any data. Your only output should be produced by the main thread, and only after waiting for the thread to complete.

The parent thread will receive the results via the pthread_join(...) API parameter. Even if the results are stored in a floating-point array that is local to the main thread, the pointer to that array must be passed into the return result variable of pthread_join(...) API. The parent thread will then display the results calculated by the child thread, followed by the results calculated by the main thread. No global variables are allowed for this assignment. Only one thread should be created, and that one thread should perform the first half of the calculations. All other calculations should be made by the main thread.

If there is only one set of three numbers to read, then the child thread should perform the calculation and the main thread should not calculate any.

Hints:

  • This assignment relies heavily on C pointers. Refer to these excellent set of notes

    Actions

    for more information. Also review Lecture video 0.9 on pointers.
  • In order to successfully use the pthreads APIs necessary to complete this assignment you will need to modify more than the assn3.c source code. You will also need to modify the Makefile to add an option to the CFLAGS variable. Check out the man page for the pthreads APIs you will be using.

Please fix all memory leaks before submitting your assignment.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!