-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite.c
More file actions
56 lines (47 loc) · 1.26 KB
/
write.c
File metadata and controls
56 lines (47 loc) · 1.26 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
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/shm.h> //For generating keys in
#include <semaphore.h>
#include <string.h> //For string compare
#define MAX_SLOT_LENGTH 32
typedef struct
{
sem_t filledSlots;
sem_t emptySlots;
int head;
int tail;
int bufferSize;
char buffer[];
}ringBuffer;
void writeToBuffer(ringBuffer *rb, char *valString)
{
sem_wait(&rb->emptySlots);
strncpy(rb->buffer + rb->tail*MAX_SLOT_LENGTH, valString, MAX_SLOT_LENGTH);
rb->tail = (rb->tail+1) % rb->bufferSize;
sem_post(&rb->filledSlots);
}
void readFromBuffer(ringBuffer *rb)
{
sem_wait(&rb->filledSlots);
printf("%s", rb->buffer + rb->head*MAX_SLOT_LENGTH);
printf(" head: %d\n", rb->head);
rb->head = ((rb->head+1) % rb->bufferSize);
sem_post(&rb->emptySlots);
}
int main(int argc, char *argv[])
{
int shmid = atoi(argv[1]);
void *test = shmat(shmid, NULL, 0);
if(test == (void *)-1)
printf("There is an error with shmid");
ringBuffer * rb = (ringBuffer *)test;
printf("We are in the second process: \n");
for(int x = 0; x < 10; x++) {
readFromBuffer(rb);
fflush(stdout);
sleep(1);
}
return 0;
}