SlideShare a Scribd company logo
CRITICAL SECTION
PROBLEM
CS Problem
●   When one process is executing in its critical
    section, then no process is allowed to execute in its
    critical section, thus the execution of CS by the
    processes is mutually exclusive in time
CS Problem
do
{
entry section
     Critical Section
Exit section
     Remainder section
}while(1);
CS problem
●   A solution to CS problem must satisfy
    ●   Mutual exclusion: if process Pi is executing in its CS, then
        no other processes can be executing in their CS
    ●   Progress: if no process is executing in its CS and some
        processes want to enter thier CS, then only those processes
        that are not executing in their remainder section can
        participate in the decision on which will enter its CS next,
        and this selection cannot be postponed indefinitely
    ●   Bounded Waiting: There exists a bound on the number of
        times that other processes are allowed to enter their CS after
        a process has made a request to enter its CS and before that
        is granted
Two process solution for CS
●   Assume there are only two processes P0 and P1 at
    a time.
●   For convenience, when presenting Pi, we use Pj to
    denote the other process, that is j=1-i
Algorithm 1
Do{
  While (turn != i);
       Critical section
  Turn =j;
  Remainder section
  }while(1);
  if turn==i, then process Pi is allowed to execute CS
  ●  Mutual exclusion is satisfied, but the progress is not
     satisfied
  ●   If turn==0 and P1 is ready to enter CS, P1 cannot do,
      eventhough P0 may be in its remainder section
Algorithm 2
Do{
  flag[i] = true;
  while(flag[j])'
      Critical section
  flag[i]=false;
      Remainder section;
  }while(1);
Algorithm 2
●   Boolean flag[2];
●   If flag[i] is true, then P1 is allowed to enter its CS.
●   Mutual exclusion is satisfied and progress is not
    satisfied
●   P0 sets flag[0]=true;
●   P1 sets flag[1]=true;
●   Both P0 and P1 are looping forever in their
    respective while statements.
Algorithm 3
●   The processes share two variables
●   Boolean flag[2]
●   Int turn;
●   Initial values of flag[0] and flag[1] is false and turn
    value either 0 or 1 (immaterial)
●   All the three are satisfied
    ●   Progress
    ●   Mutual exclusion
    ●   Bounded waiting
Algorithm 3
Do{
  flag[i]=true;
  turn=j;
  while(flag[j] && turn ==j);
      Critical section
  flag[i]=false;
      Remainder section
}while(1);
Algorithm 3
●   Pi can enter its CS only when either flag[j]=false
    and turn=i
●   And if P0 and P1 change the flag[0] and flag[1]
    simultaneously to true and wanted to execute its
    CS, then turn =0 or 1 can happen only one at a
    time, so progress is satisfied.
Semaphores
●   Two atomic operations
●   P – wait – proberen (Dutch)
●   V – signal – verhogen
●   Pseudocode is
●   wait(S)
    While (S<=0)
    ;//no operation
    S--;
●   }
Semaphores
Signal (S)
  {
  S++;
  }
  ●   Modification to the integer value of the semaphore in
      the wait and singal operations.
  ●   That is when one process changes the semaphore value,
      no other process can simultaneously modify that same
      semaphore value.
Semaphore implementation
typedef struct {
Int value;
Struct process *L;
}semaphore;
each semaphore has an integer value and list of
  processes. When a process must wait on a
  semaphore, it is added to the list of processes.
●   A signal operation removes one process from the
    list of waiting processes and awakens that process
Semaphore implementation
Void wait(semaphore S){
    S.value--;
    if(S.value <0){
    Add this process to S.L;
    Block();
    }
}
Semaphore implementation
Void signal(Semaphore S){
    S.value++;
    if(S.value <=0){
    Remove a process P from S.L;
    wakeup(P);
    }
}
Deadlock Situations
• Mutual exclusion. At least one resource must be held in a
  non sharable mode; that is, only one process at a time can
  use the resource. If another process requests that
  resource, the requesting process must be delayed until the
  resource has been released.
• Hold and wait. A process must be holding at least one
  resource and waiting to acquire additional resources that
  are currently being held by other processes.
Deadlock
• No preemption. Resources cannot be preempted; that is,
  a resource can be released only voluntarily by the process
  holding it, after that process has completed its task.
