Objective: Writing programming assignments demonstrating the concepts of threads and multithreading. [CLO. 6, ABET: SO1, SO6]
Question: Multi-Threading and Synchronization (15 Points):
Consider the given C-like code below. Use <pthread.h> and <semaphore.h> libraries to create 3 threads as follows.
1. The first thread (name it decBytwo) will decrements all the elements of the MyArray [] by 2, then prints on the screen the new contents of the array with proper message. Note that this thread must run first before the other two threads.
2. The second thread (name it findNeg) will print on the screen all the negative numbers in the modified MyAray []. So any other number that is not negative will be omitted.
3. The third thread (name it SumPos) will do two things: first sum only the positive numbers in the modified MyArray[], and second, calculated the average of those numbers only. Next it should print both the summation value and the average with a proper message.
4. You must use semaphores to control a critical region and order or running threads.
5. In all threads, you must increment the variable counter each time you access an element of the array. Hint: add the instruction count++ inside each loop accessing the array. Make sure to protect simultaneous access to it. This instruction has been added in the threads code below to remind you to use it and to print it at the end of the program.
Page 1 of 2