我有两个进程 rwlock1(parent) 和 rwlock2(child) 。我想在进程之间使用读写锁,我需要转移pthread_rwlock_t mem_lock
到子进程,我有一个简单的代码,我该如何转移句柄。我不想使用互斥锁。
rwlock1(父)的代码
#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;
}