• Circular wait. A set { P0 , Pl, ... , P11 } of waiting processes
  must exist such that Po is waiting for a resource held by
  P1, P1 is waiting for a resource held by P2, ... , Pn-1 is
  waiting for a resource held by P,v and P11 is waiting for a
  resource held by Po.
Resource Allocation Graph
• The sets P, K and E:
• P == {P1, P2, P3}
• R== {R1, R2, R3, ~}
• E == {Pl -> Rl P2-> R3, Rl->P2, R2->P2, R2-> Pl, R3-> P3}
• Resource instances:
• One instance of resource type R1
• Two instances of resource type R2
Resource Allocation Graph
Resource Allocation Graph
• One instance of resource type R3
• Three instances of Resource R4
• Process states:
  • Process P1 is holding an instance of resource type R2 and is
    waiting for an instance of resource type R1 .
  • Process P2 is holding an instance of R1 and an instance of R2 and
    is waiting for an instance of R3.
  • Process P3 is holding an instance of R3nces of resource type R4
Resource Allocation Graph
• One instance of resource type R3
• Three instances of Resource R4
• Process states:
  • Process P1 is holding an instance of resource type R2 and is
    waiting for an instance of resource type R1 .
  • Process P2 is holding an instance of R1 and an instance of R2 and
    is waiting for an instance of R3.
  • Process P3 is holding an instance of R3nces of resource type R4
Resource Allocation Graph
Resource Allocation Graph
• P1 ->R1-> P3-> R2-> P1 (P4 may release R2 so that the
 cycle breaks)
Handling Deadlocks
• We can use a protocol to prevent or avoid deadlocks,
  ensuring that the system will never enter a deadlocked
  state.
• We can allow the system to enter a deadlocked state,
  detect it, and recover.
• We can ignore the problem altogether and pretend that
  deadlocks never occur in the system. (used in windows
  and Unix OS)
Deadlock prevention
• Mutual Exclusion
  • Handles non-sharable resource
  • Read only files are the good examples, so any process can use
    that resource, since it is sharable
  • But printers are non sharable, they cannot be denied since printers
    are intrinsically non sharable
Deadlock prevention
• Hold and Wait
  • Whenever a process request any resource, it should not hold any
    other resource
  • Before a process begins its execution, all resource allocated to it.
  • But when the process needs any new resource during its
    execution, it should release the other resources.
  • Starvation is possible in this case, as some process will wait
    indefinitely to use a resource.
  • Also resource utilization is low, that is the resources are unused for
    a long period.
  • Example: if a process wanted to copy some files from a DVD to
    disk and then sort the files and printing it. A Process may request
    DVD and disk, once the copying is done, it may request the Printer
    rather than locking everything in the beginning.
Deadlock prevention
• No Preemption
  • If a process requests some resources, we first check whether they
    are available and we allocate them.
  • If they are not, we check whether they are allocated to some other
    process that is waiting for additional resources. If so, we preempt
    the desired resources from the waiting process and allocate them
    to the requesting process. If the resources are neither available nor
    held by a waiting process, the requesting process must wait. While
    it is waiting, some of its resources may be preempted, but only if
    another process requests them.
  • This protocol is often applied to resources whose state can be
    easily saved and restored later, such as CPU registers and memory
    space. It cannot generally be applied to such resources as printers
    and tape drives.
Deadlock prevention
• Circular Wait
  • To avoid deadlock with this, a number can be allotted to each
    resource
  • Any process can request the resource in terms of increasing order
  • For example F(tape)=1, F(disk)=5, F(printer) =12, a process which
    need tape and printer can request only tape first and then the
    printer. Till that time some other process can use the printer
  • While releasing the resource also, the processes will release in the
    descending order of Numbers.
Dining Philosophers Algorithm
Dining Philosophers Algorithm
Semaphore chopstick[5]={1};
Int I;
Room=4;
Void philosopher(int i)
{
While(TRUE){
Think();
Wait(room);
Wait(chopstick[i]);
Wait (chopstick[i+1]%5);
Eat();
signal(chopstick[i+1]%5);
signal(chopstick[i]);
Signal(room);
}
}
Deadlock Avoidance
• Requires knowledge of future process resource request
• Two approaches
   • do not start a process if its demands might lead to deadlock
   • Do not grant any incremental resource request to a process if this
     allocation might lead to deadlock
Deadlock Avoidance
Resource R = (R1,R2,……Rn)        Total amount of each resource in
                                 the system
