Process Management (X) - Process Synchronization

Process Management (X) - Process Synchronization

Directly restrict VS indirect constraints

Restrict resulting in the reason
Directly restricts Exclusive Competitive public resources
Indirect constraints Synchronize Concurrent processes to share each other's private resources

What is synchronization

Simply put, refers to the existence of a relationship between the timing of system events occur in multiple processes, the need to cooperate with each other together to accomplish a task

A set of concurrent processes in an asynchronous environment, by sending messages to each other directly restricts and perform mutual cooperation , waiting for each process, so that the processes performed in a constant speed is referred to as inter-process synchronization

A set of concurrent processes with synchronous relationship is called the process of cooperation , cooperation in the process of sending signals between each other is called a message or an event.

Synchronization examples

The calculation process and the printing process using the same common buffer Buf.
Repeatedly computing process every time the calculation results into Buf, the printing process and put in the computing process every time the data Buf placed printout by a printer.
If you do not take any measures to restrict execution start time and execution speed of these two processes are independent of each other, the corresponding control segment can be described as:

Every calculation process put the results into the buffer, which is conditional, must buffer is empty, in order to put the results into them, and the printing process continues to the condition that the buffer is not empty, because you buffer is not empty I have things to print

Therefore, the results of the two processes execute each other's conditions, thus limiting direct restriction process execution speed of the processes called concurrent processes between

To achieve synchronization

Each signal transmission

One of the most simple and intuitive way is to directly restricts the process of sending signals to each other's process of execution conditions have been met.
In this way, by eliminating constraints to the implementation of the process of testing conditions, as long as the restriction process received a signal sent to the execution began, and when the signal sent by the restriction process is not received will enter the wait state

  • wait (message name), it represents the cooperation process waits to send a message;
  • signal (message name), sends a message to indicate the process of cooperation.

Message name Bufempty represent Buf empty,
the message name Buffull represent Buf filled with data.
Initialization Bufempty = true, Buffull = false.

Private vs Public Semaphore Semaphore

1. Each message sent between processes semaphore treated as
different from 2. When the mutual exclusion is here only semaphores regardless of concurrent processes and the entire set of constraints related to the process and the process is restricted. Thus, the semaphore is called private (Private Semaphore)
private semaphore 3. Semi is a process of Pi restriction process to send a message from a process Pi is performed the required conditions. The signal corresponding to the amount of private, said mutex semaphore is used for the common semaphore

Synchronized with P, V primitive operations

Implementation steps

1. First, set the semaphore for the private concurrent process
2. private semaphore initial value
3. Finally, P, V and private primitive semaphore predetermined execution sequence of the processes

Examples of realization

Provided processes PA and PB communicate data buffer queue.

PA is sending process, PB is the receiving process. PA transmitting data call transmission process deposit (data), the calling procedure remove (data) when the received data PB.

Data transmission and reception procedure satisfies the following condition

1. In at least one PA transmitting data into a buffer before, PB can not retrieve data from the buffer
2.PA transmit data buffer to the queue, at least one buffer is empty;
3 data blocks sent by PA arranged FIFO (FIFO) manner in the buffer queue.

process

1. private semaphore is set Bufempty the PA process, the amount of signal for the private BUFFULL process of PB
2. Order Bufempty initial value n (n is the number of buffers queue buffer), the initial BUFFULL value of 0
. 3 .description

PA: deposit(data):
    begin local x;
        P(Bufempty);
        按FIFO方式选择一个空缓冲区Buf(x);
        Buf(x)← data;
        Buf(x)置满标记;
        V(Buffull);
    end
PB: remove(data):
    Begin local x;
        P(Buffull);
        按FIFO方式选择一个装满数据的缓冲区Buf(x);
        data ← Buf(x);
        Buf(x)置空标记;
        V(Bufempty);
    end

Producer - consumer issues

Producer vs Consumers

Consumers use a certain type of system resource process is called the resource: Consumers

Manufacturer: releasing process known as producers of similar resources of the resource

Producer - consumer issues

The length n of a bounded buffer (n> 0) with a group of producers process P1, P2, ..., Pm and a group of consumers process C1, C2, ..., Ck linked.
Above producer - consumer problem is a synchronization problem. Since bounded buffer is a critical resource, therefore, must be mutually exclusive processes performed between each producer and each consumer process.

Producers and consumers to solve

With shared semaphore mutex to ensure mutual exclusion between the producer and consumer processes the process, set the semaphore to avail private semaphore producer processes the signal for the full amount of the private semaphore consumer process.

Avail semaphore empty cell sector number expressed in the buffer, the initial value for the n-; full semaphore representing a bounded nonempty buffer unit number, the initial value is 0. Mutex semaphore can be represented with a bounded number of buffer, the initial value is 1.

Each producer process set the course of deposit (data), each consumer use remove (data).

Guess you like

Origin www.cnblogs.com/mengxiaoleng/p/11621391.html