-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmulti_threading.c
390 lines (370 loc) · 14.8 KB
/
multi_threading.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/* ************************************************************************** */
/* Author: Hye Yeon Park */
/* Date: 2/18/2022 */
/* Project: Line Processor (Multi-threaded Producer Consumer Pipeline) */
/* */
/* */
/* ************************************************************************** */
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
// Size of the buffers
#define SIZE 50
// Number of items that will be produced.
// This number is less than the size of the buffer. (Unbounded buffer)
#define NUM_ITEMS 49
#define NUM_CHAR 1000
// Buffer 1, shared resource between input thread and line_separator thread
char* buffer_1[SIZE];
// Number of items in the buffer
int count_1 = 0;
// Index where the input thread will put the next item
int prod_idx_1 = 0;
// Index where the line_separator thread will pick up the next item
int con_idx_1 = 0;
// Initialize the mutex for buffer 1
pthread_mutex_t mutex_1 = PTHREAD_MUTEX_INITIALIZER;
// Initialize the condition variable for buffer 1
pthread_cond_t full_1 = PTHREAD_COND_INITIALIZER;
// Buffer 2, shared resource between line_separator thread and plus_sign thread
char* buffer_2[SIZE];
// Number of items in the buffer
int count_2 = 0;
// Index where the line_separator thread will put the next item
int prod_idx_2 = 0;
// Index where the plus_sign thread will pick up the next item
int con_idx_2 = 0;
// Initialize the mutex for buffer 2
pthread_mutex_t mutex_2 = PTHREAD_MUTEX_INITIALIZER;
// Initialize the condition variable for buffer 2
pthread_cond_t full_2 = PTHREAD_COND_INITIALIZER;
// Buffer 3, shared resource between plus_sign thread and output thread
char* buffer_3[SIZE];
// Number of items in the buffer
int count_3 = 0;
// Index where the plus_sign thread will put the next item
int prod_idx_3 = 0;
// Index where the output thread will pick up the next item
int con_idx_3 = 0;
// Initialize the mutex for buffer 3
pthread_mutex_t mutex_3 = PTHREAD_MUTEX_INITIALIZER;
// Initialize the condition variable for buffer 3
pthread_cond_t full_3 = PTHREAD_COND_INITIALIZER;
// Stores remaining string of the input
char remaining_line[NUM_CHAR + 81];
/* ************************************************************************** */
/* Function: put_buff_1() */
/* Description: Put an item in buff_1 */
/* Reference: replit.com/@cs344/65prodconspipelinec */
/* */
/* */
/* ************************************************************************** */
void put_buff_1(char* line)
{
// Lock the mutex before putting the item in the buffer
pthread_mutex_lock(&mutex_1);
// Put the item in the buffer
buffer_1[prod_idx_1] = line;
// Increment the index where the next item will be put.
prod_idx_1 = prod_idx_1 + 1;
count_1++;
// Signal to the consumer that the buffer is no longer empty
pthread_cond_signal(&full_1);
// Unlock the mutex
pthread_mutex_unlock(&mutex_1);
}
/* ************************************************************************** */
/* Function: get_input() */
/* Description: Function that the input thread will run; Get input from user. */
/* Put the item in the buffer shared with line_separator thread. */
/* */
/* */
/* ************************************************************************** */
void* get_input(void* args)
{
while (1)
{
// Get the user input
char* line = malloc(sizeof * line * NUM_CHAR);
fgets(line, sizeof * line * NUM_CHAR, stdin);
// Set the line to NULL if STOP string is passed
if (strncmp(line, "STOP\n", 5) == 0)
{
line = NULL;
// Put the line in the buffer 1 shared with line_separator thread
put_buff_1(line);
// Exit function
return NULL;
}
else
{
put_buff_1(line);
}
}
return NULL;
}
/* ************************************************************************** */
/* Function: get_buff_1() */
/* Description: Get the next item from buffer 1 */
/* Reference: replit.com/@cs344/65prodconspipelinec */
/* */
/* */
/* ************************************************************************** */
char* get_buff_1()
{
char* line;
// Lock the mutex before checking if the buffer has data
pthread_mutex_lock(&mutex_1);
while (count_1 == 0)
// Buffer is empty. Wait for the producer to signal that the buffer has data
pthread_cond_wait(&full_1, &mutex_1);
line = buffer_1[con_idx_1];
// Increment the index from which the item will be picked up
con_idx_1 = con_idx_1 + 1;
count_1--;
// Unlock the mutex
pthread_mutex_unlock(&mutex_1);
// Return the item
return line;
}
/* ************************************************************************** */
/* Function: put_buff_2() */
/* Description: Put an item in buff_2 */
/* Reference: replit.com/@cs344/65prodconspipelinec */
/* */
/* */
/* ************************************************************************** */
void put_buff_2(char* line)
{
// Lock the mutex before putting the item in the buffer
pthread_mutex_lock(&mutex_2);
// Put the item in the buffer
buffer_2[prod_idx_2] = line;
// Increment the index where the next item will be put.
prod_idx_2 = prod_idx_2 + 1;
count_2++;
// Signal to the consumer that the buffer is no longer empty
pthread_cond_signal(&full_2);
// Unlock the mutex
pthread_mutex_unlock(&mutex_2);
}
/* ************************************************************************** */
/* Function: replace_line_separator() */
/* Description: Funtion that the line_separator thread will run. Replace a ne */
/* w line character with a space. Get the item from the buffer s */
/* hared with the input thread. Put the item in the buffer share */
/* d with the plus_sign thread. */
/* ************************************************************************** */
void* replace_line_separator(void* args)
{
char* line;
for (int i = 0; i < NUM_ITEMS; i++)
{
// Get line from the buffer 1 shared with input thread
line = get_buff_1();
if (line != NULL)
{
// Replace the line separator with a space
line[strlen(line) - 1] = ' ';
// Put the line in the buffer 2 shared with plus_sign thread
put_buff_2(line);
}
else
{
put_buff_2(line);
return NULL;
}
}
return NULL;
}
/* ************************************************************************** */
/* Function: get_buff_2() */
/* Description: Get the next item from buffer 2 */
/* Reference: replit.com/@cs344/65prodconspipelinec */
/* */
/* */
/* ************************************************************************** */
char* get_buff_2()
{
char* line;
// Lock the mutex before checking if the buffer has data
pthread_mutex_lock(&mutex_2);
while (count_2 == 0)
// Buffer is empty. Wait for the producer to signal that the buffer has data
pthread_cond_wait(&full_2, &mutex_2);
line = buffer_2[con_idx_2];
// Increment the index from which the item will be picked up
con_idx_2 = con_idx_2 + 1;
count_2--;
// Unlock the mutex
pthread_mutex_unlock(&mutex_2);
// Return the item
return line;
}
/* ************************************************************************** */
/* Function: put_buff_3() */
/* Description: Put an item in buff_3 */
/* */
/* */
/* */
/* ************************************************************************** */
void put_buff_3(char* line)
{
// Lock the mutex before putting the item in the buffer
pthread_mutex_lock(&mutex_3);
// Put the item in the buffer
buffer_3[prod_idx_3] = line;
// Increment the index where the next item will be put.
prod_idx_3 = prod_idx_3 + 1;
count_3++;
// Signal to the consumer that the buffer is no longer empty
pthread_cond_signal(&full_3);
// Unlock the mutex
pthread_mutex_unlock(&mutex_3);
}
/* ************************************************************************** */
/* Function: replace_plus_sign() */
/* Description: Funtion that the plus_sign thread will run. Replace double pl */
/* us signs(++) to caret sign(^). Get the item from the buffer s */
/* hared with the line_separator thread. Put the item in the buf */
/* fer shared with the output thread. */
/* ************************************************************************** */
void* replace_plus_sign(void* args)
{
char* line;
for (int i = 0; i < NUM_ITEMS; i++)
{
// Get the line from the buffer 2 shared with line_separator
line = get_buff_2();
if (line != NULL)
{
// Set a buffer to hold revised string
char buffer[NUM_CHAR];
char* buffer_ptr = buffer;
char* line_ptr = line;
while (1)
{
// Search ++ from line
char* temp = strstr(line_ptr, "++");
// If no match, copy rest of the string
if (temp == NULL)
{
strcpy(buffer_ptr, line_ptr);
break;
}
// Copy substring before ++ and move pointer
int substr_len = temp - line_ptr;
memcpy(buffer_ptr, line_ptr, substr_len);
buffer_ptr += temp - line_ptr;
// Add the new character ^ and move pointer
memcpy(buffer_ptr, "^", 1);
buffer_ptr += 1;
// Move line pointer
line_ptr = temp + 2;
}
// Copy it back to line
strcpy(line, buffer);
// Put the line in the buffer 3 shared with output thread
put_buff_3(line);
}
else
{
put_buff_3(line);
return NULL;
}
}
return NULL;
}
/* ************************************************************************** */
/* Function: get_buff_3() */
/* Description: Get the next item from buffer 3 */
/* */
/* */
/* */
/* ************************************************************************** */
char* get_buff_3()
{
char* line;
// Lock the mutex before checking if the buffer has data
pthread_mutex_lock(&mutex_3);
while (count_3 == 0)
// Buffer is empty. Wait for the producer to signal that the buffer has data
pthread_cond_wait(&full_3, &mutex_3);
line = buffer_3[con_idx_3];
// Increment the index from which the item will be picked up
con_idx_3 = con_idx_3 + 1;
count_3--;
// Unlock the mutex
pthread_mutex_unlock(&mutex_3);
// Return the item
return line;
}
/* ************************************************************************** */
/* Function: write_output() */
/* Description: Function that the output thread will run. Write the processed */
/* data to standard output as lines of exactly 80 characters. Ge */
/* t the item from the buffer shared with the plus_sign thread. */
/* */
/* ************************************************************************** */
void* write_output(void* args)
{
char* line;
for (int i = 0; i < NUM_ITEMS; i++)
{
// Get the line from the buffer 3 shared with plus_sign thread
line = get_buff_3();
if (line != NULL)
{
// Concatenate the new line with the remaining line
strcat(remaining_line, line);
// Redirect the line pointer
line = remaining_line;
size_t count = strlen(line);
// Display output for every 80 characters
while (count >= 80)
{
char output_line[256];
// Copy 80 characters
memcpy(output_line, line, 80);
puts(output_line);
// Move the pointer
line += 80;
count -= 80;
}
// Store the remaining line
strcpy(remaining_line, line);
}
else
{
return NULL;
}
}
return NULL;
}
/* ************************************************************************** */
/* Function: main() */
/* Description: main function that creates and joins threads */
/* Reference: replit.com/@cs344/65prodconspipelinec */
/* */
/* */
/* ************************************************************************** */
int main(int argc, char* argv[])
{
srand(time(0));
pthread_t input_t, line_separator_t, plus_sign_t, output_t;
// Create the threads
pthread_create(&input_t, NULL, get_input, NULL);
pthread_create(&line_separator_t, NULL, replace_line_separator, NULL);
pthread_create(&plus_sign_t, NULL, replace_plus_sign, NULL);
pthread_create(&output_t, NULL, write_output, NULL);
// Wait for the threads to terminate
pthread_join(input_t, NULL);
pthread_join(line_separator_t, NULL);
pthread_join(plus_sign_t, NULL);
pthread_join(output_t, NULL);
return EXIT_SUCCESS;
}