Available V = (V1,V2,V3,…..Vn)   Total amount of each resource not
                                 allocated to any process
Claim C=                         Cij = requirement of process i for
c11 c12 ...... c1m               resource j
c21 c22 ...... c2m
....
Cn1 cn2........cnm
Allocation A =                   Aij = current allocation to process i of
a11 a12 ...... a1m               resource j
a21 a22 ...... a2m
....
an1 an2........anm
Banker’s algorithm
• Safe State
  • Is one in which there is at least one sequence of resource
    allocations to processes that does not result in deadlock (All
    process run to completion)
• Unsafe state
  • A state that is not safe
Banker’s algorithm

More Related Content

What's hot

Semaphores OS Basics
Semaphores OS BasicsSemaphores OS Basics
Semaphores OS Basics
Shijin Raj P
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process SynchronizationSonali Chauhan
 
Operating system critical section
Operating system   critical sectionOperating system   critical section
Operating system critical section
Harshana Madusanka Jayamaha
 
Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationWayne Jones Jnr
 
Deadlock
DeadlockDeadlock
Deadlock
Rajandeep Gill
 
Lec11 semaphores
Lec11 semaphoresLec11 semaphores
Lec11 semaphoresanandammca
 
Semaphores and Monitors
 Semaphores and Monitors Semaphores and Monitors
Semaphores and Monitors
sathish sak
 
Process synchronization(deepa)
Process synchronization(deepa)Process synchronization(deepa)
Process synchronization(deepa)Nagarajan
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronization
Syed Hassan Ali
 
Mutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmMutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmSouvik Roy
 
Critical section problem in operating system.
Critical section problem in operating system.Critical section problem in operating system.
Critical section problem in operating system.
MOHIT DADU
 
Operating Systems - Process Synchronization and Deadlocks
Operating Systems - Process Synchronization and DeadlocksOperating Systems - Process Synchronization and Deadlocks
Operating Systems - Process Synchronization and Deadlocks
Mukesh Chinta
 
Semophores and it's types
Semophores and it's typesSemophores and it's types
Semophores and it's types
Nishant Joshi
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronizationvinay arora
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronization
Ali Ahmad
 
Synchronization
SynchronizationSynchronization
SynchronizationMohd Arif
 

What's hot (20)

Semaphores OS Basics
Semaphores OS BasicsSemaphores OS Basics
Semaphores OS Basics
 
OS_Ch8
OS_Ch8OS_Ch8
OS_Ch8
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronization
 
Monitors
MonitorsMonitors
Monitors
 
Operating system critical section
Operating system   critical sectionOperating system   critical section
Operating system critical section
 
Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process Synchronization
 
Deadlock
DeadlockDeadlock
Deadlock
 
Lec11 semaphores
Lec11 semaphoresLec11 semaphores
Lec11 semaphores
 
Semaphores and Monitors
 Semaphores and Monitors Semaphores and Monitors
Semaphores and Monitors
 
Process synchronization(deepa)
Process synchronization(deepa)Process synchronization(deepa)
Process synchronization(deepa)
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronization
 
OS_Ch7
OS_Ch7OS_Ch7
OS_Ch7
 
Mutual exclusion and sync
Mutual exclusion and syncMutual exclusion and sync
Mutual exclusion and sync
 
Mutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmMutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's Algorithm
 
Critical section problem in operating system.
Critical section problem in operating system.Critical section problem in operating system.
Critical section problem in operating system.
 
Operating Systems - Process Synchronization and Deadlocks
Operating Systems - Process Synchronization and DeadlocksOperating Systems - Process Synchronization and Deadlocks
Operating Systems - Process Synchronization and Deadlocks
 
Semophores and it's types
Semophores and it's typesSemophores and it's types
Semophores and it's types
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronization
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronization
 
Synchronization
SynchronizationSynchronization
Synchronization
 

Viewers also liked

Lecture 1 introduction to operating systems
Lecture 1  introduction to operating systemsLecture 1  introduction to operating systems
Lecture 1 introduction to operating systemsPradeep Kumar TS
 
Lecture 3,4 operating systems
Lecture 3,4   operating systemsLecture 3,4   operating systems
Lecture 3,4 operating systemsPradeep Kumar TS
 
Inter process communication
Inter process communicationInter process communication
Inter process communicationPradeep Kumar TS
 
