Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I'm trying to write 4 programs all reading from shared memory. I can write 1 reader and 1 writer program. The problem is when I try to get my program reading from shared memory to add a number on to the front of the message in my shared memory for a third program to read it. I have added my reader code that I'm using to try to do this. Any help would be appreciated.
using namespace std;
#define TEXT_SIZE 2000
struct shared_memory_struct // definition
int written_flag; // this flag will be set: to 0 when there
// is nothing new in shared memory to read
char some_text [TEXT_SIZE];
int main ( )
int running = 1;
struct shared_memory_struct *shared_data; // declaration
void *shared_memory = (void *)0;
// This is just to take an input to continue at end.
char *concat2 = buffer;
/* Create a shared memory segment with pre-selected key (param 1).
* The size of the created segment will be the size of the struct
* we have set up to handle the memory (param 2). param 3 indicates
* that owner, group, and world can assess the shared memory (0666)
* and that the shared memory segment should be created if it doesn't
* already exist (IPC_CREAT).
shmid = shmget ((key_t)4321, sizeof(struct shared_memory_struct), 0666 | IPC_CREAT);
/* If the sharedmemory segment is successfully created (or was
* already successfully created by another process) shmget will
* return the id of the segment. It will return -1 if there was
* an error.
if (shmid == -1)
cerr << "shmget failed" << endl;
/* Attach to the shared memory to use it. param 1 is the
* id of the segment returned by shmget. param 2 allows us to specify
* the address at which we want the shared memory to be attached.
* If this is a null pointer (as in this example), the system will
* choose for us where to attach the shared memory. param 3 is a
* set of bitwise flags.
shared_memory = shmat(shmid, (void *)0, 0);
/* If the attach is successful, it will return a pointer to the
* first byte of the shared memory. If unsuccessful, it will
* return -1.
if (shared_memory == (void *)-1)
cerr << "shmat failed" << endl;
cout << "Memory attached at :" << (int)shared_memory
<< endl << " with shmid: " << shmid << endl;
/* Note that the following "maps" our structure to the shared
* memory segment.
(struct shared_memory_struct *)shared_memory;
/* Assume the reader program is started before the writer.
* Set the flag to indicate that we are ready to read.
shared_data->written_flag = 0;
/* We will stay in the while loop continuously checking
* the flag in shared memory until it is set to 1.
/* Once the flag is set, read the information and
cout << "You wrote: " <<
shared_data->some_text << endl;
concat2 = ("021804 %s", buffer);
strcpy (shared_data->some_text, concat2);
// Wait a bit so the writer needs to wait
// Reset the flag so program 3 can read data
shared_data->written_flag = 2;
// If user entered "end" for other process, it's
// time to exit.
if (strncmp (shared_data->some_text,
"end", 3) == 0)
running = 0;
} // end if
} // end while
// Detach from shared memory.
if (shmdt(shared_memory) == -1)
cerr << "shmdt failed" << endl;
cout << "We have detached from shared memory, but have not deleted it" << endl << "Hit any key, then enter to continue" << endl;
cin >> buffer;
/* This actually removes the shared memory from the system.
* param 1 tels which segment, param 2 is the command to shmctl
* (which can perform several actions) to remove the memory, and
* param 3 is not used (set to 0) when param 2 is set to IPC_RMID.
if (shmctl(shmid, IPC_RMID, 0) == -1)
cerr << "shmctl(IPC_RMID) failed" << endl;