Project 5 Consumer Producer Problem Solution

$35.00 $30.80

Description

Description

 

Project ve will introduce you to POSIX synchronization primitives. You will rst x your code from homework four using the correct synchronization primitive. Now the correct output of the program should be 0.

 

For part two, you will need to implement the consumer-producer problem. In essence this problem is simply maintaining shared resources that are going to be used by many threads. Some threads will add items and some will consume items. It is your job to make sure each thread doesn’t negatively e ect the others.

 

 

Part 1 – Modifying pthread race.c

 

 

For the rst part of assignment ve you will add synchronization to your solution to HW #4 to eliminate all the race conditions. You should not remove any of the nanosleep com-mands that you used to produce the race conditions, but simply add the necessary mutex synchronization calls for Linux threads. Your implementation should use the synchroniza-tion primitives of the programming API you are working with (Posix). Your code must be put into the pthread race.c le inside the part1 folder. NO OTHER FILES WILL BE GRADED

 

 

 

Part 2 – Implementing Consumer Producer Problem

 

Part two of the assignment is (very roughly) based on Programming Project 6.40 in Sil-bershatz. You will be implementing a Producer-Consumer program with a bounded bu er queue of N elements, P producer threads and C consumer threads (N, P and C should be command line arguments to your program, along with three additional parameters, X, Ptime and Ctime, that are described below). Each Producer thread should Enqueue X di erent numbers onto the queue (sleeping for Ptime seconds in between each call to Enqueue). Each Consumer thread should Dequeue P*X/C (be careful when P*X/C is not evenly divisible) items from the queue (sleeping for Ctime seconds in between each call to Dequeue). The main program should create/initialize the Bounded Bu er Queue, print a timestamp, spawn o C consumer threads & P producer threads, wait for all of the threads to nish and then print o another timestamp & the duration of execution.

 

Step 1. Write high level pseudocode for the Producer and Consumer threads, as well as for the Bounded Bu er Queue (Enqueue, Dequeue). Use semaphores to describe syn-chronization logic in your pseudocode. You may use the P/V calls to denote locking

 

1

 

and unlocking of a mutex and signal/wait calls to denote the locking and unlocking of semaphores. Submit this pseudocode in a le called pandcpseudo.txt . Design a testing strategy for verifying that all of the threads are collectively executing correctly. One possible testing strategy is to have a single atomic counter (i.e. a counter with mutex synchronization so it is guaranteed to produce di erent numbers) to generate numbers for Producer threads, then have the main routine combine the output from all of the Consumer threads, sort it and verify that all of the input numbers appeared as output. Submit this testing strategy as part of your design documentation.

 

Step 2. Implement your Producer-Consumer program using Linux threads.

 

For step 2, you will take your pandcpseudo.txt and implement it in C using the appro-priate synchronization primitives(mutex locks and/or signal semaphores and/or count semaphores) so that your code always executes correctly. Submit well-commented source code and annotated output to demonstrate that your code is executing cor-rectly.

 

The implementation has the the following requirements:

 