Software Defined Networking - 1
Software Defined Networking - 1Software Defined Networking - 1
Software Defined Networking - 1
Pradeep Kumar TS
 
Challenges in Embedded Computing
Challenges in Embedded ComputingChallenges in Embedded Computing
Challenges in Embedded Computing
Pradeep Kumar TS
 
Virtual classrooms and labs using Big Blue Button
Virtual classrooms and labs using Big Blue ButtonVirtual classrooms and labs using Big Blue Button
Virtual classrooms and labs using Big Blue Button
Pradeep Kumar TS
 
Introduction to TCP
Introduction to TCPIntroduction to TCP
Introduction to TCP
Pradeep Kumar TS
 

Viewers also liked (9)

Lecture 1 introduction to operating systems
Lecture 1  introduction to operating systemsLecture 1  introduction to operating systems
Lecture 1 introduction to operating systems
 
Lecture 7 cpu scheduling
Lecture 7   cpu schedulingLecture 7   cpu scheduling
Lecture 7 cpu scheduling
 
Lecture 3,4 operating systems
Lecture 3,4   operating systemsLecture 3,4   operating systems
Lecture 3,4 operating systems
 
Inter process communication
Inter process communicationInter process communication
Inter process communication
 
Software Defined Networking - 1
Software Defined Networking - 1Software Defined Networking - 1
Software Defined Networking - 1
 
Lecture 5 process concept
Lecture 5   process conceptLecture 5   process concept
Lecture 5 process concept
 
Challenges in Embedded Computing
Challenges in Embedded ComputingChallenges in Embedded Computing
Challenges in Embedded Computing
 
Virtual classrooms and labs using Big Blue Button
Virtual classrooms and labs using Big Blue ButtonVirtual classrooms and labs using Big Blue Button
Virtual classrooms and labs using Big Blue Button
 
Introduction to TCP
Introduction to TCPIntroduction to TCP
Introduction to TCP
 

Similar to Cs problem [repaired]

OS - Unit 3 Deadlock (Bankers Algorithm).pptx
OS - Unit 3 Deadlock (Bankers Algorithm).pptxOS - Unit 3 Deadlock (Bankers Algorithm).pptx
OS - Unit 3 Deadlock (Bankers Algorithm).pptx
GovindJha93
 
OS Module-3 (2).pptx
OS Module-3 (2).pptxOS Module-3 (2).pptx
OS Module-3 (2).pptx
KokilaK25
 
Deadlock Detection.pptx
Deadlock Detection.pptxDeadlock Detection.pptx
Deadlock Detection.pptx
infomerlin
 
Module 3 Deadlocks.pptx
Module 3 Deadlocks.pptxModule 3 Deadlocks.pptx
Module 3 Deadlocks.pptx
shreesha16
 
CPU SCHEDULING AND DEADLOCK
CPU SCHEDULING AND	DEADLOCKCPU SCHEDULING AND	DEADLOCK
CPU SCHEDULING AND DEADLOCK
Vicky Kumar
 
Chapter5_Deadlock.pptx
Chapter5_Deadlock.pptxChapter5_Deadlock.pptx
Chapter5_Deadlock.pptx
DenisPriscus
 
Deadlock
DeadlockDeadlock
Deadlock
Mayuri Verma
 
Dead lock
Dead lockDead lock
Dead lock
M_Javed Ashraf
 
Deadlock in Operating System
Deadlock in Operating SystemDeadlock in Operating System
Deadlock in Operating System
AUST
 
6. Deadlock_1640227623705.pptx
6. Deadlock_1640227623705.pptx6. Deadlock_1640227623705.pptx
6. Deadlock_1640227623705.pptx
shrijanasharma3
 
Deadlocks Part- III.pdf
Deadlocks Part- III.pdfDeadlocks Part- III.pdf
Deadlocks Part- III.pdf
Harika Pudugosula
 
Fcfs and sjf
Fcfs and sjfFcfs and sjf
Fcfs and sjf
A. S. M. Shafi
 
7308346-Deadlock.pptx
7308346-Deadlock.pptx7308346-Deadlock.pptx
7308346-Deadlock.pptx
sheraz7288
 
Ch7 deadlocks
Ch7   deadlocksCh7   deadlocks
Ch7 deadlocks
Welly Dian Astika
 
Process Synchronization -1.ppt
Process Synchronization -1.pptProcess Synchronization -1.ppt
Process Synchronization -1.ppt
jayverma27
 
