1

gen semaphore implemented using binary semaphores:

click image please, gen semaphore implemented using binary semaphores

So I am having trouble understanding why we need the entry semaphore, I can see how it works correctly without it. How can multiple processes enter the critical section? After the first process enters, it does wait(mutex), so that means that no one else can get in,furthermore there are other processes waiting for signal(mutex)

A general semaphore can allow multiple processes to enter the critical section area but I cannot see how that is done in this code.

2
  • 1
    why down vote, i dont speak much english well, please edit if need be Commented Dec 16, 2016 at 6:16
  • The entry semaphore prevents multiple processes from waiting on the delay semaphore at the same time. Also prevents c from going lower than -1. Commented Dec 16, 2016 at 7:39

2 Answers 2

0

After seeing your question image, The purpose for entry semaphore is allow only single process/thread to wait for lock, if you don't use it other process will go in wait queue.

why we need the entry semaphore

  • entry semaphore is not initialized with any value and if it is declared globally then it will be initialized with 0. So if entry semaphore is 0, wait(entry) will allow only single process to enter because wait() functionality check if entry value is less the zero then process will go in wait queue.

How can multiple processes enter the critical section?

  • Only one process can be in the critical section at a time -- otherwise what critical section?

  • A Critical Section is a code segment that accesses shared variables and has to be executed as an atomic action. It means that in a group of cooperating processes, at a given point of time, only one process must be executing its critical section. If any other process also wants to execute its critical section, it must wait until the first one finishes.

A general semaphore can allow multiple processes to enter the critical section area but I cannot see how that is done in this code.

It is not right, if you allow multiple processes to critical section who want to modify shared data, then you change the mean of critical section. you will get wrong data at the end of process.

General semaphore can be used to allow multiple processes to access critical data if processes read the shared data only, don't modify the shared data.

I have very small code for you to show how semaphore works and how can multiple processes can allow to access shared data. you can take it as multiple reader and a writer.

semaphore mutex = 1;                 // Controls access to the reader count
semaphore db = 1;                    // Controls access to the database
int reader_count;                    // The number of reading processes accessing the data

Reader()
{
  while (TRUE) {                     // loop forever
     down(&mutex);                          // gain access to reader_count
     reader_count = reader_count + 1;       // increment the reader_count
     if (reader_count == 1)
         down(&db);                         // if this is the first process to read the database,
                                            // a down on db is executed to prevent access to the 
                                            // database by a writing process
     up(&mutex);                            // allow other processes to access reader_count
     read_db();                             // read the database
     down(&mutex);                          // gain access to reader_count
     reader_count = reader_count - 1;       // decrement reader_count
     if (reader_count == 0)
         up(&db);                           // if there are no more processes reading from the 
                                            // database, allow writing process to access the data
     up(&mutex);                            // allow other processes to access reader_countuse_data();
                                            // use the data read from the database (non-critical)
}

Writer()
{
  while (TRUE) {                     // loop forever
     create_data();                         // create data to enter into database (non-critical)
     down(&db);                             // gain access to the database
     write_db();                            // write information to the database
     up(&db);                               // release exclusive access to the database
}
Sign up to request clarification or add additional context in comments.

Comments

0

How can multiple processes enter the critical section? After the first process enters, it does wait(mutex), so that means that no one else can get in… A general semaphore can allow multiple processes to enter the critical section area

It's a bit unfortunate that you speak of a critical section here, since a critical section is generally understood as a program's part that cannot be executed by more than one process at a time. What you actually mean is probably that a general (counting) semaphore can allow multiple processes to share (a pool of) resources. The implementation you present is indeed able to do this, because: After the first process enters and does wait(mutex), it immediately continues, since mutex has been initialized to 1. Then, if the decremented c is not less than 0, it does signal(mutex), so another process can get in.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.