I have two processes rwlock1(parent) and rwlock2(child) . i want to use read-writer lock between processes , i need to transfer pthread_rwlock_t mem_lock
to child process, i have a simple code , how can i trasfer the handle . i dont want to use a mutex.
rwlock1(parent)'s code
#include <unistd.h>
#include <pthread.h>
#include <sys/wait.h>
pthread_rwlock_t mem_lock;
int main() {
pid_t pid;
//init attr
pthread_rwlockattr_t mem_lock_attr;
pthread_rwlockattr_init(&mem_lock_attr);
pthread_rwlockattr_setpshared(&mem_lock_attr, 1);
//init read writer lock
pthread_rwlock_init(&mem_lock, &mem_lock_attr);
pid = fork();
if (pid == 0) {
execl("rwlock2", "rwlock2", (char *) nullptr);
}
//wait child
wait(nullptr);
return 0;
}
I haven't tried, but it looks like this can be done if you put the lock in shared memory (e.g. set up by mmap
or shmget
) and use pthread_rwlockattr_setpshared
to set the lock as process-shared.
See also 2.9.9 Synchronization Object Copies and Alternative Mappings in IEEE Std 1003.1-2017.