OS Process Chapter 3.pdf
OS Process Chapter 3.pdfOS Process Chapter 3.pdf
OS Process Chapter 3.pdf
Kp Sharma
 
deadlocks.pptx
deadlocks.pptxdeadlocks.pptx
deadlocks.pptx
JOHNZHOU52
 
programming .pptx
programming .pptxprogramming .pptx
programming .pptx
SHUJEHASSAN
 

Similar to Cs problem [repaired] (20)

OS - Unit 3 Deadlock (Bankers Algorithm).pptx
OS - Unit 3 Deadlock (Bankers Algorithm).pptxOS - Unit 3 Deadlock (Bankers Algorithm).pptx
OS - Unit 3 Deadlock (Bankers Algorithm).pptx
 
OS Module-3 (2).pptx
OS Module-3 (2).pptxOS Module-3 (2).pptx
OS Module-3 (2).pptx
 
Deadlock Detection.pptx
Deadlock Detection.pptxDeadlock Detection.pptx
Deadlock Detection.pptx
 
Module 3 Deadlocks.pptx
Module 3 Deadlocks.pptxModule 3 Deadlocks.pptx
Module 3 Deadlocks.pptx
 
CPU SCHEDULING AND DEADLOCK
CPU SCHEDULING AND	DEADLOCKCPU SCHEDULING AND	DEADLOCK
CPU SCHEDULING AND DEADLOCK
 
Chapter5_Deadlock.pptx
Chapter5_Deadlock.pptxChapter5_Deadlock.pptx
Chapter5_Deadlock.pptx
 
Deadlock
DeadlockDeadlock
Deadlock
 
Dead lock
Dead lockDead lock
Dead lock
 
Deadlock in Operating System
Deadlock in Operating SystemDeadlock in Operating System
Deadlock in Operating System
 
6. Deadlock_1640227623705.pptx
6. Deadlock_1640227623705.pptx6. Deadlock_1640227623705.pptx
6. Deadlock_1640227623705.pptx
 
Deadlocks Part- III.pdf
Deadlocks Part- III.pdfDeadlocks Part- III.pdf
Deadlocks Part- III.pdf
 
Fcfs and sjf
Fcfs and sjfFcfs and sjf
Fcfs and sjf
 
Ipc feb4
Ipc feb4Ipc feb4
Ipc feb4
 
7308346-Deadlock.pptx
7308346-Deadlock.pptx7308346-Deadlock.pptx
7308346-Deadlock.pptx
 
Ch7 deadlocks
Ch7   deadlocksCh7   deadlocks
Ch7 deadlocks
 
Os5
Os5Os5
Os5
 
Process Synchronization -1.ppt
Process Synchronization -1.pptProcess Synchronization -1.ppt
Process Synchronization -1.ppt
 
OS Process Chapter 3.pdf
OS Process Chapter 3.pdfOS Process Chapter 3.pdf
OS Process Chapter 3.pdf
 
deadlocks.pptx
deadlocks.pptxdeadlocks.pptx
deadlocks.pptx
 
programming .pptx
programming .pptxprogramming .pptx
programming .pptx
 

More from Pradeep Kumar TS

Digital Portfolio and Footprint
Digital Portfolio and FootprintDigital Portfolio and Footprint
Digital Portfolio and Footprint
Pradeep Kumar TS
 
Open book Examination
Open book ExaminationOpen book Examination
Open book Examination
Pradeep Kumar TS
 
Software Define Networking (SDN)
Software Define Networking (SDN)Software Define Networking (SDN)
Software Define Networking (SDN)
Pradeep Kumar TS
 
What next - Career Enhancement of Graduates
What next - Career Enhancement of GraduatesWhat next - Career Enhancement of Graduates
What next - Career Enhancement of Graduates
Pradeep Kumar TS
 
Protothreads
ProtothreadsProtothreads
Protothreads
Pradeep Kumar TS
 
6LoWPAN
6LoWPAN 6LoWPAN
Software Defined Networks
Software Defined NetworksSoftware Defined Networks
Software Defined Networks
Pradeep Kumar TS
 
Higher Order Thinking - Question paper setting
Higher Order Thinking - Question paper settingHigher Order Thinking - Question paper setting
Higher Order Thinking - Question paper setting
Pradeep Kumar TS
 
