Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7604887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:01:39+00:00 2026-05-31T00:01:39+00:00

I’m running into kind of a weird issue. I am making a shared memory

  • 0

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;

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:01:42+00:00Added an answer on May 31, 2026 at 12:01 am

    This:

    memcpy((void*)sharedBuff, (void*)fifo, 120); /* Why 120 ? */
    

    will copy 120 bytes from fifo: it will not copy the dynamically allocated array of fifo.queue. As the number of elements required for queue is hard-coded change the definition of FIFO_QUEUE to:

    typedef struct fifoQueue{
        int queue[25];
        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: always 25
    }FIFO_QUEUE;
    

    and change the memcpy() to:

    memcpy(sharedBuff, fifo, sizeof(*fifo));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.