Results 1 to 2 of 2
HI,
I am trying to write structure in shared memory,
typedef struct _node{
char name[50];
char desc[50];
}node;
I am creating shared memory of size 11000 byte i.e
shmid = ...
- 07-01-2010 #1Just Joined!
- Join Date
- Jul 2010
- Posts
- 1
Writting structure in shared memry got SIGSEGV
HI,
I am trying to write structure in shared memory,
typedef struct _node{
char name[50];
char desc[50];
}node;
I am creating shared memory of size 11000 byte i.e
shmid = shmget(key,110*sizeof(node),IPC_CREAT|0666);
I am populating structure varaible using file that is mapped into memory
ptr = mmap(NULL,4096,PROT_READ |PROT_WRITE,MAP_SHARED,fd,0);
np = (node*)shmat(shmid,NULL,0);
shm = np;
while(file_size>0)
{
sem_wait(mutex);
memcpy(np->name,ptr,50);
ptr +=50;
memcpy(np->desc,ptr,50);
sem_post(mutex);
ptr +=50;
file_size -=100;
np +=100;
}
mapped file has 6 records of length 100 byte and adding 100 byte offset to shared memory pointer to write each record into Shared memory, but while writing 4th record , memcpy(np->name,ptr,50); gives segmentation fault and program stops executing.
can some one help me, I am in middle of my work, not able to go ahead.
- 07-04-2010 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Did you try to create a buffer that is a multiple of the page size (I see you are using 4096 for that in mmap())? Also, please post all of the relevant code, including how you are using all of the shared memory functions, creating fd for mmap(), etc. And please enclose the code in a
block for easier reading - it will retain indents and not interpret special sequences as "smileys".Code:// here is some code.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote