有三个不同的进程Pa、Pb、Pc,其中Pa对一段共享内存进行写,Pb和Pc对这段共享内存进行读。如何实现三个进程的读写锁?即Pa进程对共享内存进行写时,Pb和Pc对这段共享内存不能进行访问,Pb或Pc对共享内存进行读时,另一个进程可以对这段共享内存进行读访问。

解决方案 »

  1.   

    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. 
      

  2.   

              很久没有写这方面代码了,这个是属于进程间通信,现成代码没有,建议倒有。
              楼主可以看下《unix网络编程 卷2 进程通信》这本书,里面包含posix和systemV的IPC,对api的使用有详细介绍,也有很多例子。读写锁在同步那章里,在最后也有讲共享内存。
              建议看下,挺不错的。虽然现在没做这方面,但偶尔我也会翻着看下滴。
      

  3.   

           相信楼主有了这本书,IPC就不再是问题了。
      

  4.   

    方法很多啊
    文件锁
    线程锁 processshared
    信号量
    都可以实现
    3楼推荐的那本书上都有~
      

  5.   

    《unix网络编程》 卷2 :进程通信 有介绍,把互斥锁、读写锁 等同步变量建在共享内存区就行了,也就是用一个数据结构,如结构体表示共享内存区,这个结构体包含一个互斥锁或读写锁。
      

  6.   

    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). 
      

  7.   

    通过信号量实现读写的原子操作。// P操作:访问资源
    //  若信号量值为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;
    }