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

  • Home
  • SEARCH
  • 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 7873527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:32:27+00:00 2026-06-03T02:32:27+00:00

I’m trying to build an application that has two process that uses Shared memory

  • 0

I’m trying to build an application that has two process that uses Shared memory to exchange messages…
what I’m doing as you will see , is requesting for a shared memory and then putting a struct in it .
the struct consist of a string , Bool flag and an enum values ..
the string should hold the message , the flag should tell whether this message has been seen or not by the other side (cause no one is allowed to add a message if there is a unread message in the memory )
I’m suffering from several problems
1- i cant reach the string at the string….
2- when i replaced the string with an int , i face a problem at the client side when trying to get to the memory
and this is the code …

Server’s side:

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <iostream>
#include <string>
#include <sys/wait.h>
using namespace std;
enum e {Server,Client};

struct chat             // struct that will reside in the memory
{


    bool ifread;        //boolian variable to determin if the message has been raed or not
    e who;
    char message[50];
    int msgl;


};

int main(void)
{
string temp;
int shmid;
//key_t key=ftok(".",'a');
key_t key=433;

if ((shmid = shmget(key, sizeof(chat), IPC_CREAT | 0666)) < 0)
{
cout<<"shmget"<<endl;
return(1);
}

chat *str ;
str = (chat *)shmat(shmid, NULL, 0);

pid_t pid;
pid=fork();
str->ifread==true;

str->who=Server;
if(pid==0)
{

    while(temp!="bye")
    {
        if(str->ifread==false && str->who==Client)
        {
            //cout<<"Client said : ";

            for(int i=0;i<str->msgl;i++)
            cout<<str->message[i];
    str->ifread==true;

        }

    }

}

else if (pid>0)
{
    while(temp!="bye")
    {
    getline(cin,temp);
    str->msgl=temp.length();
    if(str->ifread)
     {
        str->who=Server;
        for(int i=0;i<str->msgl;i++)
         {
            str->message[i]=temp.at(i);
         }

    str->who=Server;
    str->ifread==false;
      }

    else if (!str->ifread && str->who==Client)
       {
    sleep(1);
    waitpid(pid,NULL,0);
        }
}

}

shmctl (shmid, IPC_RMID, NULL);

}

Client’s side:

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <iostream>
#include <string>
#include <sys/wait.h>
using namespace std;
enum e {Server,Client};

struct chat             // struct that will reside in the memory
{


    bool ifread;        //boolian variable to determin if the message has been raed or not
    e who;
    char message[50];
    int msgl;


};

int main(void)
{
string temp;
int shmid;
//key_t key=ftok(".",'a');
key_t key=433;

if ((shmid = s`hmget(key, sizeof(chat),  0666)) < 0)
{
cout<<"shmget"<<endl;
return(1);
}

chat *str ;
str = (chat *)shmat(shmid, NULL, 0);

pid_t pid;
pid=fork();


if(pid==0)
{

    while(temp!="bye")
    {
        if(str->ifread==false && str->who==Server)
        {
            //cout<<"Server said : ";

            for(int i=0;i<str->msgl;i++)
            cout<<str->message[i];
    str->ifread==true;

        }

    }

}

else if (pid>0)
{
    while(temp!="bye")
    {
    getline(cin,temp);
    str->msgl=temp.length();
    if(str->ifread)
     {
        str->who=Client;
        for(int i=0;i<str->msgl;i++)
         {
            str->message[i]=temp.at(i);
         }
    str->ifread=false;

      }

    else if (!str->ifread && str->who==Server)
       {
    sleep(1);
    //waitpid(pid,NULL,0);
        }
}

}

shmctl (shmid, IPC_RMID, NULL);

}

Thanks in advance , and sorry for the bad English …..

edit:
thanks aix but there is another problem ,i.e the client first couldn’t get any data from the shared memory even when i used the int x to exchange numbers between them
the client first kept giving 0 even that the server has placed another value , and after a while it started giving me an error when calling shmget();

edit(2): i did some changes ,yet it still not working ….

Edit(3): Problem Solved Thank you all guys , turned out to be bad ordering to the flags
thanks Again …

  • 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-06-03T02:32:29+00:00Added an answer on June 3, 2026 at 2:32 am

    The main problem is that under the covers std::string uses heap allocation.

    This means that

    struct chat
    {
       string  letter;
    

    doesn’t place the entire string into the struct. It places the string object, but not necessarily the character data associated with the string.

    One way to fix this is by replacing std::string with char[N].

    In general, extra care should be taken when using shared memory with anything that involves pointers or references.

    Also, to avoid complications around construction/destruction, it may be best to make sure that chat is a POD.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

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.