Process Management 2 -- Process Synchronization and semaphores, process management 2 --

Source: Internet
Author: User

Process Management 2 -- Process Synchronization and semaphores, process management 2 --

Address:Http://www.cnblogs.com/archimedes/p/os-process-management2.html, Reprinted, please specify the source address.

Process Synchronization

Process Synchronization: coordinates the execution sequence of multiple related processes;

Synchronization tasks: enables various processes in the system to effectively share resources and cooperate with each other, so that program execution can be realized;

Logical constraints between processes in the system:

Direct Link-synchronization

Indirect link-mutex

The mechanism used for synchronization is called synchronization mechanism. For example:
  • Software and Hardware methods;
  • Semaphore mechanism;
  • Management Mechanism
Basic concepts of Process Synchronization
  • Two forms of constraints
  • Critical resource and Critical Zone
  • Rules to be followed by the synchronization mechanism
Semaphore mechanism
  • Integer semaphore
  • Record semaphores
  • AND-type semaphores AND general semaphores
Semaphore Application
  • Semaphores implement process mutex
  • Semaphores describe the frontend relationship between processes
The basic concept of Process Synchronization in the Process Management Mechanism 1. Two forms of constraints between processes

There are two logical constraints between processes in the system:

Direct mutual constraint/Mutual Cooperation (Process Synchronization ):

That is, the direct control relationship between processes that complete the same task and wait for and exchange information for coordination of their work.

Indirect mutual constraint/Resource Sharing (process mutual exclusion ):

It is an indirect constraint that processes share exclusive resources and must be mutually exclusive.

Comparison between synchronization and mutex: 2. The shared resources that only one process can use at a time for critical resources and critical zones are called critical resources, such as printers, printers, variables, and data, processes share such critical resources in a mutually exclusive manner, so that concurrent processes are closed.

Example: producer-consumer problems

A group of producers provide products to a group of consumers. They share a buffer pool, where producers place products and consumers obtain products from them. It is an abstraction of many cooperative processes, such as input and computing processes, and computing and printing processes.

Set the buffer pool length to n (n> 0). A group of producer processes P1, P2 ,..., Pm, a group of consumer processes C1, C2 ,..., Ck ,. Assuming that the producer and consumer are equivalent to each other, as long as the buffer pool is not full, the producer can send the product to the buffer zone. Similarly, as long as the buffer pool is not empty, the consumer can take the product from the buffer zone and consume it.

The producer and consumer processes run in asynchronous mode, but the synchronization relationship must be maintained. That is, the producer is prohibited from delivering products to the full buffer pool, and the consumer is prohibited from extracting products from the empty buffer pool.

The producer consumer process shares the following variables:
Int n; // n indicates the number of buffers in the buffer pool. buffer: array [0 .. n-1] of item; // buffer is a buffer pool with n buffers; in, out: 0 .. n-1; // in and out are pointers, respectively pointing to the buffer zone of the next launched product and the counter of the next available product: 0 .. n;
The producer and consumer processes can be described as follows:
Void producer () // producer {while (1 ){...... Produce an item in nextp; // produce a product ...... If (counter <= n) {// when the number of production is equal to count, stop buffer [in] = nextp; in = (in + 1) mod n; counter = counter + 1;} else {break ;}} void consumer () // consumer {while (1) {while (counter> 0) {nextc = buffer [out]; out = (out + 1) mod n; counter = counter-1; consume the item in nextc; // consume a product} break ;}}
Errors may occur during concurrent execution. The problem lies in the counter variable shared by the two processes.

The operations to add and subtract 1 variables in machine language are as follows:

// Variable counter plus 1: register1 = counter; register1 = register1 + 1; counter = register1; // variable counter minus 1: register2 = counter; register2 = register2-1; counter = register2;

If you modify the variable in another order, what will happen?

register1 = counter;register1 = register1 + 1;register2 = counter;register2 = register2 - 1;counter = register1;counter = register2;

To ensure the correct use of critical resources, you can describe a cyclic process that accesses critical resources as follows:

Entry Section)

Add a piece of code before the critical section to check whether the critical resource to be accessed is accessed at the moment.

Exit Section)

Add a piece of code after the critical section to restore the access flag of the critical resource to the unaccessed flag.

Remainder Section)

Code other than the entry, critical, and exit areas in a process.

Several processes to enter the critical section must meet the following requirements:

(1) only one process can enter the critical section at a time.

(2) At any time, there must be no more than one process in the critical section.

(3) processes entering the critical section must exit within a limited period of time.

(4) If you cannot enter your critical section, the processor resources should be provided.

