pthread_mutexattr_getpshared, pthread_mutexattr_setpshared - set and get process-shared attribute
int pthread_mutexattr_getpshared(const pthread_mutexattr_t *attr,
int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr,
The pthread_mutexattr_getpshared() function obtains the value of the process-shared attribute from the attributes object referenced by attr. The pthread_mutexattr_setpshared() function is used to set the process-shared attribute in an initialised attributes object referenced by attr.
The process-shared attribute is set to PTHREAD_PROCESS_SHARED to permit a mutex to be operated upon by any thread that has access to the memory where the mutex is allocated, even if the mutex is allocated in memory that is shared by multiple processes. If the process-shared attribute is PTHREAD_PROCESS_PRIVATE, the mutex will only be operated upon by threads created within the same process as the thread that initialised the mutex; if threads of differing processes attempt to operate on such a mutex, the behaviour is undefined. [b]The default value of the attribute is PTHREAD_PROCESS_PRIVATE[/b]
I am not able to understand the difference between the shared memory created using creat() and shm_open(). Able to print some variable initialized in one process on the other process when used creat() call for making shared memory and share the variable. But when tried to lock the mutex same way, the program crashed giving following error
Finally got the shared mutex working fine with both creat() and shm_open() calls. The only change done was adding MAP_FIXED flag in mmap call after which the memory got mapped to the required location which was not happening without this flag.
This is done for a driver which handle communication between PowerPC core and DSP core using hardware FIFO’s. QNX is running on PPC. The driver was originally written for Linux when linux was running on PPC. I am also not very sure of the design since it was not documented. Also I am very new to QNX. So I would like to understand more about this issue.
How are you telling that the mutex is in the device memory? What I was thinking is that the address 0x40000000 is just a virtual address in process address space where the shared memory region should be mapped.
What you mean by stating device memory? Is it the physical memory, I mean DRAM address?
Another interesting thing I noticed was, when the MAP_FIXED was not there in flags, the PROCESS1 mmap() return address was 0x40122000 and the PROCESS2 mmap() return address was 0x40000000 even though the mmap() call was same,
ptr = mmap(0x40000000, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Following is the structure shared using shared memory between processes,
The variable test pattern was initialized to 0xabcdef in PROCESS1 and after the mmap() in PROCESS2, i was able to print the value properly. But when tried to lock the mutex, the program crashed. Any idea why is this happening?
Sorry my bad. The first arg is not the physical address, but the VA hint. I was thinking that this was the address specified as the last arg (the physical offset).
Ok, so it sounds as if the code you’re porting assumes that the pointer address returned by the mmap call will be the same in different processes (I am surprised it worked on Linux).
You need to change the mmap call to set the first arg to zero; remove MAP_FIXED, then adjust the code to derive pointers into the shared memory area by adding offsets to the pointer returned from mmap().
Is there any place in this code where the last arg to mmap is non-zero?