can somebody (PLEASE!!!) explaine this bounded buffer shared-memory solution to me


I was given an assignment to define a buffer with 5 memory spaces and so I'm thinking it would by something like this
int myBuffer[5];
but what i was given for an example and what I've also googled and researched is totally different
this is the piece of code I was just given to follow and i don't understand a few things about it.
1
2
3
4
5
6
7
8
#define BUFFER_SIZE 10 //it seams here a Buffer is being declared with 10 memory spaces
typedef struct { //this part I don't understand, I mean I get the (typedef struct) a little
. . . //But i don't understand what parameters go inside the brackets
} item; // i dont understand where item comes from or what it does.

item buffer[BUFFER_SIZE]; // here it seems the Buffer size definition from up top is being initialized
int in = 0; //to an actual array
int out = 0; //and the rest is self explanatory 

we were also expected to create a producer but I could only come up with two possibilities
this is number one (and its not finished)
it just endlessly loops right now
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

using namespace std;


//shared data************************************…
char sharedData[5]={'a', 'b', 'c', 'd', 'e'};
int prodAdd = 0;
int consSub = 0;
//shared memory**********************************…
int myBuffer[5];
int i=0;
int a=5;
void producer(){


while (prodAdd <= myBuffer[a]){
for(i;i<sizeof(myBuffer);i++){
sharedData[i] = myBuffer[a];

cout<<myBuffer[a]<<"-";
}


and I also started this piece of code
and it sucks balls too

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const int N=5;
int myBuffer[N];
int prodAdd = 0;
int consSub =0;

void producer(){



while (true){
int difference = abs(prodAdd - consSub);
if(difference < N)
{myBuffer[prodAdd%N];
}

}

}

IM NOT ASKING ANYBODY TO DO MY HOMEWORK FOR ME I JUST NEED SOME ADVICE HERE!!!!

asking a butt load of questions and doing Google research is my only resources

Please help

Thank You!!!!
This is just a macro called BUFFER_SIZE that holds the token 10
Anywhere in the later code that has BUFFER_SIZE will be replaced by 10
#define BUFFER_SIZE 10

This explains the typedef struct stuff better than I can. Moral of the story is to not do it in c++ because it's sloppy because it hides data that shouldn't be hidden.
http://www.cplusplus.com/forum/beginner/15967/

Hi

Well, int myBuffer[5]; is just what you want (space for 5 integers?)

The BUFFER_SIZE code you posted looks like C to me. You are doing the right thing by using const int N=5; instead. C didn't have consts originally, so C programmers had to use #defines to name constant values.

(The compiler uses the define to do a literal search and replace of BUFFER_SIZE, so the code which is actually compiled will be item buffer[10];, assuming BUFFER_SIZE didn't get modified)

But, in your first code snippet, sizeof(myBuffer) should be sizeof(myBuffer)/sizeof(myBuffer[0]) (size of whole buffer divided by size of first element)

or just 5, as you already know the size.

sizeof() gives the size of a variable or type in bytes. As myBuffer is an array of ints, the size in bytes will (assuming 4-byte ints) will be 5 * 4 = 20 bytes. So your loop is going too far.

If you use the const, like in your second fragment, you can use N for the loop condition

1
2
3
4
5
6
7
8
const int N=5;
int myBuffer[N];

// etc

for(i;i<N;i++){

    // etc 


Andy

PS I haven't tried to understand what you're trying to do inside your loop.
Last edited on
Topic archived. No new replies allowed.