There are several methods to solve the problem of critical partition (mutex:

(1) software and hardware methods

(2) P-V operation

(3) Management Mechanism

3. rules to be followed by the synchronization mechanism
  • Idle forward: when no process is in a critical zone, it indicates that the critical resource is idle. A process that requests to enter the critical zone should be allowed to immediately enter its critical zone to effectively use the critical resource.

  • Busy waiting: When an existing process enters the critical zone, it indicates that the critical resource is being accessed. Therefore, other processes trying to enter the critical zone must wait to ensure mutually exclusive access to the critical resource.

  • Limited wait: For processes requiring access to critical resources, ensure that they can enter their critical zones within a limited period of time, so as not to fall into the "dead" status.

  • Let the process wait: when the process cannot enter its critical section, immediately release the processor to avoid the process being "busy ".

Semaphore mechanism
Semaphore mechanism is a synchronization mechanism proposed by Dutch scientist E. W. Dijkstra in 1965, also known as P and V Operations. From the initial integer semaphores to record semaphores, and then to the semaphores set mechanism.
1. Integer semaphores Integer semaphore-- Non-negative integer, except initialization (I .e. initial value: the initial value must be set once and only once, and the initial value cannot be negative, you can only access wait and signal through two atomic operations (or P and V Operations) (that is, the semaphore value can only be changed by these two atomic operations ). Wait and signal operation description:

Wait (S): While S <= 0 do no-op; test availability of resources

S = S-1; the number of available resources minus one unit

Signal (S): S = S + 1;

Main problems:As long as S is <= 0, wait operations are continuously tested (busy). Therefore,Waiting for permission".

2. Record-type semaphores Basic Ideas

1. Set an integer variable value (resource semaphore) that represents the number of resources)

2. Set a linked list L to link all the waiting processes waiting to access the same resource

Data Structure of record semaphores

Type semaphore = record

Value: integer;

L: list of processes;

End

Var S: semaphore;

Wait and signal operation description:
// Call the blocking primitive to set the process status to // wait state, and insert the PCB of the process into // the corresponding waiting queue S. L end; wait (S): S. value: = S. value-1; if S. value <0 then block (S. l); // call the wake-up primitive to wake up the corresponding waiting queue // S. L a process waiting in, changes its status // to the ready state, and inserts it into the ready queue signal (S): S. value: = S. value + 1; if S. value £0 then wakeup (S. L );

In the recording semaphore mechanism:

The initial value of S. value indicates the number of resources in the system.

The initial values of S. value are 1, and S is the mutex semaphores;

The initial value of S. value is greater than 1, and S is the resource semaphore;

In the recording semaphore mechanism

Each wait (S) operation means that the process requests resources of one unit, and the number of resources is reduced by 1. When S. value <0 indicates that the resource has been allocated, the process calls the Block () primitive to self-blocking, discard the processor, and insert it into the S. L of the semaphore linked list. | S. value | indicates the number of blocked processes in the semaphore linked list.

Each signal (S) indicates that the execution process releases one unit of resources, and the number of resources increases by 1. If S still exists after 1 is added. value <= 0, indicating that there are other processes waiting for the resource in the semaphore linked list, call the Wakeup () primitive to wake up the first process in the linked list.

AND semaphores (Understanding)

General semaphore set (Understanding)

Application of semaphores 1. Process mutex using semaphores

Semaphores can be used to easily solve the critical zone problem (process mutex) (see the figure below ). Set A mutex semaphore lock for critical resources. If the initial value is 1, the description of process A, process B, and process C are mutually exclusive:

The complete running sequence is shown as follows: 2. Use semaphores to describe the frontend relationship.

Method:

If node S1 points to the directed edge of node S2 in the figure, it indicates that the program segment S1 in process P1 should be executed first, and the program segment S2 in process P2 should be executed later. Set a semaphore s with an initial value of 0. Put V (s) behind S1, and execute P (s) before S2 ).

Statement sequence of process P1: S1; V (s)

Statement sequence of process P2: P (s); S2

For example, use semaphores to describe the relationship between the front Graph

The frontend graph with eight nodes. In the front graph, there are 10 directed sides and 10 semaphores can be set. The initial values are 0. There are 8 nodes and 8 concurrent processes can be designed as follows:
struct semaphore a,b,c,d,e,f,g,h,i,j=0,0,0,0,0,0,0,0,0,0cobegin     {S1;V(a);V(b);V(c);}    {P(a);S2;V(d);}    {P(b);S3;V(e);V(f);}    {P(c);S4;V(g);}    {P(d);P(e);S5;V(h);}    {P(f);P(g);S6;V(i)}    {P(h);P(i);S7;V(j);}    {P(j);S8;}coend
Reference: China East University of Science and Technology Operating System
Three processes share the same process segment. at most two processes can enter the same process segment at a time. If the P and V operations are used for synchronization, the semaphore S Value

S: 2,-1 ].
Solution: There are three processes in total, and only two processes are allowed to enter. The maximum value of s is 2,
If no resources are applied for by the three processes, s = 2;
If the three processes request resources at the same time, s =-1;

Multiple processes perform 5 P operations on semaphore S. After 2 V Operations, the current semaphore value is

There are three blocked processes. You can see the current semaphore value.
The initial value of the semaphores is 0. Each P operation is performed on the semaphores value-1. Each V operation is performed on the semaphores with a value of-1 and a value of-3 after 2 V Operations, therefore, the initial value is 0.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.