IoT Communication Protocols
IoT Communication ProtocolsIoT Communication Protocols
IoT Communication Protocols
Pradeep Kumar TS
 
IoT Applications
IoT ApplicationsIoT Applications
IoT Applications
Pradeep Kumar TS
 
RPL - Routing Protocol for Low Power and Lossy Networks
RPL - Routing Protocol for Low Power and Lossy NetworksRPL - Routing Protocol for Low Power and Lossy Networks
RPL - Routing Protocol for Low Power and Lossy Networks
Pradeep Kumar TS
 
Mannasim for NS2
Mannasim for NS2Mannasim for NS2
Mannasim for NS2
Pradeep Kumar TS
 
Recompiling network simulator 2
Recompiling network simulator 2Recompiling network simulator 2
Recompiling network simulator 2
Pradeep Kumar TS
 
OTcl and C++ linkages in NS2
OTcl and C++ linkages in NS2OTcl and C++ linkages in NS2
OTcl and C++ linkages in NS2
Pradeep Kumar TS
 
Wired and Wireless Examples in ns2
Wired and Wireless Examples in ns2Wired and Wireless Examples in ns2
Wired and Wireless Examples in ns2
Pradeep Kumar TS
 
Installation of ns2
Installation of ns2Installation of ns2
Installation of ns2
Pradeep Kumar TS
 
Introduction to ns2
Introduction to ns2Introduction to ns2
Introduction to ns2
Pradeep Kumar TS
 
Software Defined Networking - 2
Software Defined Networking - 2Software Defined Networking - 2
Software Defined Networking - 2
Pradeep Kumar TS
 
Software Defined Networking - 3
Software Defined Networking - 3Software Defined Networking - 3
Software Defined Networking - 3
Pradeep Kumar TS
 
Tracing and awk in ns2
Tracing and awk in ns2Tracing and awk in ns2
Tracing and awk in ns2
Pradeep Kumar TS
 

More from Pradeep Kumar TS (20)

Digital Portfolio and Footprint
Digital Portfolio and FootprintDigital Portfolio and Footprint
Digital Portfolio and Footprint
 
Open book Examination
Open book ExaminationOpen book Examination
Open book Examination
 
Software Define Networking (SDN)
Software Define Networking (SDN)Software Define Networking (SDN)
Software Define Networking (SDN)
 
What next - Career Enhancement of Graduates
What next - Career Enhancement of GraduatesWhat next - Career Enhancement of Graduates
What next - Career Enhancement of Graduates
 
Protothreads
ProtothreadsProtothreads
Protothreads
 
6LoWPAN
6LoWPAN 6LoWPAN
6LoWPAN
 
Software Defined Networks
Software Defined NetworksSoftware Defined Networks
Software Defined Networks
 
Higher Order Thinking - Question paper setting
Higher Order Thinking - Question paper settingHigher Order Thinking - Question paper setting
Higher Order Thinking - Question paper setting
 
IoT Communication Protocols
IoT Communication ProtocolsIoT Communication Protocols
IoT Communication Protocols
 
IoT Applications
IoT ApplicationsIoT Applications
IoT Applications
 
RPL - Routing Protocol for Low Power and Lossy Networks
RPL - Routing Protocol for Low Power and Lossy NetworksRPL - Routing Protocol for Low Power and Lossy Networks
RPL - Routing Protocol for Low Power and Lossy Networks
 
Mannasim for NS2
Mannasim for NS2Mannasim for NS2
Mannasim for NS2
 
Recompiling network simulator 2
Recompiling network simulator 2Recompiling network simulator 2
Recompiling network simulator 2
 
OTcl and C++ linkages in NS2
OTcl and C++ linkages in NS2OTcl and C++ linkages in NS2
OTcl and C++ linkages in NS2
 
Wired and Wireless Examples in ns2
Wired and Wireless Examples in ns2Wired and Wireless Examples in ns2
Wired and Wireless Examples in ns2
 
Installation of ns2
Installation of ns2Installation of ns2
Installation of ns2
 
Introduction to ns2
Introduction to ns2Introduction to ns2
Introduction to ns2
 
Software Defined Networking - 2
Software Defined Networking - 2Software Defined Networking - 2
Software Defined Networking - 2
 
Software Defined Networking - 3
Software Defined Networking - 3Software Defined Networking - 3
Software Defined Networking - 3
 
