[Solved] CSCI215 Lab5 -Threads

$25

File Name: CSCI215_Lab5_-Threads.zip
File Size: 197.82 KB

SKU: [Solved] CSCI215 Lab5 -Threads Category: Tag:
5/5 - (1 vote)

Make your first multithreaded 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 mycodewithpthreads.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 < N1) 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/51ce875553814dd5bae58ae3b3c862d0/Worksheets/SkeletonCode/lab05ex4barrierskel.c) so that it forces a rendezvous 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

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] CSCI215 Lab5 -Threads
$25