-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer.c
187 lines (149 loc) · 4.54 KB
/
consumer.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
186
187
/*
CSC139
Spring 2023
First Assignment
Varia, Himanshukumar
Section #1
OSs Tested on: such as Linux(putty).
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <string.h>
// Size of shared memory block
// Pass this to ftruncate and mmap
#define SHM_SIZE 4096
// Global pointer to the shared memory block
// This should receive the return value of mmap
// Don't change this pointer in any function
void* gShmPtr;
// You won't necessarily need all the functions below
void SetIn(int);
void SetOut(int);
void SetHeaderVal(int, int);
int GetBufSize();
int GetItemCnt();
int GetIn();
int GetOut();
int GetHeaderVal(int);
void WriteAtBufIndex(int, int);
int ReadAtBufIndex(int);
int main()
{
const char *name = "OS_HW1_HimanshukumarVaria"; // Name of shared memory block to be passed to shm_open
int bufSize; // Bounded buffer size
int itemCnt; // Number of items to be consumed
int in; // Index of next item to produce
int out; // Index of next item to consume
// Write code here to create a shared memory block and map it to gShmPtr
// Use the above name
// **Extremely Important: map the shared memory block for both reading and writing
// Use PROT_READ | PROT_WRITE
int shm_fd = shm_open(name, O_RDWR, 0666);
gShmPtr = mmap(0, SHM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
if(gShmPtr < 0 ) {
printf("Error: Failed to map virtual address space.\n");
exit(1);
}
// Write code here to read the four integers from the header of the shared memory block
// These are: bufSize, itemCnt, in, out
// Just call the functions provided below like this:
bufSize = GetBufSize();
itemCnt = GetItemCnt();
in = GetIn();
out = GetOut();
// Write code here to check that the consumer has read the right values:
printf("Consumer reading: bufSize = %d\n",bufSize);
// Write code here to consume all the items produced by the producer
int i = 0; // Loop counter.
// Use the functions provided below to get/set the values of shared variables in, out, bufSize
while(i < itemCnt) {
in = GetIn(); // Get in value from header.
// If the buffer is not empty, read from it.
if(in != out) {
int val = ReadAtBufIndex(out); // Read value at buffer index.
printf("Consuming Item %d with value %d at Index %d\n", i, val, out); // Print stuff so we know it worked.
out = (out + 1) % bufSize; // Increase the index, mod with the buffer size.
SetOut(out); // Set the header to the new buffer index for output.
i++; // Increase the loop counter.
}
}
// remove the shared memory segment
if (shm_unlink(name) == -1) {
printf("Error removing %s\n",name);
exit(-1);
}
return 0;
}
// Set the value of shared variable "in"
void SetIn(int val)
{
SetHeaderVal(2, val);
}
// Set the value of shared variable "out"
void SetOut(int val)
{
SetHeaderVal(3, val);
}
// Get the ith value in the header
int GetHeaderVal(int i)
{
int val;
void* ptr = gShmPtr + i*sizeof(int);
memcpy(&val, ptr, sizeof(int));
return val;
}
// Set the ith value in the header
void SetHeaderVal(int i, int val)
{
// Write the implementation
void* ptr = gShmPtr + i*sizeof(int);
memcpy(ptr, &val, sizeof(int));
}
// Get the value of shared variable "bufSize"
int GetBufSize()
{
return GetHeaderVal(0);
}
// Get the value of shared variable "itemCnt"
int GetItemCnt()
{
return GetHeaderVal(1);
}
// Get the value of shared variable "in"
int GetIn()
{
return GetHeaderVal(2);
}
// Get the value of shared variable "out"
int GetOut()
{
return GetHeaderVal(3);
}
// Write the given val at the given index in the bounded buffer
void WriteAtBufIndex(int indx, int val)
{
// Write the implementation
void* ptr = gShmPtr + 4*sizeof(int) + indx*sizeof(int);
memcpy(ptr, &val, sizeof(int));
}
// Read the val at the given index in the bounded buffer
int ReadAtBufIndex(int indx)
{
// Write the implementation
int val;
// Skip the four-integer header and go to the given index
void* ptr = gShmPtr + 4*sizeof(int) + indx*sizeof(int);
memcpy(&val, ptr, sizeof(int));
return val;
}
// Get a random number in the range [x, y]
int GetRand(int x, int y)
{
int r = rand();
r = x + r % (y-x+1);
return r;
}