0% found this document useful (0 votes)
15 views

Week 10 & 11

Uploaded by

hammad.ma.malik
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Week 10 & 11

Uploaded by

hammad.ma.malik
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

CS-303 Operating Systems

Dr. Samia Ijaz


Fall - 2024

Department of Computer
Science
HITEC University Taxila
Operating Systems
Week 10 & 11

Operating System Concepts – 9th Silberschatz, Galvin and Gagne


Process Synchronization

● Cooperating Processes:
● A process that can affect or be affected by other
processes executing in the system
● Can be allowed to share data
● Concurrent access to shared data may result in data
inconsistency
● Process synchronization is needed for data
consistency
● the way by which processes that share same
memory space are managed in an OS.

Operating System Concepts – 9th 5.3 Silberschatz, Galvin and Gagne


Process Synchronization

Operating System Concepts – 9th 5.4 Silberschatz, Galvin and Gagne


Process Synchronization

Operating System Concepts – 9th 5.5 Silberschatz, Galvin and Gagne


Background

● Producer Consumer Problem:

Operating System Concepts – 9th 5.6 Silberschatz, Galvin and Gagne


Process Synchronization

● Suppose that we wanted to provide a


solution to the consumer-producer
problem.
● We can do so by having an integer
counter that keeps track of the number
of buffer slots that are currently full.
● Initially, counter is set to 0. It is
incremented by the producer after it
produces a new buffer and is
decremented by the consumer after it
consumes a buffer.

Operating System Concepts – 9th 5.7 Silberschatz, Galvin and Gagne


Producer

while (true) {
/* produce an item in next produced */

while (counter == BUFFER_SIZE) ;


/* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
counter++;
}

Operating System Concepts – 9th 5.8 Silberschatz, Galvin and Gagne


Consumer

while (true) {
while (counter == 0)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
/* consume the item in next consumed */
}

Operating System Concepts – 9th 5.9 Silberschatz, Galvin and Gagne


● Suppose that the value of the variable counter is
currently 5.
● The producer and consumer processes execute
the statements "counter++" and "counter--"
simultaneously.
● Following the execution of these two statements,
the value of the variable counter may be 4, 5, or
6.
● The only correct result, though, is counter = 5,
which is generated correctly if the producer and
consumer execute separately.

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


counter++" may be implemented in machine
language (on a typical machine) as:
•register1 = counter
•register1 = register1 + 1
•counter = register1
counter--" may be implemented in machine
language (on a typical machine) as:
•register2 = counter
•register2 = register2 - 1
•counter = register2

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne
Race Condition
● A situation like this in which several processes
access and manipulate the same data
simultaneously and outcome of the execution
depends on the particular order in which the
access takes place is called race condition.

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


Critical Section Problem

● Consider system of n processes {p0, p1, …


pn-1}
● Each process has critical section segment
of code
● Process may be changing shared data
such as common variables, updating
table, writing file, etc
● When one process in critical section, no
other may be in its critical section
● Critical section problem needs to design
algorithms / protocols to solve this
● Each process must ask permission to enter
critical section in entry section, may follow
critical section with exit section, then
remainder
Operating System Concepts – 9
th section 5. Silberschatz, Galvin and Gagne
Critical Section Problem
A critical section is a segment of code which
can be accessed by a single process at a
specific point of time.
The section consists of shared data resources
that are required to be accessed by other
processes.

Memory

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


Process P1 P2
● { ● {
● // entry section ● // entry section
● ----------
● //critical section ● //critical section
● // exit section ● // exit section
● ------} ● }
● //remainder section ● //remainder section

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


Solution to Critical-Section Problem

Conditions that must be satisfied:


1. Mutual Exclusion - If process Pi is executing
in its critical section, then no other processes can
be executing in their critical sections
2. Progress - If no process is executing in its
critical section and there exist some processes
that wish to enter their critical section, then the
selection of the processes that will enter the
critical section next cannot be postponed
indefinitely
3. Bounded Waiting - A bound must exist on the
number of times that other processes are allowed
to enter their critical sections after a process has
made a request to enter its critical section and
before that request is granted

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


Peterson’s Solution
● A classic software-based solution to critical section
problem
● Restricted to only two processes
● The two processes share two variables:
● int turn;
● Boolean flag[2]

● The variable turn indicates whose turn it is to enter


the critical section
● The flag array is used to indicate if a process is
ready to enter the critical section. flag[i] = true
implies that process Pi is ready!

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


Peterson’s Solution

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


Mutex Locks

● Software approach to solve critical section


problem
● Mutex lock is simplest approach to process
synchronization
● Used to achieve mutual exclusion
● In the entry section of code, a LOCK is
obtained over the critical resources used inside
the critical section. In the exit section that lock
is released.
● acquire() to get a lock then release() to
release the lock
● Boolean variable indicating if lock is
available or not
● Calls to acquire() and release() must be
atomic
● Causes Busy/ waiting problem
Operating System Concepts – 9
th 5. Silberschatz, Galvin and Gagne
acquire() and release()

● acquire() {
while (!available); /* busy wait */
available = false;
}
● release() {
available = true;
}
● do {
acquire()
critical section
release()
remainder section
} while (true);

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


Process P1 P2
● { ● {
● // entry section ● // entry section
acquire() ● ----------
● //critical section
● //critical section ● // exit section
● // exit section ● }
release()
● //remainder section
● ------}
● //remainder section

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


Synchronization Hardware

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


test_and_set Instruction

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


Semaphore
● A technique to manage concurrent processes
to achieve process synchronization in a
multiprocessing environment.
● A semaphore is simply a non-negative integer
value, used to solve the critical section
problem
● Initialized in the beginning
● Then can only be accessed through two
operations:

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


Process P1 P2
● { ● {
● // entry section wait() ● // entry section wait()
● ----------
● //critical section ● //critical section
● // exit section signal() ● // exit section signal ()
● }
● ------} ● //remainder section
● //remainder section

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne
Semaphore
● Binary semaphore – integer value can range
only between 0 and 1
● Behaves like mutex lock
● Counting semaphore – integer value can
range over an unrestricted domain
● Used to control access to a resource that
has multiple instances.

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


Disadvantages of Semaphores

● Busy waiting:
● When one process is in the critical section, any
other process that tries to access the critical
section must loop continuously in the entry
section code.
● Wastes the CPU cycles that some other
processes might be able to use productively.
● This type of semaphore is also called spinlock
because the process spins while waiting for the
lock.

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


Disadvantages of Semaphores

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


Disadvantages of Semaphores -
Deadlocks

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


Classical Problems of Synchronization

● Classical problems used to test newly-proposed


synchronization schemes
● Bounded-Buffer Problem
● Readers and Writers Problem
● Dining-Philosophers Problem

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


Bounded-Buffer Problem

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


Bounded-Buffer Problem

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


Bounded-Buffer Problem

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


Bounded-Buffer Problem

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


Readers-Writers Problem

● A data set is shared among a number of concurrent


processes
● Readers – only read the data set; they do not
perform any updates
● Writers – can both read and write
● Problem –
● If multiple readers want to read the same data,
they should be allowed
● Only one single writer can access the shared
data at the same time
● If a writer and reader access the file/data
simultaneously, problem may occur as well

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


Readers-Writers Problem

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


Readers-Writers Problem Variations

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


Dining-Philosophers Problem

● Home Assignment for Students to be discussed in


the next lecture

Operating System Concepts – 9th 5. Silberschatz, Galvin and Gagne


Operating System Concepts – 9th Silberschatz, Galvin and Gagne

You might also like