-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbounded_buffer.c
More file actions
136 lines (114 loc) · 3.01 KB
/
bounded_buffer.c
File metadata and controls
136 lines (114 loc) · 3.01 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Drew Cooper B00811386 CSCI 3120 Assignment 5 - Synchronization
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#define BUFFER_SIZE 5
// defining custom structs & variable types
typedef int buffer_item;
// used to store the index of a thread
typedef struct thread_param{
int index;
} ThreadParam;
// global variables
pthread_mutex_t mutex;
sem_t full;
sem_t empty;
int in = 0;
int out = 0;
int buffer[BUFFER_SIZE];
int numProducers = 0;
int numConsumers = 0;
int sleepTime = 0;
// updates the in / out values used to insert and remove elements from the buffer
int update_position(int pos){
int temp = pos += 1;
return (pos % BUFFER_SIZE);
}
// inserts a random number into the buffer at the next free position
void insert_item(int index, buffer_item item){
buffer[in] = item;
printf("Producer %d inserted item %d into buffer[%d]\n", index, item, in);
in = update_position(in);
return;
}
// removes the next element from the buffer
void remove_item(int index){
buffer_item item = buffer[out];
printf("Consumer %d removed item %d from buffer[%d]\n", index, item, out);
buffer[out] = -1;
out = update_position(out);
return;
}
void* producer(void* index){
buffer_item item;
while(1){
// sleep
sleep(rand() % 4);
ThreadParam* temp_ptr = (ThreadParam*)index;
int ind = temp_ptr->index;
// generate random number
item = rand();
//request access
sem_wait(&empty);
pthread_mutex_lock(&mutex);
// critical section
insert_item(ind, item);
// release access
pthread_mutex_unlock(&mutex);
sem_post(&full);
}
}
void* consumer(void* index){
while(1){
// sleep
sleep(rand() % 4);
ThreadParam* temp_ptr = (ThreadParam*)index;
int ind = temp_ptr->index;
// request access
sem_wait(&full);
pthread_mutex_lock(&mutex);
// critical section
remove_item(ind);
//release access
pthread_mutex_unlock(&mutex);
sem_post(&empty);
}
}
int main(int argc, char *argv[]){
// initialze thread arrays
pthread_t producers[numProducers];
pthread_t consumers[numConsumers];
// initializing mutex and semaphores
pthread_mutex_init(&mutex, NULL);
sem_init(&full, 0, 0);
sem_init(&empty, 0, BUFFER_SIZE);
// used to ensure generated random numbers are unique
srand(time(NULL));
// if there is not enough parameters given, the program will exit
if (argc != 4) return 1;
sleepTime = atoi(argv[1]);
numProducers = atoi(argv[2]);
numConsumers = atoi(argv[3]);
// creating producer threads
for(int i = 0; i < numProducers; i++){
ThreadParam* p = malloc(sizeof(int)*2);
p->index = i;
pthread_create(&producers[i], NULL, producer, p);
}
// creating consumer threads
for(int x = 0; x < numConsumers; x++){
ThreadParam* p = malloc(sizeof(int)*2);
p->index = x;
pthread_create(&consumers[x], NULL, consumer, p);
}
// sleep to allow the threads to run
sleep(sleepTime);
// destroying mutex and semaphores before terminating the program
pthread_mutex_destroy(&mutex);
sem_destroy(&empty);
sem_destroy(&full);
return 0;
}