Tracing and awk in ns2
Tracing and awk in ns2Tracing and awk in ns2
Tracing and awk in ns2
 

Recently uploaded

The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 

Recently uploaded (20)

The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 

Cs problem [repaired]

  • 2. CS Problem ● When one process is executing in its critical section, then no process is allowed to execute in its critical section, thus the execution of CS by the processes is mutually exclusive in time
  • 3. CS Problem do { entry section Critical Section Exit section Remainder section }while(1);
  • 4. CS problem ● A solution to CS problem must satisfy ● Mutual exclusion: if process Pi is executing in its CS, then no other processes can be executing in their CS ● Progress: if no process is executing in its CS and some processes want to enter thier CS, then only those processes that are not executing in their remainder section can participate in the decision on which will enter its CS next, and this selection cannot be postponed indefinitely ● Bounded Waiting: There exists a bound on the number of times that other processes are allowed to enter their CS after a process has made a request to enter its CS and before that is granted
  • 5. Two process solution for CS ● Assume there are only two processes P0 and P1 at a time. ● For convenience, when presenting Pi, we use Pj to denote the other process, that is j=1-i
  • 6. Algorithm 1 Do{ While (turn != i); Critical section Turn =j; Remainder section }while(1); if turn==i, then process Pi is allowed to execute CS ● Mutual exclusion is satisfied, but the progress is not satisfied ● If turn==0 and P1 is ready to enter CS, P1 cannot do, eventhough P0 may be in its remainder section
  • 7. Algorithm 2 Do{ flag[i] = true; while(flag[j])' Critical section flag[i]=false; Remainder section; }while(1);
  • 8. Algorithm 2 ● Boolean flag[2]; ● If flag[i] is true, then P1 is allowed to enter its CS. ● Mutual exclusion is satisfied and progress is not satisfied ● P0 sets flag[0]=true; ● P1 sets flag[1]=true; ● Both P0 and P1 are looping forever in their respective while statements.
  • 9. Algorithm 3 ● The processes share two variables ● Boolean flag[2] ● Int turn; ● Initial values of flag[0] and flag[1] is false and turn value either 0 or 1 (immaterial) ● All the three are satisfied ● Progress ● Mutual exclusion ● Bounded waiting
  • 10. Algorithm 3 Do{ flag[i]=true; turn=j; while(flag[j] && turn ==j); Critical section flag[i]=false; Remainder section }while(1);
  • 11. Algorithm 3 ● Pi can enter its CS only when either flag[j]=false and turn=i ● And if P0 and P1 change the flag[0] and flag[1] simultaneously to true and wanted to execute its CS, then turn =0 or 1 can happen only one at a time, so progress is satisfied.
  • 12. Semaphores ● Two atomic operations ● P – wait – proberen (Dutch) ● V – signal – verhogen ● Pseudocode is ● wait(S) While (S<=0) ;//no operation S--; ● }
  • 13. Semaphores Signal (S) { S++; } ● Modification to the integer value of the semaphore in the wait and singal operations. ● That is when one process changes the semaphore value, no other process can simultaneously modify that same semaphore value.
  • 14. Semaphore implementation typedef struct { Int value; Struct process *L; }semaphore; each semaphore has an integer value and list of processes. When a process must wait on a semaphore, it is added to the list of processes. ● A signal operation removes one process from the list of waiting processes and awakens that process
  • 15. Semaphore implementation Void wait(semaphore S){ S.value--; if(S.value <0){ Add this process to S.L; Block(); } }
  • 16. Semaphore implementation Void signal(Semaphore S){ S.value++; if(S.value <=0){ Remove a process P from S.L; wakeup(P); } }
  • 17. Deadlock Situations • Mutual exclusion. At least one resource must be held in a non sharable mode; that is, only one process at a time can use the resource. If another process requests that resource, the requesting process must be delayed until the resource has been released. • Hold and wait. A process must be holding at least one resource and waiting to acquire additional resources that are currently being held by other processes.
  • 18. Deadlock • No preemption. Resources cannot be preempted; that is, a resource can be released only voluntarily by the process holding it, after that process has completed its task. • Circular wait. A set { P0 , Pl, ... , P11 } of waiting processes must exist such that Po is waiting for a resource held by P1, P1 is waiting for a resource held by P2, ... , Pn-1 is waiting for a resource held by P,v and P11 is waiting for a resource held by Po.
  • 19. Resource Allocation Graph • The sets P, K and E: • P == {P1, P2, P3} • R== {R1, R2, R3, ~} • E == {Pl -> Rl P2-> R3, Rl->P2, R2->P2, R2-> Pl, R3-> P3} • Resource instances: • One instance of resource type R1 • Two instances of resource type R2
  • 21. Resource Allocation Graph • One instance of resource type R3 • Three instances of Resource R4 • Process states: • Process P1 is holding an instance of resource type R2 and is waiting for an instance of resource type R1 . • Process P2 is holding an instance of R1 and an instance of R2 and is waiting for an instance of R3. • Process P3 is holding an instance of R3nces of resource type R4
  • 22. Resource Allocation Graph • One instance of resource type R3 • Three instances of Resource R4 • Process states: • Process P1 is holding an instance of resource type R2 and is waiting for an instance of resource type R1 . • Process P2 is holding an instance of R1 and an instance of R2 and is waiting for an instance of R3. • Process P3 is holding an instance of R3nces of resource type R4
  • 24. Resource Allocation Graph • P1 ->R1-> P3-> R2-> P1 (P4 may release R2 so that the cycle breaks)
  • 25. Handling Deadlocks • We can use a protocol to prevent or avoid deadlocks, ensuring that the system will never enter a deadlocked state. • We can allow the system to enter a deadlocked state, detect it, and recover. • We can ignore the problem altogether and pretend that deadlocks never occur in the system. (used in windows and Unix OS)
  • 26. Deadlock prevention • Mutual Exclusion • Handles non-sharable resource • Read only files are the good examples, so any process can use that resource, since it is sharable • But printers are non sharable, they cannot be denied since printers are intrinsically non sharable
  • 27. Deadlock prevention • Hold and Wait • Whenever a process request any resource, it should not hold any other resource • Before a process begins its execution, all resource allocated to it. • But when the process needs any new resource during its execution, it should release the other resources. • Starvation is possible in this case, as some process will wait indefinitely to use a resource. • Also resource utilization is low, that is the resources are unused for a long period. • Example: if a process wanted to copy some files from a DVD to disk and then sort the files and printing it. A Process may request DVD and disk, once the copying is done, it may request the Printer rather than locking everything in the beginning.
  • 28. Deadlock prevention • No Preemption • If a process requests some resources, we first check whether they are available and we allocate them. • If they are not, we check whether they are allocated to some other process that is waiting for additional resources. If so, we preempt the desired resources from the waiting process and allocate them to the requesting process. If the resources are neither available nor held by a waiting process, the requesting process must wait. While it is waiting, some of its resources may be preempted, but only if another process requests them. • This protocol is often applied to resources whose state can be easily saved and restored later, such as CPU registers and memory space. It cannot generally be applied to such resources as printers and tape drives.
  • 29. Deadlock prevention • Circular Wait • To avoid deadlock with this, a number can be allotted to each resource • Any process can request the resource in terms of increasing order • For example F(tape)=1, F(disk)=5, F(printer) =12, a process which need tape and printer can request only tape first and then the printer. Till that time some other process can use the printer • While releasing the resource also, the processes will release in the descending order of Numbers.
  • 31. Dining Philosophers Algorithm Semaphore chopstick[5]={1}; Int I; Room=4; Void philosopher(int i) { While(TRUE){ Think(); Wait(room); Wait(chopstick[i]); Wait (chopstick[i+1]%5); Eat(); signal(chopstick[i+1]%5); signal(chopstick[i]); Signal(room); } }
  • 32. Deadlock Avoidance • Requires knowledge of future process resource request • Two approaches • do not start a process if its demands might lead to deadlock • Do not grant any incremental resource request to a process if this allocation might lead to deadlock
  • 33. Deadlock Avoidance Resource R = (R1,R2,……Rn) Total amount of each resource in the system Available V = (V1,V2,V3,…..Vn) Total amount of each resource not allocated to any process Claim C= Cij = requirement of process i for c11 c12 ...... c1m resource j c21 c22 ...... c2m .... Cn1 cn2........cnm Allocation A = Aij = current allocation to process i of a11 a12 ...... a1m resource j a21 a22 ...... a2m .... an1 an2........anm
  • 34. Banker’s algorithm • Safe State • Is one in which there is at least one sequence of resource allocations to processes that does not result in deadlock (All process run to completion) • Unsafe state • A state that is not safe