Hi there ...
I'm trying to build 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 ...
#include <iostream>
#include <sys/types.h>
#include <sys/ipc.h>
#include <string.h>
#include <sys/shm.h>
usingnamespace std;
enum e {Server,Client}; //enumeration that will be used to determine who wrote the message
struct chat // struct that will reside in the memory
{
string letter; //the message it self
bool ifread; //boolian variable to determine if the message has been raed or not
e who;
int x;
};
int main(void)
{
string name;
cout << "Welcome to the chat room application....please enter your Alias" << endl;
cin>>name; //taking the name that will be used in the chat
int shmid; //the id of the shard memory area
key_t key=ftok(".",'g');//generating a key for the shared memory
shmid=shmget(key,sizeof(chat),IPC_CREAT | 0666); //creating the shared memory
if (shmid<0)
{cout<<"an Error has happend please try again"<<endl;
return(1);
}
chat *str ; // pointer to the struct
str=(chat*)shmat(shmid,NULL,0);// attaching the shared memory area to the struct's pointer...
int temp; //temp area to store the read input
/*while(temp!=5) //keep waiting for the right input to quit
{
cout<<"please enter the num"<<endl;
cin>>temp;
if(str->ifread)
{str->ifread=false;
str->x=temp;
str->who=Server;
}
else if(!str->ifread && str->who==Client)
{
cout<<str->x<<endl;
}
else{sleep(1);}
}
*/
while(str->x!=5)
{
str->ifread=false;
cout<<"Enter #"<<endl;
cin>>str->x;
while(str->ifread==false)
{
cout<<"waiting"<<endl;
sleep(1);
}
cout<<str->x<<endl;
}
shmctl (shmid, IPC_RMID, NULL);
}
#include <iostream>
#include <sys/types.h>
#include <sys/ipc.h>
#include <string.h>
#include <sys/shm.h>
usingnamespace std;
enum e {Server,Client}; //enumeration that will be used to determine who wrote the message
struct chat // struct that will reside in the memory
{
string letter; //the message it self
bool ifread; //boolian variable to determin if the message has been raed or not
e who;
int x;
};
int main(void)
{
string name;
cout << "Welcome to the chat room application....please enter your Aliase" << endl;
cin>>name; //taking the name that will be used in the chat
int shmid; //the id of the shard memory area
key_t key=ftok(".",'g');//generating a key for the shared memory
shmid=shmget(key,sizeof(chat),0666); //requesting the shared memory
if (shmid<0)
{cout<<"an Error has happened please try again"<<endl;
return(1);
}
chat *str ; // pointer to the struct
str=(chat*)shmat(shmid,NULL,0);// attaching the shared memory area to the struct's pointer...
int temp; //temp area to store the read input
/* while(temp!=5) //keep waiting for the right input to quit
{
cout<<"please enter the num"<<endl;
cin>>temp;
if(str->ifread)
{str->ifread=false;
str->x=temp;
str->who=Client;
}
else if(!str->ifread && str->who==Server)
{
cout<<str->x;
}
else{sleep(1);}
}
*/
while(str->x!=5)
{
str->ifread=false;
cout<<"please #"<<endl;
cin>>str->x;
while(str->ifread==false)
{sleep(1);
cout<<"waiting"<<endl;
}
cout<<str->x<<endl;
}
shmctl (shmid, IPC_RMID, NULL);
}
Thanks in advance , and sorry for the bad English .....
You can't do that. If you're going to use shared memory, you can only put POD (plain old data) types in it; you know, int, long, double, char ... and arrays of those types.
Why? Because objects (like string) allocate memory and use pointers to that memory. A pointer is only meaningful within a process.