In my code I do the following initialization :
struct PipeShm myPipe = { .init = 0 , .flag = FALSE , .mutex = NULL , .ptr1 = NULL , .ptr2 = NULL ,
.status1 = -10 , .status2 = -10 , .semaphoreFlag = FALSE };
int initPipe()
{
if (!myPipe.init)
{
myPipe.mutex = mmap (NULL, sizeof *myPipe.mutex, PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (!sem_init (myPipe.mutex, 1, 0)) // semaphore is initialized to 0
{
myPipe.init = TRUE;
}
else
perror ("initPipe");
}
return 1; // always successful
}
I can have multiple processes that can be invoked from main() (note the fork) .
Thanks
AFAICS your error is in your control variables. Only your
mutexvariable is shared between the processes, not yourinitorflagvariables. These are copy on write, so you wouldn’t see the changes in a different process.You’d have to pack all of your control variables inside the segment that you create. Create an appropriate
structtype for all the fields that you need.BTW, calling a semaphore
mutexis really a bad idea. A mutex has a semantic that is quite different from a semaphore. (Or if you really use it as a mutex, I didn’t check, usepthread_mutex_twithpsharedin the initializer.)Edit after your edit: No it wouldn’t work like this. You really have to place the whole
structin the shared segment. So yourstruct PipeShmmust contain asem_t semand not asem_t* mutex. Then you’d do something likeOther things you should be aware of:
sem_tinterfaces can be interrupted by any kind of IO or other signals. You always have to checkthe return of these functions and in particular restart the function
if it received
EINTR.<stdbool.h>through names ofbool,falseandtrue.