SlideShare a Scribd company logo
Lecture18-19 (1).ppt
 Previously we studied a software based solution to critical
section problem (Peterson solution).
 Many systems provide hardware support for critical section
code.
 Uniprocessors – We could disable interrupts for preamption so
when a shared data is being modified no preamption will occur.
◦ Generally too inefficient on multiprocessor systems.
Disabling interrupts on a multiprocessor is very time
consuming, as the message is passed to all the processors
 Modern machines provide special atomic hardware instructions
 Atomic = non-interruptable
 The important characteristic is that this instruction is executed
atomically.
 If two TestAndSet C) instructions are executed simultaneously
(each on a different CPU), they will be executed sequentially in
some arbitrary order.
boolean TestAndSet (boolean *target)
{
boolean rv = *target;
*target = TRUE;
return rv:
}
 If the machine supports the TestAndSet () instruction, then we
can implement mutual exclusion by declaring a global Boolean
variable lock, initialized to false.
 Solution using TestAndSet
 Shared boolean variable lock is initialized to false.
do {
while ( TestAndSet (&lock ))
; /* do nothing
// critical section
lock = FALSE;
// remainder section
} while ( TRUE);
Another synchronization tool is called semaphore. A semaphore S
is an integer variable that is accessed only through two standard
atomic operations: wait () and signal ().
 Wait is represented by P() and signal by V().
 Binary semaphore – integer value can range only between 0
and 1; can be simpler to implement
◦ Also known as mutex locks
 Counting semaphore – integer value can range over an
unrestricted domain.
◦ Used to control access to a given resource consisting of a finite number of
instances.
◦ Semaphore is initialized to the number of resources available. Each process
performs a waitQ operation on the semaphore (thereby decrementing the
count).
◦ When a process releases a resource, it performs a signal () operation
(incrementing the count).
◦ When the count for the semaphore goes to 0, all resources are being used.
After that, processes that wish to use a resource will block until the count
becomes greater than 0.
 Mutual Exclusion
 We can use binary semaphores to deal with the critical-section
problem for multiple processes. The n processes share a binary
semaphore (mutex lock) initialized to 1.
 Each process P, is organized as shown
◦ Semaphore S; // initialized to 1
◦ wait (S);
Critical Section
signal (S);
Lecture18-19 (1).ppt
 Implementation– Main disadvantage of the semaphore
definition given previously is that every process waiting for
semaphore loops continuously in the entry code.
 Looping wastes CPU cycles that some other process might be
able to use productively. This type of semaphore is also called a
spinlock (process "spins" while waiting for the lock).
 To eliminate busy waiting when a process has to wait for
semaphore it is blocked.
 The block operation places a process into a waiting queue
associated with the semaphore. Hence a waiting process is not
scheduled so no cpu cycles are wasted.
 Implementation– semaphores under this definition is defined
as a "C" struct:
typedef struct {
int value;
struct process *list;
} semaphore;
 Implementation of wait:
wait (S){
S->value--;
if (S->value < 0) {
add this process to waiting
queue
block();
}
}
Implementation of signal:
Signal (S){
S->value++;
if (S->value <= 0) {
remove a process P from the
waiting queue
wakeup(P);
}
}
 The block() operation suspends the process that invokes it.
 The wakeup(P) operation resumes the execution of a blocked
process P. These two operations are provided by the operating
system as basic system calls.
 Note that, although under the classical definition of semaphores
with busy waiting the semaphore value is never negative, this
implementation may have negative semaphore values.
 If the semaphore value is negative, its magnitude is the number
of processes waiting on that semaphore.
 Deadlock – two or more processes are waiting indefinitely for an
event that can be caused by only one of the waiting processes
 Let S and Q be two semaphores initialized to 1
P0 P1
wait (S); wait (Q);
wait (Q); wait (S);
. .
. .
. .
signal (S); signal (Q);
signal (Q); signal (S);
 Starvation – indefinite blocking. A process may never be
removed from the semaphore queue in which it is suspended.
 May occur if we add and remove processes from the list associated
with a semaphore in LIFO (last-in, first-out) order.

More Related Content

Similar to Lecture18-19 (1).ppt (20)

PDF
CH05.pdf
ImranKhan880955
 
PPTX
Process Synchronization in operating system | mutex | semaphore | race condition
Shivam Mitra
 
PPT
Inter process communication
Gift Kaliza
 
PPTX
OS UNIT3.pptx
DHANABALSUBRAMANIAN
 
PPT
14-Semaphores.ppt
KamranAli649587
 
PDF
Process coordination
Sweta Kumari Barnwal
 
PPT
Ipc feb4
yashnand
 
PPTX
Concurrency: Mutual Exclusion and Synchronization
Anas Ebrahim
 
PPTX
9-Operating Systems -Synchronization, interprocess communication, deadlock.pptx
sekkiran
 
PPT
Process Synchronization -1.ppt
jayverma27
 
PDF
Operating Systems - Process Synchronization and Deadlocks
Mukesh Chinta
 
PPT
OS Process Synchronization, semaphore and Monitors
sgpraju
 
PPTX
OS 6.pptx
ZainabShahzad9
 
PPTX
Interprocess Communication important topic in iOS .pptx
thetale999
 
PPTX
Lecture 9 - Process Synchronization.pptx
EhteshamulIslam1
 
PPT
Ipc feb4
Ruchi Sharma
 
PPTX
PPT 123456617829291912192891semaphores.pptx
kritisrivastav497
 
PPTX
Operating system 27 semaphores
Vaibhav Khanna
 
PPTX
Lecture 5 inter process communication
Kumbirai Junior Muzavazi
 
CH05.pdf
ImranKhan880955
 
Process Synchronization in operating system | mutex | semaphore | race condition
Shivam Mitra
 
Inter process communication
Gift Kaliza
 
OS UNIT3.pptx
DHANABALSUBRAMANIAN
 
14-Semaphores.ppt
KamranAli649587
 
Process coordination
Sweta Kumari Barnwal
 
Ipc feb4
yashnand
 
Concurrency: Mutual Exclusion and Synchronization
Anas Ebrahim
 
9-Operating Systems -Synchronization, interprocess communication, deadlock.pptx
sekkiran
 
Process Synchronization -1.ppt
jayverma27
 
Operating Systems - Process Synchronization and Deadlocks
Mukesh Chinta
 
OS Process Synchronization, semaphore and Monitors
sgpraju
 
OS 6.pptx
ZainabShahzad9
 
Interprocess Communication important topic in iOS .pptx
thetale999
 
Lecture 9 - Process Synchronization.pptx
EhteshamulIslam1
 
Ipc feb4
Ruchi Sharma
 
PPT 123456617829291912192891semaphores.pptx
kritisrivastav497
 
Operating system 27 semaphores
Vaibhav Khanna
 
Lecture 5 inter process communication
Kumbirai Junior Muzavazi
 

Recently uploaded (20)

PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PPSX
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
PPTX
Latest Features in Odoo 18 - Odoo slides
Celine George
 
PPTX
LEGAL ASPECTS OF PSYCHIATRUC NURSING.pptx
PoojaSen20
 
PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PPTX
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
PPTX
Views on Education of Indian Thinkers J.Krishnamurthy..pptx
ShrutiMahanta1
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
PPTX
Nutri-QUIZ-Bee-Elementary.pptx...................
ferdinandsanbuenaven
 
PDF
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
PPTX
How to Configure Storno Accounting in Odoo 18 Accounting
Celine George
 
PPTX
Presentation: Climate Citizenship Digital Education
Karl Donert
 
PPTX
How to Create Rental Orders in Odoo 18 Rental
Celine George
 
PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
PDF
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
PPT
digestive system for Pharm d I year HAP
rekhapositivity
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
Latest Features in Odoo 18 - Odoo slides
Celine George
 
LEGAL ASPECTS OF PSYCHIATRUC NURSING.pptx
PoojaSen20
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
Views on Education of Indian Thinkers J.Krishnamurthy..pptx
ShrutiMahanta1
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
Nutri-QUIZ-Bee-Elementary.pptx...................
ferdinandsanbuenaven
 
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
How to Configure Storno Accounting in Odoo 18 Accounting
Celine George
 
Presentation: Climate Citizenship Digital Education
Karl Donert
 
How to Create Rental Orders in Odoo 18 Rental
Celine George
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
digestive system for Pharm d I year HAP
rekhapositivity
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Ad

Lecture18-19 (1).ppt

  • 2.  Previously we studied a software based solution to critical section problem (Peterson solution).  Many systems provide hardware support for critical section code.  Uniprocessors – We could disable interrupts for preamption so when a shared data is being modified no preamption will occur. ◦ Generally too inefficient on multiprocessor systems. Disabling interrupts on a multiprocessor is very time consuming, as the message is passed to all the processors  Modern machines provide special atomic hardware instructions  Atomic = non-interruptable
  • 3.  The important characteristic is that this instruction is executed atomically.  If two TestAndSet C) instructions are executed simultaneously (each on a different CPU), they will be executed sequentially in some arbitrary order. boolean TestAndSet (boolean *target) { boolean rv = *target; *target = TRUE; return rv: }
  • 4.  If the machine supports the TestAndSet () instruction, then we can implement mutual exclusion by declaring a global Boolean variable lock, initialized to false.  Solution using TestAndSet  Shared boolean variable lock is initialized to false. do { while ( TestAndSet (&lock )) ; /* do nothing // critical section lock = FALSE; // remainder section } while ( TRUE);
  • 5. Another synchronization tool is called semaphore. A semaphore S is an integer variable that is accessed only through two standard atomic operations: wait () and signal ().  Wait is represented by P() and signal by V().
  • 6.  Binary semaphore – integer value can range only between 0 and 1; can be simpler to implement ◦ Also known as mutex locks  Counting semaphore – integer value can range over an unrestricted domain. ◦ Used to control access to a given resource consisting of a finite number of instances. ◦ Semaphore is initialized to the number of resources available. Each process performs a waitQ operation on the semaphore (thereby decrementing the count). ◦ When a process releases a resource, it performs a signal () operation (incrementing the count). ◦ When the count for the semaphore goes to 0, all resources are being used. After that, processes that wish to use a resource will block until the count becomes greater than 0.
  • 7.  Mutual Exclusion  We can use binary semaphores to deal with the critical-section problem for multiple processes. The n processes share a binary semaphore (mutex lock) initialized to 1.  Each process P, is organized as shown ◦ Semaphore S; // initialized to 1 ◦ wait (S); Critical Section signal (S);
  • 9.  Implementation– Main disadvantage of the semaphore definition given previously is that every process waiting for semaphore loops continuously in the entry code.  Looping wastes CPU cycles that some other process might be able to use productively. This type of semaphore is also called a spinlock (process "spins" while waiting for the lock).  To eliminate busy waiting when a process has to wait for semaphore it is blocked.  The block operation places a process into a waiting queue associated with the semaphore. Hence a waiting process is not scheduled so no cpu cycles are wasted.
  • 10.  Implementation– semaphores under this definition is defined as a "C" struct: typedef struct { int value; struct process *list; } semaphore;  Implementation of wait: wait (S){ S->value--; if (S->value < 0) { add this process to waiting queue block(); } } Implementation of signal: Signal (S){ S->value++; if (S->value <= 0) { remove a process P from the waiting queue wakeup(P); } }
  • 11.  The block() operation suspends the process that invokes it.  The wakeup(P) operation resumes the execution of a blocked process P. These two operations are provided by the operating system as basic system calls.  Note that, although under the classical definition of semaphores with busy waiting the semaphore value is never negative, this implementation may have negative semaphore values.  If the semaphore value is negative, its magnitude is the number of processes waiting on that semaphore.
  • 12.  Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes  Let S and Q be two semaphores initialized to 1 P0 P1 wait (S); wait (Q); wait (Q); wait (S); . . . . . . signal (S); signal (Q); signal (Q); signal (S);  Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended.  May occur if we add and remove processes from the list associated with a semaphore in LIFO (last-in, first-out) order.