I'm running into kind of a weird issue. I am making a shared memory fifo queue for an OS class that basically mimics the producer consumer problem. In one of my functions putBuffer() which inserts an item into the shared buffer I wasn't getting any output after a certain point so I ran it through gdb it prints what I thought would print during the initial terminal run and when I quit gdb it says program exited normally so I'm not really sure where my error is. Has anyone else ever experienced anything like this?
so when i run it through gdb it prints "made it past initial check and the value of fifo->[12] which is just set with a hard coded value here for testing purposes. but in the terminal only prints "made it past initial check. I even made sure the error was not in printf() Any thoughts? Heres the code
int putBuffer(FIFO_QUEUE *fifo, int element)
{
printf("made it past initial check\n");
fifo->queue[12] = 23;
//insert the element at the next available position IFF there is one
if(printf("made it to putBuffer and fifo->queue[12] = %d\n", fifo->queue[12]) < 0)
{
printf("error in putBuffer\n");
return -1;
}
//determine whether or not we need to "wrap" around to the beginning of the queue
if(fifo->putPos == fifo->size - 1)
fifo->putPos = 0; //wrap to the beginning
else
fifo->putPos++;
//increment the number of items in the queue
fifo->numItems++;
//if all went well return 0
return 0;
}
per request here is the def of FIFO_QUEUE i am dynamically allocating the queue structure in another function but its storing the values and prints through gdb
typedef struct fifoQueue{
int *queue;
int putPos; //next position to insert to
int rmPos; //next position to remove from
int numItems; //number of items currently in the queue
int size; //the max size of the queue
}FIFO_QUEUE;
This is where I think I am going wrong I need to dynamically allocate the fifo queue in a function and what I am trying to do is use memcpy to basically create a fifo queue and then copy its contents into my shared memory but there seems to be a disconnect due to the int* in FIFO_QUEUE and I can't figure out. where I'm going wrong. I suspect it has something to do with the dynamic allocation of the in the mkBuffer() function my thinking was that memcpy would just copy the 100 bytes of whatever was in there but I could be mistaken
code for dynamic allocation
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<stdlib.h>
#include<stdio.h>
#include <fcntl.h> /* For O_* constants */
#include <sys/stat.h>
#include<string.h>
#include "fifoQueue.h"
FIFO_QUEUE *makeBuffer(int size);
int main(int argc, char *argv[])
{
//our buffer
int i = 0;
int segment_id = 0;
FIFO_QUEUE *sharedBuff;
FIFO_QUEUE *fifo = NULL;
fifo = makeBuffer(25);
//-------retrieve COMMAND LINE arguments------//
if(argc != 2)
{
printf("getBuffer requires 2 command line args\n");
exit(-1);
}
//------SET UP SHARED MEMORY--------//
//get memory ID
segment_id = atoi(argv[1]);
printf("MAKE_BUFFER: Shared mem seg Id in get buffer = %d\n", segment_id);
//Attach
sharedBuff = (FIFO_QUEUE*)shmat(segment_id, NULL, SHM_RND);
//COPY contents of fifo into shared mem
memcpy((void*)sharedBuff, (void*)fifo, 120);
//--------CLEANUP--------//
//DETACH shared mem
shmdt(sharedBuff);
//deallocate memory
rmBuffer(fifo);
return 0;
}
/* makeBuffer()
Description:
- Creates a FIFO buffer of integers of size <size>
*/
FIFO_QUEUE *makeBuffer(int size)
{
//variables
int i = 0;
FIFO_QUEUE *fifo = NULL;
//allocate room for our struct
fifo = (FIFO_QUEUE*)malloc(sizeof(FIFO_QUEUE));
//allocate room for our queue
fifo->queue = (int*)malloc(sizeof(int) * size);
//set the initial position and number of items in the queue to 0
fifo->putPos = 0;
fifo->rmPos = 0;
fifo->numItems = 5;
fifo->size = size;
//return our pointer
return fifo;
}
FIFO_QUEUE?fifoQueue.queue?