Operating System: process synchronization, mutual exclusion

  • Critical resource: a period of time only allows a process to use the resources.
  • Process Synchronization
    • Concurrency brought asynchronous, and sometimes need to be synchronized to solve this problem by asynchronous processes.
    • Need to cooperate with each other to complete the work between some processes, the work of advancing the process needs to follow a certain order.
  • Mutual exclusion
    • Access to critical resources, need be mutually exclusive. That is the same time period only allows a process to access the resource.
    • Four parts
      • Entry zone: Check to enter the critical section, if they can enter, you need to be locked
      • Critical area: access critical resources that code
      • Exit zone: responsible for unlocking
      • The remaining area: remainder code portion
    • Following the rule
      • Idle let into: critical resource is idle, a process should be allowed access
      • Busy waiting: the critical zone is being accessed, the other trying to access process needs to wait
      • Finite Waiting: To enter the critical zone for a limited time, guaranteed not to hunger
      • Let the right to wait: do not come into the critical area of the process, to release the processor, and so prevent busy
  • Mutual exclusion of software implementation
    • Single notation:
      • Both processes will use authority in critical areas After accessing the critical section transferred to another process. That permission to enter the critical region of each process can only be given by another process.
      • Does not meet the "idle let into"
    • Double check mark law:
      • Each process before entering the critical section check currently there is no other process wants to enter the critical region, if not, put their corresponding flag is set to true, then begin to access the critical section.
      • Does not meet the "busy waiting"
    • After double-checking mark law:
      • Algorithm is a problem before the first "check", "locked", but these two operations at one go and can not, therefore leading to two processes at the same time to enter the critical problem areas. Therefore, later changed to "lock", "check" method.
      • Does not meet the "let into idle" and "limited wait"
    • Peterson:
      • In the entry area "initiative to fight - Active humility - to check whether the other party want to go, whether one's own humility."
      • Not satisfied "Let's wait for the right"
  • Mutual exclusion of hardware implementation
    • Interrupt mask method:
      • Disable interrupts before entering the critical section, which does not allow the current process is interrupted, the switching process will certainly not happen.
      • Open break after exiting the critical section, complete access to the current process critical section, and then open the interrupt instruction execution, be possible to have access to the critical area on the processor and other processes.
      • Cons: Once the interrupt is disabled, the process can not be stopped. Does not apply to a multi-CPU, the process does not apply to the user (since the switch can interrupt instruction running in kernel mode).
    • TestandSet:
      • Atomic instructions --TestandSet:. 1 reads the value from the memory 2. Test whether the value 1 and returns true or false value is set to 3. Memory 1
      • Let not satisfied to wait for the right
    • Swap:

      • Principle operation instruction --Swap: exchange of two values ​​in memory
      • Let not satisfied to wait for the right
  • Semaphore mechanism
    image
    • Semaphore (abstract data types): is a variable, the number can be used to represent a resource in the system.
      • An integer (sem), two atomic operations
      • P operation (the wait () primitive):
        • sem minus 1, if sem <0, wait, otherwise continue
        • Must be the first sem -, you may need to be performed after block primitive
      • V operation (signal () primitive):
        • sem plus 1, if sem <= 0, the wait for a wake-up P
        • Must be the first sem ++, you may need to perform after wakeup primitives
          image
  • Semaphore mechanism to achieve mutual exclusion and synchronization
    image
  • The tube
    • Objective: To write a program to solve the existing semaphore mechanism is difficult, error-prone problem.
    • The composition of the tube
      • Shared data structure
      • Statement of data structure initialization
      • A set of process (function) to access the data structure
    • The basic characteristics of the tube:
      • Specific "entry" various external processes / threads can only be provided through the tube to access shared data.
      • It can only allow a process to perform an internal process within the tube.
      • Access to the tube characteristics must be mutually exclusive of each process is implemented by the compiler.
      • Condition variable may be disposed in the tube process and wait / wake-up operations in order to solve the synchronization problem.

Guess you like

Origin www.cnblogs.com/xiaobaizzz/p/12285226.html
Recommended