Skip to content

Commit fd42fb5

Browse files
committed
Initial commit
0 parents  commit fd42fb5

File tree

4 files changed

+415
-0
lines changed

4 files changed

+415
-0
lines changed

Diff for: .gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

Diff for: pc_mutex_cond_pthread.c

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <unistd.h>
4+
#include <pthread.h>
5+
#include <assert.h>
6+
7+
#define MAX_ITEMS 10
8+
const int NUM_ITERATIONS = 200;
9+
const int NUM_CONSUMERS = 2;
10+
const int NUM_PRODUCERS = 2;
11+
12+
int producer_wait_count; // # of times producer had to wait
13+
int consumer_wait_count; // # of times consumer had to wait
14+
int histogram [MAX_ITEMS+1]; // histogram [i] == # of times list stored i items
15+
16+
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
17+
pthread_cond_t condc, condp;
18+
19+
int items = 0;
20+
21+
void* producer (void* v) {
22+
for (int i=0; i<NUM_ITERATIONS; i++) {
23+
// protects buffer from over-production
24+
pthread_mutex_lock(&mutex);
25+
if(items >= MAX_ITEMS) {
26+
while(items >= MAX_ITEMS) {
27+
pthread_cond_wait(&condp, &mutex);
28+
}
29+
producer_wait_count++;
30+
}
31+
32+
//PRODUCE
33+
items++;
34+
histogram[items]++;
35+
//give greenlight to consume
36+
pthread_cond_signal(&condc);
37+
pthread_mutex_unlock(&mutex);
38+
39+
}
40+
return NULL;
41+
}
42+
43+
void* consumer (void* v) {
44+
for (int i=0; i<NUM_ITERATIONS; i++) {
45+
// protects buffer from over-consumption
46+
pthread_mutex_lock(&mutex);
47+
if(items <= 0 ) {
48+
while(items <= 0) {
49+
pthread_cond_wait(&condc, &mutex);
50+
}
51+
consumer_wait_count++;
52+
}
53+
//CONSUME
54+
items--;
55+
histogram[items]++;
56+
//give greenlight to produce
57+
pthread_cond_signal(&condp);
58+
pthread_mutex_unlock(&mutex);
59+
}
60+
return NULL;
61+
}
62+
63+
int main (int argc, char** argv) {
64+
pthread_t t[4];
65+
66+
// Create Threads and Join
67+
pthread_create(&t[0], NULL, producer, NULL);
68+
pthread_create(&t[1], NULL, producer, NULL);
69+
pthread_create(&t[2], NULL, consumer, NULL);
70+
pthread_create(&t[3], NULL, consumer, NULL);
71+
72+
pthread_join(t[0], NULL);
73+
pthread_join(t[1], NULL);
74+
pthread_join(t[2], NULL);
75+
pthread_join(t[3], NULL);
76+
77+
printf ("producer_wait_count=%d\nconsumer_wait_count=%d\n", producer_wait_count, consumer_wait_count);
78+
printf ("items value histogram:\n");
79+
int sum=0;
80+
for (int i = 0; i <= MAX_ITEMS; i++) {
81+
printf (" items=%d, %d times\n", i, histogram [i]);
82+
sum += histogram [i];
83+
}
84+
printf("sum: %d\n", sum);
85+
assert (sum == sizeof (t) / sizeof (pthread_t) * NUM_ITERATIONS);
86+
}

Diff for: pc_sem_pthread.c

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <assert.h>
4+
#include <pthread.h>
5+
#include <semaphore.h>
6+
#include <unistd.h>
7+
8+
#define MAX_ITEMS 10
9+
const int NUM_ITERATIONS = 200;
10+
const int NUM_CONSUMERS = 2;
11+
const int NUM_PRODUCERS = 2;
12+
13+
int histogram [MAX_ITEMS+1]; // histogram [i] == # of times list stored i items
14+
sem_t sem_empty;
15+
sem_t sem_full;
16+
sem_t avail;
17+
18+
int items = 0;
19+
20+
void* producer (void* v) {
21+
for (int i=0; i<NUM_ITERATIONS; i++) {
22+
23+
sem_wait(&sem_empty);
24+
sem_wait(&avail);
25+
26+
printf("-");
27+
items++;
28+
histogram[items]++;
29+
sem_post(&avail);
30+
if(items >= MAX_ITEMS)
31+
sem_post(&sem_full);
32+
else
33+
sem_post(&sem_empty);
34+
35+
}
36+
return NULL;
37+
}
38+
39+
void* consumer (void* v) {
40+
for (int i=0; i<NUM_ITERATIONS; i++) {
41+
42+
sem_wait(&sem_full);
43+
sem_wait(&avail);
44+
45+
printf("_");
46+
items--;
47+
histogram[items]++;
48+
sem_post(&avail);
49+
if(items <= 0)
50+
sem_post(&sem_empty);
51+
else
52+
sem_post(&sem_full);
53+
54+
}
55+
return NULL;
56+
}
57+
58+
int main (int argc, char** argv) {
59+
pthread_t t[4];
60+
61+
sem_init(&sem_empty, 0, 1);
62+
sem_init(&sem_full, 0, 0);
63+
sem_init(&avail, 0, 1);
64+
65+
// Create Threads and Join
66+
pthread_create(&t[0], NULL, producer, NULL);
67+
pthread_create(&t[1], NULL, producer, NULL);
68+
pthread_create(&t[2], NULL, consumer, NULL);
69+
pthread_create(&t[3], NULL, consumer, NULL);
70+
71+
pthread_join(t[0], NULL);
72+
pthread_join(t[1], NULL);
73+
pthread_join(t[2], NULL);
74+
pthread_join(t[3], NULL);
75+
76+
printf ("items value histogram:\n");
77+
int sum=0;
78+
for (int i = 0; i <= MAX_ITEMS; i++) {
79+
printf (" items=%d, %d times\n", i, histogram [i]);
80+
sum += histogram [i];
81+
}
82+
printf("sum: %d\n", sum);
83+
assert (sum == sizeof (t) / sizeof (pthread_t) * NUM_ITERATIONS);
84+
85+
sem_destroy(&sem_empty);
86+
sem_destroy(&sem_full);
87+
sem_destroy(&avail);
88+
89+
}

0 commit comments

Comments
 (0)