-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsharedMemory.h
More file actions
72 lines (57 loc) · 1.98 KB
/
sharedMemory.h
File metadata and controls
72 lines (57 loc) · 1.98 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
//Isha Gupta
//3.28.2025
#ifndef SHAREDMEMORY_H
#define SHAREDMEMORY_H
#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <semaphore.h>
#include <pthread.h>
#include <string.h>
// Modifiable constants
const char *sharedMemoryName = "/producerConsumerTable";
const unsigned int nItemSlots = 2; // Buffer size
const float defaultDelay = 0.01;
// Shared Memory Structure
struct sharedMemory {
sem_t *mutex;
sem_t *emptySlots;
sem_t *fullSlots;
unsigned short *items;
};
// Global delay variable
float delay;
// Initialize Shared Memory & Semaphores
struct sharedMemory initializeSharedMemory(const char *name) {
int fd = shm_open(name, O_CREAT | O_RDWR, 0666);
if (fd == -1) { perror("shm_open"); exit(1); }
size_t tableSize = sizeof(sem_t) * 3 + sizeof(unsigned short) * nItemSlots;
ftruncate(fd, tableSize);
void *ptr = mmap(NULL, tableSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (ptr == MAP_FAILED) { perror("mmap"); exit(3); }
struct sharedMemory shm;
shm.mutex = (sem_t*) ptr;
shm.emptySlots = (sem_t*)((char*)ptr + sizeof(sem_t));
shm.fullSlots = (sem_t*)((char*)ptr + 2 * sizeof(sem_t));
shm.items = (unsigned short*)((char*)ptr + 3 * sizeof(sem_t));
sem_init(shm.mutex, 1, 1);
sem_init(shm.emptySlots, 1, nItemSlots);
sem_init(shm.fullSlots, 1, 0);
return shm;
}
// Run Producer or Consumer
int run(int argc, char *argv[], void* (*produceOrConsumeItems)(void*)) {
setbuf(stdout, NULL);
struct sharedMemory shm = initializeSharedMemory(sharedMemoryName);
delay = (argc > 1) ? atof(argv[1]) : defaultDelay;
unsigned int num_threads = (argc > 2) ? atoi(argv[2]) : 1;
pthread_t threads[num_threads];
for (unsigned int i = 0; i < num_threads; ++i)
pthread_create(&threads[i], NULL, produceOrConsumeItems, (void *) &shm);
for (unsigned int i = 0; i < num_threads; ++i)
pthread_join(threads[i], NULL);
return 0;
}
#endif