I only know POSIX thread has pthread_rwlock_t. I don't think shared memory in terms of IPC offering it...I guess you may need two Semaphores acting as read lock and write lock. Here is a read/write lock with Semaphores in Java, which gives a general idea. import java.util.concurrent.Semaphore;public class ReaderWriterLock { public ReaderWriterLock() { readers = 0; readLock = new Semaphore(1); readWriteLock = new Semaphore(1); } public void writeLock() throws InterruptedException { readWriteLock.acquire(); } public void writeUnlock() throws InterruptedException { readWriteLock.release(); } public void readLock() throws InterruptedException { readLockUnlock.acquire(); if(readers == 0) { readWriteLock.acquire(); } readers++; readLockUnlock.release(); } public void readUnlock() throws InterruptedException { assert readers > 0; readLockUnlock.acquire(); readers--; if(readers == 0) { readWriteLock.release(); } readLockUnlock.release(); } private Semaphore readLock; private Semaphore readWriteLock; private int readers; } Linux kernel actually has a Reader-Writer Semaphores as well. <linux/rwsem.h>/* attempt to acquire the semaphore for reading ... */ down_read(&mr_rwsem);/* critical region (read only) ... *//* release the semaphore */ up_read(&mr_rwsem);/* ... *//* attempt to acquire the semaphore for writing ... */ down_write(&mr_rwsem);/* critical region (read and write) ... *//* release the semaphore */ up_write(&mr_sem);As with semaphores, implementations of down_read_trylock() and down_write_trylock() are provided. Each has one parameter: a pointer to a reader-writer semaphore.
nice! never read UNP vol2. Good stuff! Ordered one on Amzon 2 minutes ago...Page 193: The read-write locks described in the previous chapter are allocated in memory as varibles of datatype pthread_rwlock_t. These variables can be within a single process when the read-write locks are shared among the threads within that process(the default), or within shared memory when the read-write locks are shared among the processees that share that memory (and assuming that the PTHREAD_PROCESS_SHARE attribute is specified when the read-write lock is initialized).
import java.util.concurrent.Semaphore;public class ReaderWriterLock { public ReaderWriterLock() {
readers = 0;
readLock = new Semaphore(1);
readWriteLock = new Semaphore(1);
} public void writeLock() throws InterruptedException {
readWriteLock.acquire();
} public void writeUnlock() throws InterruptedException {
readWriteLock.release();
} public void readLock() throws InterruptedException {
readLockUnlock.acquire();
if(readers == 0) {
readWriteLock.acquire();
}
readers++;
readLockUnlock.release();
} public void readUnlock() throws InterruptedException {
assert readers > 0;
readLockUnlock.acquire();
readers--;
if(readers == 0) {
readWriteLock.release();
}
readLockUnlock.release();
} private Semaphore readLock;
private Semaphore readWriteLock;
private int readers;
}
Linux kernel actually has a Reader-Writer Semaphores as well. <linux/rwsem.h>/* attempt to acquire the semaphore for reading ... */
down_read(&mr_rwsem);/* critical region (read only) ... *//* release the semaphore */
up_read(&mr_rwsem);/* ... *//* attempt to acquire the semaphore for writing ... */
down_write(&mr_rwsem);/* critical region (read and write) ... *//* release the semaphore */
up_write(&mr_sem);As with semaphores, implementations of down_read_trylock() and down_write_trylock() are provided. Each has one parameter: a pointer to a reader-writer semaphore.
楼主可以看下《unix网络编程 卷2 进程通信》这本书,里面包含posix和systemV的IPC,对api的使用有详细介绍,也有很多例子。读写锁在同步那章里,在最后也有讲共享内存。
建议看下,挺不错的。虽然现在没做这方面,但偶尔我也会翻着看下滴。
文件锁
线程锁 processshared
信号量
都可以实现
3楼推荐的那本书上都有~
// 若信号量值为1,获取资源并将信号量值-1
// 若信号量值为0,进程挂起等待int IPCz::sem_p(int sem_id) {
struct sembuf sbuf;
sbuf.sem_num = 0; /*序号*/
sbuf.sem_op = -1; /*P操作*/
sbuf.sem_flg = SEM_UNDO; if (semop(sem_id, &sbuf, 1) == -1) {
perror("P operation Error");
return -1;
}
return 0;
}// V操作:释放资源
// 释放资源并将信号量值+1
// 如果有进程正在挂起等待,则唤醒它们int IPCz::sem_v(int sem_id) {
struct sembuf sbuf;
sbuf.sem_num = 0; /*序号*/
sbuf.sem_op = 1; /*V操作*/
sbuf.sem_flg = SEM_UNDO; if (semop(sem_id, &sbuf, 1) == -1) {
perror("V operation Error");
return -1;
}
return 0;
}