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
}
entrysemaphore prevents multiple processes from waiting on thedelaysemaphore at the same time. Also preventscfrom going lower than-1.