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 7617603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:11:28+00:00 2026-05-31T03:11:28+00:00

I have 2 programs a writer and a reader. The writer is supposed to

  • 0

I have 2 programs a writer and a reader. The writer is supposed to create shared memory and then save an array of structs to that piece of memory… reader is supposed to use that piece of memory and be able to output what was saved in the memory by the writer. I’m very much struggling to output more then just the first part of the array so I’m not even sure if the array is saving correctly to the shared memory so i said I’d post my code here and maybe someone could help me out…

WRITER:

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/shm.h>
#include <unistd.h>
#include "header.h"


int main()
{
    key_t key = 1234;
    int shmid;
    int i = 0;
    int p;
    struct companyInfo * pdata[4];

    for (i = 0; i < 5; i++)
    {
        pdata[i] = malloc(sizeof(struct companyInfo));
        p = sizeof(struct companyInfo);
        //printf("size: %d\n", p);
        //printf("look: %x\n", pdata[i]);
    }

    int sizeOfCompanyInfo = sizeof(struct companyInfo);

    int sizeMem = sizeOfCompanyInfo*5;

    shmid = shmget(key, sizeMem, 0644 | IPC_CREAT);
    if(shmid == -1)
    {
        perror("shmget");       
        exit(1);
    }

    *pdata = (struct companyInfo*) shmat(shmid, (void*) 0, 0);
    if(*pdata == (struct companyInfo*) -1)
    {
        perror("schmat error");
        exit(1);
    }

    strcpy(pdata[0]->companyName,"AIB");
    pdata[0]->sharePrice = 11.2;
    strcpy(pdata[1]->companyName,"BOI");
    pdata[1]->sharePrice = 10.2;
    strcpy(pdata[2]->companyName,"TSB");
    pdata[2]->sharePrice = 9.2;


    printf("name is %s and %f \n",pdata[0]->companyName,pdata[0]->sharePrice);
    printf("name is %s and %f \n",pdata[1]->companyName,pdata[1]->sharePrice);
    printf("name is %s and %f \n",pdata[2]->companyName,pdata[2]->sharePrice);

    exit(0);

}

READER:

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/shm.h>
#include <unistd.h>
#include "header.h"

int main()
{
    key_t key = 1234;
    int shmid;
    int sizeMem = 100;

    struct companyInfo * pdata[4];

    //int sizeOfCompanyInfo = sizeof(pdata);

    //printf("Size: %d\n", sizeOfCompanyInfo);

    shmid = shmget(key, 0, 0);
    if(shmid == -1)
    {
        perror("shmget");       
        exit(1);
    }

    *pdata = (struct companyInfo*) shmat(shmid,(void*)0,0);
    if(*pdata==(struct companyInfo*) -1)
    {
        perror("shmat error");
        exit(1);
    }

    printf("The id is %d\n",shmid);

    printf("Bank is %s and %f . \n",pdata[0]->companyName,pdata[0]->sharePrice);
    printf("Bank is %s and %d . \n",pdata[1]->companyName,pdata[1]->sharePrice);
    printf("Bank is %s and %d . \n",pdata[2]->companyName,pdata[2]->sharePrice);
    exit(0);
}

HEADER:

struct companyInfo
{
    double sharePrice;
    char companyName[100];
}; 
  • 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-31T03:11:29+00:00Added an answer on May 31, 2026 at 3:11 am

    The problem is that your pdata is an array of pointers to structs, and when you do the shmat() you set only the first pointer in your array (*pdata). So when you write to the structs, only the zero’th is actually going into shared memory, the others are going to the space you malloc’d earlier (which you shouldn’t be doing).

    The correct way is something like this:

    int main()
    {
        key_t key = 1234;
        int shmid;
        int i = 0;
        int p;
        struct companyInfo *pdata;
        int ncompanies = 5;
    
        int sizeMem = sizeof(*pdata) * ncompanies;
    
        shmid = shmget(key, sizeMem, 0644 | IPC_CREAT);
        if(shmid == -1)
        {
            perror("shmget");       
            exit(1);
        }
    
        pdata = (struct companyInfo*) shmat(shmid, (void*) 0, 0);
        if(pdata == (void*)-1)
        {
            perror("schmat error");
            exit(1);
        }
    
        strcpy(pdata[0].companyName,"AIB");
        pdata[0].sharePrice = 11.2;
        strcpy(pdata[1].companyName,"BOI");
        pdata[1].sharePrice = 10.2;
        strcpy(pdata[2].companyName,"TSB");
        pdata[2].sharePrice = 9.2;
    
        exit(0);
    
    }
    

    This stores all the structs in the shared memory rather than just pointers.

    Along with appropriate changes to the reader this works (I’ll leave that as an exercise for you, for now).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two programs, Writer and Reader. I have a FIFO from Writer to
I have two (UNIX) programs A and B that read and write from stdin/stdout.
I have a program that I need to use signals and handlers with. I
We use SharpSVN to programmatically access SVN repositories. Now we have the problem that
I have a web application that uses a couple of PDF forms to create
I have a simple txt file that will save only 1 word, but whenever
I have a pair of shell programs that talk over a named pipe. The
I have a program that writes to a FILE *cgiOut and just after it
I have to write a program that read from a file that contains the
I have to write a program that sniffs network packets (part1-the simple part). And

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.