-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtapper.c
More file actions
250 lines (215 loc) · 6.15 KB
/
tapper.c
File metadata and controls
250 lines (215 loc) · 6.15 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
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
// Online C compiler to run C program online
#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
#include <sys/wait.h>
#include <ctype.h> //For is digit
#define MAX_SLOT_LENGTH 1000//Max lenght of each slot of the buffer
char *typeBuffer;
typedef enum {bit0=0, bit1=1} bit;
typedef struct
{
bit latest;
bit reading;
bit slot[2];
sem_t filledSlots;
sem_t emptySlots;
int head;
int tail;
int bufferSize;
char buffer[];
}ringBuffer;
//initalizing slot buffer
void initalizeSlotBuff(ringBuffer *sb)
{
sb->latest = 0;
sb->reading = 0;
sb->slot[0] = 0;
sb->slot[1] = 0;
}
void initalizeRingBuff(ringBuffer *rb, int size)
{
sem_init(&rb->filledSlots, 1, 0);
//Semaphore, communication between two processes, intStartPoint
sem_init(&rb->emptySlots, 1, size);
rb->head = 0;
rb->tail = 0;
rb->bufferSize = size;
}
size_t getTotalSize(int bufferSize)
{
size_t totalSize = (MAX_SLOT_LENGTH * bufferSize) + sizeof(ringBuffer);
return (totalSize);
}
int getMemoryId(int bufferSize)
{
int memoryID = shmget(IPC_PRIVATE, getTotalSize(bufferSize), 0644);
if(memoryID == -1)
{
printf("Fail to create key");
exit(EXIT_FAILURE);
}
return memoryID;
}
void *createSharedMemory(int memoryID)
{
void *temp = shmat(memoryID, NULL, 0);
if(temp == (void *)-1)
{
printf("Fail to attached Memory");
exit(EXIT_FAILURE);
}
return temp;
}
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\n", rb->buffer + rb->head*MAX_SLOT_LENGTH);
rb->head = ((rb->head+1) % rb->bufferSize);
sem_post(&rb->emptySlots);
}
int isNumber(const char *str) {
for (int i = 0; str[i] != '\0'; i++)
{
if (!isdigit(str[i])) return 0;
}
return 1;
}
int main(int argc, char *argv[])
{
int options;
int numSlots = 1;//Default is 1 slot unless changed
char *programList [1000];
int programIndex = 0;
int shmid;
int argn = 1; //Default
//Command line parsing for programs and optional argn
int index = 1;
while (index < argc) {
if (strncmp(argv[index], "-p", 2) == 0 && index + 1 < argc) {
programList[programIndex++] = argv[index + 1];
index += 2; // Skip next as it's part of -p
} else if (isNumber(argv[index])) {
argn = atoi(argv[index]);
index++;
break; // Found argn, move to next part
} else {
// Not a -p option or a number, break and handle other options
break;
}
}
optind = index; //Setting getops starting indx
//Command line parsing for the sync type and the size of the buffer
while((options=getopt(argc, argv, "b:s:"))!=-1)
{
switch(options)
{
case 'b':
typeBuffer = optarg;
break;
case 's':
numSlots = atoi(optarg);
break;
case '?':
printf("Unknown option");
break;
}
}
//printf("argn %d\n", argn);
//printf("Buffer type %s\n", typeBuffer);
//printf("size %d\n", numSlots);
//Initalization depending on async or sync
int p1p2memoryID;
int p2p3memoryID;
ringBuffer *rb;
ringBuffer *rb2;
ringBuffer *sb;
ringBuffer *sb2;
if (strcmp(typeBuffer, "async") == 0)
{
p1p2memoryID = getMemoryId(4);
p2p3memoryID = getMemoryId(4);
sb= (ringBuffer *)createSharedMemory(p1p2memoryID);
sb2 = (ringBuffer *)createSharedMemory(p2p3memoryID);
initalizeSlotBuff(sb);
initalizeSlotBuff(sb2);
}
else
{
p1p2memoryID = getMemoryId(numSlots);
p2p3memoryID = getMemoryId(numSlots);
rb= (ringBuffer *)createSharedMemory(p1p2memoryID);
rb2 = (ringBuffer *)createSharedMemory(p2p3memoryID);
initalizeRingBuff(rb,numSlots);
initalizeRingBuff(rb2, numSlots);
}
//Convert memoryID into STR to pass to P1
char p1p2shmidStr[12];
sprintf(p1p2shmidStr, "%d", p1p2memoryID);
//printf("ID1: %s", p1p2shmidStr);
char p2p3shmidStr[12];
sprintf(p2p3shmidStr, "%d", p2p3memoryID);
//convert argn to pass to P3
char argnStr[12];
sprintf(argnStr, "%d", argn);
//convert typeBuffer to pass to P1, P2, P3
char typeStr[12];
sprintf(typeStr, "%s", typeBuffer);
//Creating the path names for running the child processes
char p1Path [50];
sprintf(p1Path, "./%s", programList[0]);
char p2Path [50];
sprintf(p2Path, "./%s", programList[1]);
char p3Path [50];
sprintf(p3Path, "./%s", programList[2]);
//Starting child process and change process image to P1
int p1 = fork();
if (p1 == 0)
{
execl(p1Path, programList[0], p1p2shmidStr, typeStr, NULL);
perror("execl P1 failed");
exit(EXIT_FAILURE);
}
//Start child process and change proce image to P2
int p2 = fork();
if(p2 == 0)
{
execl(p2Path, programList[1], p1p2shmidStr, p2p3shmidStr, typeStr, NULL);
perror("execl P2 failed");
exit(EXIT_FAILURE);
}
int p3 = fork();
if(p3 == 0)
{
execl(p3Path, programList[2], p2p3shmidStr, argnStr, typeStr, NULL);
}
//Waiting for the parent to exit
if(p1 > 0)
waitpid(p1, NULL, 0);
if(p2 > 0)
waitpid(p2, NULL, 0);
if(p3 > 0)
waitpid(p3, NULL, 0);
char line[1000];
FILE *file = fopen("dataFile.txt", "r");
while(fgets(line, sizeof(line), file))
{
printf("%s", line);
}
fclose(file);
//printf("Program has finished executing: Removing shared memory segements...\n");
shmctl(p1p2memoryID, IPC_RMID, NULL);
shmctl(p2p3memoryID, IPC_RMID, NULL);
return 0;
}