on
Synchronization Tool(Part 4)
Synchronization Tool(Part 4)
Synchronization Tool(Part 4)
Monitors
- The difficulty of using semaphores:
- The semaphore is convenient and effective for synchronization.
- However, timing errors can happen
- if particular execution sequences take place.
- these sequences do not always occur,
- and it is hard to detect.
- An illustrative example of semaphores's problem
- All processes share a binary semaphore mutex initialized to 1.
- Each process must wait(mutex) before entering the CS
- and signal(mutex) afterward.
- If this sequence is not observed,
- two processes may be in their critical sections simultaneously.
- Situation 1:
- Note that the difiiculty arises
- even if a single process is not well behaved.
- Suppose that a program interchanges the order.
- in which wait() and signal() on the semaphore nutex are executed.
- Situation 2&3:
- Suppose that a program replaces signal() with wait().
- Suppose that a process omits the wait(), or the signal(), or both of them.
- How to deal with these kinds of difficulties?
- These situations may be caused
- by an honest programming error or an uncooperating programmer.
- Various typs of errores can be generated easily
- when programmers se semaphores (or mutex locks) is incorrectly.
- Incorporate simple synchronization tools
- as high-level language constructs
- monitor: one fundamental high-level synchronization construct.
- A monitor type is
- an ADT(abstract data type) that includes a set of programmer-defined operations
- that are provided with mutual exclusion within the monitor.
- declares the variables
- whose values define the state of an instance of that type.
- along with the bodies of function that operate on those variables.
- Conditional Variables:
- The monitor construct is not sufficiently powerful
- for modeling some synchronization schemes.
- We need to define the condition construct
- to provide additional synchronization mechanisms.
- Using conditional variables:
- One can define one or more variables of type condition:
- The only operations that can be invoked
- on a condition variable are wait() and signal().
- Java Monitors
- Java provides a monitor-like
- concurrency mechanism for thread synchronization.
- called as monitor-lock or intrinsic-lock.
- Basic language constructs for Java Synchronization
- synchronized keyword.
- wait() and notify() method.
- synchronized keyword:
- 임계영역에 해당하는 코드 블록을 선언할 때 사용하는 자바 키워드
- 해당 코드 블록(임계영역)에는 모니터락을 획득해야 진입 가능
- 모니터락을 가진 객체 인스턴스를 지정할 수 있음
- 메소드에 선언하면 메소드 코드 블록 전체가 임계영역으로 지정됨
- 이 때, 모니터락을 가진 객체 인스턴스는 this 객체 인스턴스임
- wait() and notify() methods:
- java.lang.Object 클래스에 선언됨: 모든 자바 객체가 가진 메소드임
- 쓰레드가 어떤 객체의 wait() 메소드를 호출하면
- 해당 객체의 모니터락을 획득하기 위해 대기 상태로 진입함.
- 쓰레드가 어떤 객체의 notify() 메소드를 호출하면
- 해당 객체 모니터에 대기중인 쓰레드 하나를 깨움.
- notify() 대신에 notifyAll() 메소드를 호출하면
- 해당 객체 모니터에 대기중인 쓰레드 전부를 깨움.
- Java Synchronization Example 1:
Synchronization을 하지 않아 잘 못된 값이 출력
- Java Synchronization Example 2:
synchronized 키워드를 사용해서 Synchronization을 함.
- Java Synchronization Example 3:
object를 synchronized 하여 Synchronization을 해결함.
- Java Synchronization Example 4:
쓰레드가 각각의 Synchronization을 하여 잘못된 값이 출력.
- Java Synchronization Example 5:
하나의 Counter를 가지고 Synchronization을 하기 때문에 올바른 값이 출력.
Liveness
- Liveness
- Two criteria for the CSP: the progress and bounded-waitnig.
- Semaphores and monitors cannot solve these requirements.
- Liveness refers to
- a set of propertiews that a system must satisfy
- to ensure that proesses make progress during their execution cycle.
- Two situations that can lead to liveness failures.
- deadlock and priority inversion.
- Deadlock
- a situation where two or more processes are waiting indefinitely
- for an event that can be caused only by one of the waiting process.
- Priority Inversion
- A situation where a higher-priority processes have to wait
- for a lower-priority one to finish the resource.
- It can arise when a higher-priority process
- needs to read or modify kernel data
- that are currently being accessed by a lower-priority process.
- Typically, priority inversion is avoided
- by implementing a priority-inheritance protocl.
- All processes accessing resources needed by a higher-priority process
- inherit th higher priority
- until they releases that resources.
# 출처 : Operating System Concepts, 10/E , Avraham Silberschatz
# 인프런 운영체제 공룡책 강의 , 주니온
from http://dabonee.tistory.com/45 by ccl(A) rewrite - 2021-09-29 21:27:13