Lab Assignment--Threads and Mutexes


Lab Code

To get the sample and exercise code, please use the following commands in your cs330 directory:
  curl https://www.labs.cs.uregina.ca/330/Threads/Lab5.zip -O -s
  unzip Lab5.zip

(Please do this lab on os1 or os2)

This week, we will be working with a countHeads function, which counts the number of 1's (representing heads) in an array of 1's and zeros. Your job is to convert the function from a regular function, written in C, to a threaded function. As a thread function, you will give it a portion of the array to work with, depending on the size of the array and the number of threads. Some of the code is provided for you already in main.c. You will notice that certain things are missing.

Change main

Change countHeads

Sample runs:

os1:~/cs330/Lab5$ ./demo
How many threads? 10
How big is the array (100 elements or more)?135
0 0 1 1 0 0 1 1 0 0 0 1 0 0 1 
1 0 0 1 1 0 1 1 0 0 0 0 1 1 1 
0 1 1 1 1 0 0 0 1 0 0 1 0 0 1 
1 1 0 1 0 1 1 1 1 1 1 1 1 0 1 
1 1 0 0 0 1 0 0 1 0 1 1 1 1 1 
1 0 0 1 1 0 0 0 1 1 1 0 1 0 1 
0 1 0 0 0 0 0 0 1 1 0 0 1 0 1 
0 1 1 1 0 0 1 0 0 1 0 1 1 1 1 
0 1 1 0 1 1 1 1 1 0 1 0 0 0 0 

Checking result: 72
Start:0 End:13
Start:13 End:26
Start:26 End:39
Start:39 End:52
Start:52 End:65
Start:65 End:78
Start:78 End:91
Start:91 End:104
Start:104 End:117
Start:117 End:135
The number of heads coin tosses is: 72

os1:~/cs330/Lab5$ ./demo
How many threads? 5
How big is the array (100 elements or more)?100
0 0 1 0 0 0 1 1 1 1 0 1 0 0 0 
1 1 0 0 1 1 0 1 0 1 0 1 0 0 1 
0 0 1 1 1 0 1 0 1 0 1 1 0 1 1 
0 0 0 0 1 0 1 1 1 1 0 1 1 0 1 
0 1 0 1 0 1 1 0 1 0 0 1 1 0 0 
0 1 1 1 1 0 1 1 1 0 0 1 0 1 1 
1 1 0 1 1 1 0 0 1 0

Checking result: 54
Start:0 End:20
Start:20 End:40
Start:40 End:60
Start:60 End:80
Start:80 End:100
The number of heads coin tosses is: 54

Deliverables:

Submit 3 files to URCourses:

  1. The code (main.c and functions.c)
  2. A few sample runs captured in a script file (saved as .txt)