{ Your implementation must be stored in pandc.c NO OTHER .C FILES WILL BE CHECKED OR GRADED FOR ANY REASON

 

{ Your program must accept 6 command like arguments N,P,C,X,Ptime,Ctime Where each arguments is :

 

N is the number of bu ers to maintain. P is the number of producer threads. C is the number of consumer threads.

 

X is the number of items each producer thread will produce.

 

Ptime is the how long (in seconds) each producer thread will sleep after producing an item.

 

Ctime is the how long (in seconds) each consumer thread will sleep after consuming an item.

 

{ Your implementation must maintain N bu ers. Size of these bu ers can be one which store one integer. Which basically makes the bounded bu er and 1-Dimensional array of integers.

 

{ Your implementation must generate a unique sequence of numbers for the Pro-ducer threads. Then the Consumer threads should consume the same sequence.

 

{ Your implementation must handle when P*X/C is not evenly divisible. For ex-ample if P = 3 and X = 5 and C = 2 then P*X/C is (5*3)/2 which is 7.5. This means one consumer thread consume 1 extra item then the other thread.

 

{ Each consumer thread must print their thread id and which item they have con-sumed. It may be useful to assign a readable number to each thread to make things easier.

 

2

 

{ Each consumer thread must sleep for Ctime AFTER consuming each item.

 

{ Each producer thread must print their thread id and which item they have pro-duced. It may be useful to assign a readable number to each thread to make things easier.

 

{ Each producer thread must sleep for Ptime AFTER producing each item.

 

{ Must use enqueue item and dequeue item functions given to add and remove items. These need to be implemented as well.

 

{ Your implementation must implement a test strategy that shows your program works. A simple approach is to maintain a consumer array and a producer array. If everything works out these two arrays should be identical and in order(Given enqueue and dequeue are implemented correctly).

 

 

 

Sample Output:

 

Current time: Sun Jul 22 01:15:15 2018

 

 

Number of Buffers : 7

 

Number of Producers : 5

 

Number of Consumers : 3

 

Number of items Produced by each producer : 16

 

Number of items consumed by each consumer : 26

 

Over consume on? : 1

 

Over consume amount : 28

 

Time each Producer Sleeps (seconds) : 1

 

Time each Consumer Sleeps (seconds) : 1

 

1 was produced by producer-> 1
2 was produced by producer-> 2
3 was produced by producer-> 3
4 was produced by producer-> 4
5 was produced by producer-> 5
1 was consumed by consumer-> 1
2 was consumed by consumer-> 2
3 was consumed by consumer-> 3
6 was produced by producer-> 5
7 was produced by producer-> 2
4 was consumed by consumer-> 2
5 was consumed by consumer-> 3
8 was produced by producer-> 4
6 was consumed by consumer-> 1

 

 

3

 

9 was produced by producer-> 3
10 was produced by producer-> 1
11 was produced by producer-> 3
7 was consumed by consumer-> 2
12 was produced by producer-> 2
13 was produced by producer-> 1
8 was consumed by consumer-> 3
14 was produced by producer-> 4
9 was consumed by consumer-> 1
15 was produced by producer-> 5
10 was consumed by consumer-> 1
11 was consumed by consumer-> 3
16 was produced by producer-> 4
12 was consumed by consumer-> 2
17 was produced by producer-> 5
18 was produced by producer-> 1
19 was produced by producer-> 3
13 was consumed by consumer-> 2
14 was consumed by consumer-> 3
20 was produced by producer-> 2
21 was produced by producer-> 1
15 was consumed by consumer-> 1
22 was produced by producer-> 4
16 was consumed by consumer-> 1
17 was consumed by consumer-> 3
18 was consumed by consumer-> 2
23 was produced by producer-> 5
24 was produced by producer-> 2
25 was produced by producer-> 4
19 was consumed by consumer-> 2
20 was consumed by consumer-> 1
26 was produced by producer-> 1
21 was consumed by consumer-> 3
27 was produced by producer-> 3
28 was produced by producer-> 2
22 was consumed by consumer-> 3
23 was consumed by consumer-> 2
24 was consumed by consumer-> 1
29 was produced by producer-> 4
30 was produced by producer-> 5
31 was produced by producer-> 2
25 was consumed by consumer-> 2

 

 

 

 

4

 

26 was consumed by consumer-> 1
32 was produced by producer-> 5
33 was produced by producer-> 1
27 was consumed by consumer-> 3
34 was produced by producer-> 4
28 was consumed by consumer-> 1
29 was consumed by consumer-> 2
35 was produced by producer-> 2
30 was consumed by consumer-> 3
36 was produced by producer-> 3
37 was produced by producer-> 1
31 was consumed by consumer-> 2
32 was consumed by consumer-> 1
38 was produced by producer-> 2
33 was consumed by consumer-> 3
39 was produced by producer-> 4
40 was produced by producer-> 3
34 was consumed by consumer-> 3
35 was consumed by consumer-> 2
41 was produced by producer-> 2
42 was produced by producer-> 1
36 was consumed by consumer-> 1
43 was produced by producer-> 3
37 was consumed by consumer-> 1
38 was consumed by consumer-> 3
44 was produced by producer-> 1
45 was produced by producer-> 5
39 was consumed by consumer-> 2
46 was produced by producer-> 3
40 was consumed by consumer-> 2
41 was consumed by consumer-> 1
42 was consumed by consumer-> 3
47 was produced by producer-> 2
48 was produced by producer-> 4
49 was produced by producer-> 5
43 was consumed by consumer-> 3
44 was consumed by consumer-> 1
50 was produced by producer-> 3
45 was consumed by consumer-> 2
51 was produced by producer-> 1
52 was produced by producer-> 5
46 was consumed by consumer-> 2

 

 

 

 

5

 

53 was produced by producer-> 5
47 was consumed by consumer-> 1
48 was consumed by consumer-> 3
54 was produced by producer-> 2
55 was produced by producer-> 1
49 was consumed by consumer-> 1
50 was consumed by consumer-> 3
51 was consumed by consumer-> 2
56 was produced by producer-> 5
57 was produced by producer-> 3
58 was produced by producer-> 2
52 was consumed by consumer-> 2
53 was consumed by consumer-> 3
59 was produced by producer-> 1
54 was consumed by consumer-> 1
60 was produced by producer-> 4
61 was produced by producer-> 2
55 was consumed by consumer-> 1
56 was consumed by consumer-> 2
62 was produced by producer-> 5
57 was consumed by consumer-> 3
63 was produced by producer-> 3
64 was produced by producer-> 1
58 was consumed by consumer-> 3
59 was consumed by consumer-> 2
60 was consumed by consumer-> 1
65 was produced by producer-> 2
66 was produced by producer-> 4
67 was produced by producer-> 1
61 was consumed by consumer-> 1
62 was consumed by consumer-> 2
63 was consumed by consumer-> 3
68 was produced by producer-> 3
69 was produced by producer-> 5
70 was produced by producer-> 1
64 was consumed by consumer-> 2
65 was consumed by consumer-> 1
71 was produced by producer-> 2
66 was consumed by consumer-> 3
72 was produced by producer-> 4
73 was produced by producer-> 3

 

Producer Thread joined: 1

 

 

 

6

 

67 was consumed by consumer-> 3
68 was consumed by consumer-> 1
69 was consumed by consumer-> 2
74 was produced by producer-> 3
75 was produced by producer-> 5
76 was produced by producer-> 4
Producer Thread joined: 2  
70 was consumed by consumer-> 1
71 was consumed by consumer-> 2
72 was consumed by consumer-> 3
77 was produced by producer-> 3
78 was produced by producer-> 4
79 was produced by producer-> 5
73 was consumed by consumer-> 3
74 was consumed by consumer-> 1
Producer Thread joined: 3  
80 was produced by producer-> 4
75 was consumed by consumer-> 2
76 was consumed by consumer-> 2
77 was consumed by consumer-> 1
78 was consumed by consumer-> 3
Producer Thread joined: 4  
Producer Thread joined: 5  
79 was consumed by consumer-> 3
Consumer Thread joined: 1  
Consumer Thread joined: 2  
80 was consumed by consumer-> 3
Consumer Thread joined: 3  

 

Current time: Sun Jul 22 01:15:43 2018
Producer Array | Consumer Array
1 | 1
2 | 2
3 | 3
4 | 4
5 | 5
6 | 6
7 | 7
8 | 8
9 | 9
10 | 10
11 | 11

 

 

 

 

7

 

12 | 12
13 | 13
14 | 14
15 | 15
16 | 16
17 | 17
18 | 18
19 | 19
20 | 20
21 | 21
22 | 22
23 | 23
24 | 24
25 | 25
26 | 26
27 | 27
28 | 28
29 | 29
30 | 30
31 | 31
32 | 32
33 | 33
34 | 34
35 | 35
36 | 36
37 | 37
38 | 38
39 | 39
40 | 40
41 | 41
42 | 42
43 | 43
44 | 44
45 | 45
46 | 46
47 | 47
48 | 48
49 | 49
50 | 50
51 | 51
52 | 52
53 | 53

 

 

 

 

8

 

54 | 54
55 | 55
56 | 56
57 | 57
58 | 58
59 | 59
60 | 60
61 | 61
62 | 62
63 | 63
64 | 64
65 | 65
66 | 66
67 | 67
68 | 68
69 | 69
70 | 70
71 | 71
72 | 72
73 | 73
74 | 74
75 | 75
76 | 76
77 | 77
78 | 78
79 | 79
80 | 80

 

Consume and Produce Arrays Match!

 

Total Runtime: 28 secs

 

 

 

 

 

What to submit

 

  1. Updated source code for pthread race.c for part 1.

 

  1. txt  le containing pseudo code from step 1 of Part 2.

 

  1. Copy-n-paste program output from completed Part 2 into output.txt

 

  1. Please  ll in README.md  le given for both Parts 1 and 2.

 

 

 

9

 

  1. Push all completed code to your given repository by the deadline.

 

 

How to submit

 

git add .

 

git commit -m ” message” git push

 

Point Breakdown

 

part1 ! 50 points.

 

part2 step1 ! 25 points. part2 step2 ! 100 points.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

10