I have a problem with shared memory and processes. I attach an area to the shared memory, I make a child proccess but when I try to modify the shared memory area in the father process I only get segmentation fault, it just can be modified in the child.
.h
struct infoHeaders {
char words[512];
int num [512];
};
int idShMem;
struct infoHeaders * ptrInfo;
.cc
idShMem = shmget( 123456, sizeof(struct infoHeaders), 0700 | IPC_CREAT );
ptrInfo = (infoHeaders *) shmat( idShMem, NULL, 0 );
if (!fork()) {
sem.wait();
exit(0);
}
else {
ptrInfo->num[0] = 1; //Segmentation Fault
sem.signal();
}
}
sem is an object of a Semaphore class, I tested it and it has no problems.
Any ideas ?
ptrInfo? You don't seem to check for an error return fromshmat(). Does your code behave differently if you try to useptrInfowithout callingfork()?fork()call actually relevant? In any case, please state which Unix system you are using and extract a minimal but complete example. See also stackoverflow.com/help/how-to-ask