Gasim
Gasim

Reputation: 8001

Homework - reader write appender semaphore

I have a homework problem that I really don't know how to start.

Here is the question: "This problem makes use of the new synchronization primitive called the ReaderWriterAppender semaphore. The same rules apply with respect to readers and writers as with ReaderWriter semaphore, with the addition of these new rules for an appender:

1) No more than one appender can have the semaphore at any given time.
2) An appender and write cannot own the semaphore at the same time
3) Reads can take place during an append"

I need to implement it on my own.

There is a structure given

typedef struct {
   //structure here
} rwasem_t;

void rwalock_init(rwasem_t * rwa) {

}

void rwa_read_lock(rwasem_t * rwa) {

}

void rwa_write_lock(rwasem_t * rwa) {

}

void rwa_append_lock(rwasem_t * rwa) {

}

void rwa_append_unlock(rwasem_t * rwa) {

}

The problem is, I don't know how to approach the problem. I know what reader writer semaphore is, I know how to use them, and I have used them before. But I don't know how to implement them. They don't even tell me if I can use an API or anything. Basically, "implement it"

Thanks in advance,
Gasim

Upvotes: 1

Views: 375

Answers (1)

J-16 SDiZ
J-16 SDiZ

Reputation: 26930

Some hints:

  1. What is the different of appender and read?

  2. What if you just use a read lock for appender? Name one case they are different.

  3. Can I have add one more lock in the appender case to prevent the case in (2)?

Pseudo code:

ReadWriteLock lock_a, lock_b;

void rwa_read_lock(rwasem_t * rwa) {
    lock_a.do_somethingA();
    lock_b.do_somethingB();   // is this needed?
}

void rwa_write_lock(rwasem_t * rwa) {
    lock_a.do_somethingC();
    lock_b.do_somethingD();   // is this needed?
}

void rwa_append_lock(rwasem_t * rwa) {
    lock_a.do_somethingE();
    lock_b.do_somethingF();   // is this needed?
}

what is "do_somethingX()" ?

Is the order of locking important? If yes, what is the correct order?

Upvotes: 2

Related Questions