Co3 Eos
Co3 Eos
A.Siddhartha
Asst. Professor
To familiarize students with the scheduling and different types of cpu scheduling algorithms
INSTRUCTIONAL OBJECTIVES
LEARNING OUTCOMES
The wait() method controls the entry to the critical section, whereas the signal() function
controls the exit.
CREATED BY K. VICTOR BABU
The following are the four most important parts of the critical section:
• Entry Section: It is a step in the process that determines whether or
not a process may begin.
• Critical Section: This section enables a single process to access and
modify a shared variable.
• Exit Section: Other processes waiting in the Entry Section are able to
enter the Critical Sections through the Exit Section. It also ensures that
a process that has completed its execution gets deleted via this
section.
• Remainder Section: The Remainder Section refers to the rest of the
code that isn't in the Critical, Entry, or Exit sections.
wait(S)
{
while (S<=0);
S--;
}
CREATED BY K. VICTOR BABU
Signal()
The Signal Semaphore Operation is used to update the value of Semaphore.
The Semaphore value is updated when the new processes are ready to enter
the Critical Section.
The Signal Operation is also known as:
• Wake up Operation
• Up Operation
• Increase Operation
• V Function (most important alias name for signal operation)
We know that the semaphore value is decreased by one in the wait operation
when the process left the critical state. So, to counter balance the decreased
number 1 we use signal operation which increments the semaphore value.
This induces the critical section to receive more and more processes into it.
signal(S)
{
S++;
}
CREATED BY K. VICTOR BABU
Types of Semaphores
1)Initialization:
• Semaphore mutex = 1; // Controls buffer access
• Semaphore empty = N; // Number of empty slots (buffer size)
• Semaphore full = 0; // Number of filled slots
• while(TRUE)
•{
• wait(w);
•
• /* perform the write operation */
•
• signal(w);
•}
•
if(read_count == 1) }Entry sec
• wait(w);
• //release lock
• signal(m);
CREATED BY K. VICTOR BABU
• /* perform the reading operation */
•
• // acquire lock
• wait(m);
• read_count--;
• if(read_count == 0)
•
signal(w); }Exit sec
•
• // release lock
• signal(m);
•}
CREATED BY K. VICTOR BABU
Here is the Code uncoded(explained)
• As seen above in the code for the writer, the writer just
waits on the w semaphore until it gets a chance to write
to the resource.
• After performing the write operation, it increments w so
that the next writer can access the resource.
• On the other hand, in the code for the reader, the lock is
acquired whenever the read_count is updated by a
process.
• When a reader wants to access the resource, first it
increments the read_count value, then accesses the
resource and then decrements the read_count value.
CREATED BY K. VICTOR BABU
• The semaphore w is used by the first reader which enters the
critical section and the last reader which exits the critical
section.
• The reason for this is, when the first reader enters the critical
section, the writer is blocked from the resource. Only new
readers can access the resource now.
• Similarly, when the last reader exits the critical section, it
signals the writer using the w semaphore because there are
zero readers now and a writer can have the chance to access
the resource.
• Mutual Exclusion: Only one process can use a resource at any given
time i.e. the resources are non-sharable.
Example:
Let’s say:
• P1 is requesting R1
• R1 is assigned to P2
• P2 is requesting R2
• R2 is assigned to P1
• Approach-01:
• In this approach,
• A process has to first request for all the resources it requires for execution.
• Once it has acquired all the resources, only then it can start its execution.
• This approach ensures that the process does not hold some resources and
wait for other resources.
In this approach,
• A process is allowed to acquire the resources it desires at the current
moment.
• After acquiring the resources, it start its execution.
• Now before making any new request, it has to compulsorily release all the
resources that it holds currently.
• A process can request a resource only when that process is holding no
resources.(No hold is here only wait is there)
• This approach is efficient and implementable.