-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.c
50 lines (44 loc) · 1.01 KB
/
reader.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
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include "shm.h"
void
receive_user_input(shm_t* shm)
{
printf("Enter your message in the writer\n");
// hopefully this process will block on this mutex
while(shm->marker)
{
pthread_mutex_lock(&shm->mutex);
while(shm->counter < 0 && shm->marker)
{
pthread_cond_wait(&shm->condition, &shm->mutex);
}
if (!shm->marker) {
pthread_mutex_unlock(&shm->mutex);
break;
}
while(shm->counter >= 0)
{
printf("Received: %d\n", shm->queue[shm->counter]);
shm->counter--;
}
pthread_mutex_unlock(&shm->mutex);
}
return;
}
int
main()
{
char* shm_name = "/test";
shm_t* shm = get_shm(shm_name);
if (shm == (void*) -1) {
printf("Failed to initialize shared memory\n");
return -1;
}
receive_user_input(shm);
return 0;
}