[SOLVED] CSCI215 Lab5 -Threads

35.00 $

Category:
Click Category Button to View Your Next Assignment | Homework

You will receive the following solution file(s) instantly after successful payment:

zip file icon 5-Threads-r80uqo.zip (163.2 KB)
Assignment Instructions Updated Recently? Submit Below and we will provide new Solution!
Submit New Instructions
🔒 Securely Powered by:
Secure Checkout
Rate this product

Make your first multi­threaded programs.

Implement basic synchronization among threads.

Remark

When asked to synchronize threads, please refrain from using tools that are outside the pthread library; for example unnamed semaphores.

Remember that compiling code that uses pthreads requires to link with the pthread library. You need to add option ­lpthread at the end of your compilation command.

  1. $gcc ­o test ­Wall my­code­with­pthreads.c ­lpthread

Exercise 1: First steps with threads

Write a program where the main thread creates N secondary threads, and passes their order of creation i (0 < i < N­1) as an argument of pthread_create.

Each secondary thread will execute a function thread_control which displays (a) the order of creation of the thread and (b) its identifier (tid), and then terminates and returns its order of creation multiplied by 2.

The main thread waits for the termination of every secondary thread and displays the termination value that was returned. When all secondary threads have terminated, the main thread completes its run.

 

Exercise 2: Mutual exclusion

Modify the program written for exercise 1 so that each secondary thread now executes a function rand_thread which generates a random value between 0 and 10 as follows:

random_val = rand() % 11;

Every secondary thread displays the random value it generated (random_val), adds it to a global variable initialized to zero by the main thread, and then terminates by calling:

pthread_exit(0);

After waiting for the termination of all secondary threads, the main thread displays the total sum of all the values generated by the secondary threads.

 

Exercise 3: Synchronization & detached threads

Modify the program written for exercise 2 so that the final display of the running total is performed by an extra secondary thread instead of the main thread. This extra secondary thread executes a function print_thread that blocks until all the other secondary threads have finished adding up the random values they generate. All secondary threads that perform rand_thread set their state to detached from the start. The last rand_thread call unblocks the print thread.

https://newclasses.nyu.edu/access/lessonbuilder/item/26532810/group/51ce8755-5381-4dd5-bae5-8ae3b3c862d0/Worksheets/Lab%20Worksheet%2005%20-%20Thr…   1/2 11/8/2018           Lab Worksheet 05 – Threads

 

Exercise 4: Synchronization by broadcast

Download and fill in the code of the program (https://newclasses.nyu.edu/access/content/group/51ce87555381­4dd5­bae5­8ae3b3c862d0/Worksheets/Skeleton­Code/lab05ex4­barrier­skel.c) so that it forces a rendez­vous point (RVP) between N secondary threads. When a thread reaches the RVP, it remains blocked until all others reach the RVP too.

If N is 3, the point is to get your program to display:

Before barrier

Before barrier

Before barrier

After barrier

After barrier

After barrier

  • 5-Threads-r80uqo.zip