-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSynchronization.c
185 lines (152 loc) · 4.16 KB
/
Synchronization.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
/* Synchronization.c
Group Members:
Kevin Thomas, [email protected]
Lucas Sager, [email protected]
Allison Meredith, [email protected]
Group: C
Author: Kevin Thomas
Date:4/7/2024
File Discription:
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <semaphore.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include "Synchronization.h"
#define MAX_QUEUE_SIZE 10
// Global variables for shared memory and semaphore
int shmid;
SharedQueue *queue;
sem_t *mutex, *full, *empty;
// Create shared memory segment for the queue
void create_shared_memory() {
shmid = shmget(IPC_PRIVATE, sizeof(SharedQueue), IPC_CREAT | 0666);
if (shmid == -1) {
perror("shmget");
exit(1);
}
queue = (SharedQueue *)shmat(shmid, NULL, 0);
if (queue == (void *)-1) {
perror("shmat");
exit(1);
}
queue->front = queue->rear = -1;
queue->size = MAX_QUEUE_SIZE;
}
// Initialize semaphores
void init_semaphores() {
mutex = sem_open("/queue_mutex", O_CREAT | O_EXCL, 0666, 1);
full = sem_open("/queue_full", O_CREAT | O_EXCL, 0666, 0);
empty = sem_open("/queue_empty", O_CREAT | O_EXCL, 0666, MAX_QUEUE_SIZE);
}
void display_queue() {
sem_wait(mutex);
if (queue->front == -1) {
printf("Queue is empty\n");
sem_post(mutex);
return;
}
printf("Queue elements:");
int i = queue->front;
do {
printf("acccount index:%d, pid:%d ;", queue->elements[i].data,queue->elements[i].pid);
i = (i + 1) % queue->size;
} while (i != (queue->rear + 1) % queue->size);
printf("\n");
sem_post(mutex);
}
// Enqueue an element into the queue
void enqueue(QueueElement element) {
//display_queue();//duplicate for seeing before que is added to.
sem_wait(empty);
sem_wait(mutex);
if (queue->front == -1)
queue->front = 0;
queue->rear = (queue->rear + 1) % queue->size;
queue->elements[queue->rear] = element;
sem_post(mutex);
sem_post(full);
display_queue();
}
void enqueue_quiet(QueueElement element) { //duplicate reader for reading final printout
sem_wait(empty);
sem_wait(mutex);
if (queue->front == -1)
queue->front = 0;
queue->rear = (queue->rear + 1) % queue->size;
queue->elements[queue->rear] = element;
sem_post(mutex);
sem_post(full);
}
// Dequeue an element from the queue
QueueElement dequeue() {
sem_wait(full);
sem_wait(mutex);
QueueElement element = queue->elements[queue->front];
if (queue->front == queue->rear)
queue->front = queue->rear = -1;
else
queue->front = (queue->front + 1) % queue->size;
sem_post(mutex);
sem_post(empty);
//printf("dequeued acccount index:%d, pid:%d account:%d ;\n", element.data,element.pid,element.account);
return element;
}
pid_t peek() {
sem_wait(mutex);
if (queue->front == -1) {
printf("Queue is empty\n");
sem_post(mutex);
return -1;
}
pid_t pid = queue->elements[queue->front].pid;
sem_post(mutex);
return pid;
}
void cleanup(int end) { // error message on or off
// Detach shared memory
if (shmdt(queue) == -1 && end) {
perror("shmdt");
}
// Remove shared memory segment
// if (shmctl(shmid, IPC_RMID, NULL) == -1) {
// perror("shmctl");
//} //this is the section that gives errors cuz we have mutlie shared memory sections.
// Close and unlink semaphores
sem_close(mutex);
sem_unlink("/queue_mutex");
sem_close(full);
sem_unlink("/queue_full");
sem_close(empty);
sem_unlink("/queue_empty");
}
/*
int main() {
create_shared_memory();
init_semaphores();
pid_t parent = getpid();
fork();
// Example usage
QueueElement element;
element.data = 42;
element.pid = getpid();
enqueue(element);
if (getpid() == parent)
{
wait(NULL);
}
display_queue();
QueueElement dequeued_element = dequeue();
printf("Dequeued element: %d\n", dequeued_element.data);
if (getpid() == parent)
{
cleanup(1);
}
return 0;
}
*/