Is is necessary to attach / dettach the shared memory segments for write operations , if more than one program is accessing same shared memory segments..
I have used semaphore mutex and still I'm getting segmentation fault when I write to the segment when other program is already attached to it... Is something else wrong..
Only one process (at a time) may have an open file descriptor for a shared memory segment. The interprocess communication that is used to insure proper file access is the semaphore.
When a semaphore is used correctly (and it can be tricky!) the problem with shared memory segmentation error will be solved (more than likely, unless you have a pointer or other C programming error).
Here is a basic algorithm:
[list=1]
[]Create a shared memory segment.
[]Create a semaphore.
[]Process A acquires the semaphore used for the shared memory segment.
[]Process A opens the shared memory.
[]Process A does cool things with shared memory.
[]Process A closed the shared memory segment.
[]Process A releases the semaphore.
[]Process B has been waiting for the semaphore to be released.
[]Now that semaphore has been released......
[]Process B acquires the semaphore used for the shared memory segment.
[]Process B opens the shared memory.
[]Process B does cool things with shared memory.
[]Process B closed the shared memory segment.
[]Process B releases the semaphore.
[/list=1]
Yadda, yadda, yadda....... does this help?