开发者

Using binary semaphores as a counting semaphore

开发者 https://www.devze.com 2023-02-22 12:33 出处:网络
I am working on a project dealing with the reader writer problem. We are supposed t开发者_JAVA百科o use binary semaphores as a counting semaphore.

I am working on a project dealing with the reader writer problem.

We are supposed t开发者_JAVA百科o use binary semaphores as a counting semaphore.

I am not allowed to use semget/semop/semctl.

First how do I declare the semaphores? I want to use S and V as the semaphore names.

I am building this program in c++ and running it in unix. (g++)

ADD ON: The class provides one counting semaphore with methods:

  • waitSemaphore: if value > 0 unblocks, else blocks
  • signalSemaphore: decrement semaphore value by 1
  • deleteSemaphore: deletes the semaphore
  • Use the class, myCountingSemaphoreUsingBinarySemaphore, to solve the reader-writer problem
  • The readerCount should be a global integer variable.
  • Readers reads it.
  • Writer updates it by adding 10 to the previous value.
  • Reader/writer couts the database value (before/after).
  • No use of any of the conventional (counting) semaphore primitives like semget/semop /semctl since those are the ones you are simulating using the binary semaphore.


From the sounds of it, you have to make your own semaphore class. You could have a private member variable to increment/decrement as with a semaphore, and have a binary semaphore private member to make those increments/decrements atomic. Have release/acquire public methods that will do the inc/dec. When the counter goes to zero release the binary semaphore, and wait on a condition (another mutex). When another thread calls release on your semaphore class, and the counter is now above zero, signal to all those waiting on the condition to wake up, and try to reaquire your semaphore.

Hope this helps and makes sense.


As @Alexander Kondratskiy pointed out, it sounds like you are supposed to use a binary semaphore in order to implement a real semaphore. Here's part of a Sempahore implementation from one of my personal projects, you still need to fill in the blanks...


#ifndef SEMAPHORE_20100517_H_
#define SEMAPHORE_20100517_H_

#include <Scheduler.h>
#include <queue>

class Semaphore {
public:
    explicit Semaphore(int count);

public:
    void wait();
    void signal();

private:
    void block();
    void unblock();

private:
    int                           value_;
    std::queue<scheduler::thread> waitlist_;
    mutex                         mutex_;
};

#endif

Semaphore::Semaphore(int count) : value_(count) {
    assert(count >= 0);
}

void Semaphore::wait() { // same as your P()

    mutex_.lock();

    if(--value_ < 0) {
        mutex_.unlock(); // we have to give up the lock if we are going to block
        block();
        mutex_.lock(); // reacquire the lock for symmetry when we exit
    }

    mutex_.unlock();
}

void Semaphore::signal() { // same as your V()

    mutex_.lock(); 

    if(++value_ <= 0) {
        unblock();
    }

    mutex_.unlock();
}

void Semaphore::block() {
    // Fill in the blanks!
    // block the current thread and add it to the queue!
}

void Semaphore::unblock() {
    // Fill in the blanks!
    // pull someone from the queue and unblock them!
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号