-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise-B.c
108 lines (83 loc) · 2.22 KB
/
exercise-B.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h> /* printf(), fprintf */
#include <stdlib.h> /* [s]rand() */
#include <unistd.h> /* sleep() */
#include <pthread.h> /* pthread_... */
#define PRODUCERS 10
#define CONSUMERS 10
#include <sstream>
using namespace std;
string int_to_str(int x) {
stringstream ss;
ss << x;
return ss.str();
}
int done = 0;
int count = 0;
static pthread_mutex_t mutex;
pthread_cond_t cond;
pthread_t consumer_tid[CONSUMERS], producer_tid[PRODUCERS];
void *
producer(void *param)
{
long int id = (long int)param;
while ( count >= 5 ) {
char str[] = "pthread_yield(): Producer: thread_id: ";
string concat_str;
concat_str = str + int_to_str((long)id) + " count: " + int_to_str(count);
const char* myCharArr = concat_str.c_str();
int err = pthread_yield();
//perror(myCharArr);
}
pthread_mutex_lock(&mutex);
if ( count < 5 ) {
count = count + 1;
}
if ( count == 5 ) {
pthread_cond_broadcast(&cond);
done = 1;
}
printf("Producer: thread_id: %2d count: %2d\n", id+1, count);
pthread_mutex_unlock(&mutex);
pthread_exit(0);
}
void *
consumer(void *param)
{
long int id = (long int)param;
pthread_mutex_lock(&mutex);
while ( done != 1 )
pthread_cond_wait(&cond, &mutex);
printf("Consumer: thread_id: %2d count: %2d\n", id+11, count);
count = count - 1;
pthread_mutex_unlock(&mutex);
pthread_exit(0);
}
int
main()
{
long int i;
/* Create the producer threads */
for (i = 0; i < PRODUCERS; i++)
if (pthread_create(&producer_tid[i], NULL, producer, (void *)i) != 0) {
perror("producer: pthread_create");
abort();
}
/* Create the consumer threads */
for (i = 0; i < CONSUMERS; i++)
if (pthread_create(&consumer_tid[i], NULL, consumer, (void *)i) != 0) {
perror("consumer: pthread_create");
abort();
}
/* Wait for them to complete */
for (i = 0; i < PRODUCERS; i++)
if (pthread_join(producer_tid[i], NULL) != 0) {
perror("producer: pthread_join");
abort();
}
for (i = 0; i < CONSUMERS; i++)
if (pthread_join(consumer_tid[i], NULL) != 0) {
perror("consumer: pthread_join");
abort();
}
return 0;
}