Hello people,
I am writing a program in C that is supposed to read from a shared memory segment and print the data on the screen. The shared memory segment is organized as an array of 23 objects, where an object is a complex datatype defined in a separate .h file.
Now, a couple things am not sure of...the SIZE parameter of shmget is based on what? I know it's the size of the mem segment in bytes, but in my program, how can I figure that out?
Here's my code:
****************************************
Code:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
int main()
{
int shmid;
int m_key;
char *data;
int size;
m_key = 123456789;
size = 23*sizeof(int);
shmid = shmget(m_key, size, 0444);
data = shmat(shmid, NULL, SHM_RDONLY);
printf("Segment contains: \n%s\n", data);
if (shmdt(data) == -1)
{
perror("shmdt");
exit(1);
}
return 0;
}
****************************************
This code compiles, yet it only outputs a string out and exits. I initially had data[0],data[1], etc to be outputted...yet, that resulted in a segmentation fault!
Please, I'd appreciate any comment on how to go about this. I read several documents on shared memory, and it's not supposed to be hard at all, but I guess I am having a hard time implementing that.
Thank you!