Inter-process communication (i): competitive conditions and mutually exclusive programmes

Source: Internet
Author: User

interprocess communication (i): conflicts between processes and how they are handled

--"Modern operating system chapter II, section III"

1, the question of the proposed

Let's imagine a fake offline printing program. When a process needs to print a file, it places the file under a spool folder. There is also a process responsible for periodically checking whether a file needs to be printed and, if so, printing it and deleting it in the folder.

Simple assumption. There are very many slots in the offline folder, each of which holds the file names, if they have two shared variables: out, point to the next file to be printed; in, point to the next spare slot.

, the next one that should be printed is slot 4th, and the next one should be slot # 7th.

Now, if process A, B will queue the file a, B, if a first read the information is 7, and a will 7 into one of its own memory variables, and then, the system feels that has been given a sufficient time, so interrupt a switch set process B. The process reads 7, stores 7 in one of its memory variables, and updates int to 8. At this point B has finished all the queue operation. Instead of doing other things. When a continues to run. It also felt that the file should be stored in slot # 7th. So a file successfully overwrites the B file, and our B file will never be printed. The problem arises.

2. Abstract some concepts

Race condition: Similar to the above scenario, two or more processes read and write some shared data. The final result depends on the precise timing of the execution of the process, called the race condition.

(The condition is understood as the situation, the competition situation, seemingly more easy to understand some =.) =

Mutual exclusion: Mutual exclusion is a means. It makes it impossible for processes that share data to process the data they share at the same time.

Critical section: a tablet that visits shared memory. In other words, through reasonable arrangements. So that two processes cannot be in the critical zone at the same time, the competition conditions can be avoided.

Busy waiting: A continuous test of a variable until a value appears to stop. (such as statement while (t!=0) {}, then T is zero.) The statement after the while () will never run, as if in the case of the book is also called hang)

Spin Lock: The lock used for busy waiting. (In 3-(3), turn even if the spin lock)

3, the mutual exclusion of busy waiting (several ways to achieve mutual exclusion)

(1) Shielding interrupt

Principle: When the process enters the critical area, it will block all interrupts immediately and open interrupts after leaving.

Disadvantage: A, multi-core system is invalid (other processes can still use other CPUs continue to access the public

Save

b, the user program to control the interruption is very critical (think about it.) A process mask is interrupted and no longer

Turn on interrupts. Then your system is GG)

Conclusion: Shielding interruption is a very useful technique in the system itself. However, the user process is not a suitable

Universal mutual exclusion Mechanism.

(2) Lock variable

Principle: The software realization mechanism of shielding interruption.

Assume a shared (lock) variable with an initial value of 0. There is no process in the critical area, the process enters the pro

The boundary area is changed to 1, representing a process within the critical area, if the process is before entering the critical section.

The lock variable value is 1. The process waits for its value to change to 0.

The reason for the failure: the same as the omission of the spool folder. Suppose a process enters the critical section, but

It interrupts the lock variable before 1, and a process enters the critical section 0 1, so.

It also resets the lock variable to 1 when the current process executes again, so that there are still two in the critical section

a process.

(3) Strict rotation

Principle: Share turn variables. Used to record the turn of the process into the critical section.

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

When turn=0, only process 0 can enter the critical section, process 0 will turn before leaving the critical area

Reset 1. thereby the sign. Turn process 1 into the critical section.

Disadvantages: Strictly rotating, may lead to processes outside the critical area that require access to critical areas (e.g.

such as: When Turn=0. means that only process 0 can enter the critical section, assuming that process 0 also

100 years to enter the critical area, and process 1 need to enter immediately, the process 1 is in vain to wait 100

Years.)

Summary: When a process is a lot slower than there is a process, it is not appropriate to use this approach.

(4) Peterson Solution

This is a simple mutual exclusion algorithm invented by Peterson.

Let's run the program in a split-case scenario:

A, process 0 enters the critical section by calling Enter_region (). At this point 1 also want to call Enter_region ()

To enter the critical section. But Interested[0] is true, so the while loop is suspended when process 0 out of the Pro

Leave_region () is called when the zone is bounded. Set Interested[0] to False, process 1 will be able to enter the critical

Area.

b, when process 0 is interrupted at random moment of call to the enter_region () process, process 1 enters the critical section

After process 0 is again, it will still be suspended. (In fact, the two inferred sentences in the while loop body

Ensure that when a process is in the critical section, there is a process that wants to enter the critical section must be suspended).

(5) TSL Instruction

Principle Brief:

The TSL (test and set lock) is a hardware support that is ideal for multi-processor computers for mutual exclusion. It will

When a process enters the critical section. Lock the memory bus so that other CPUs are blocked before the end of this instruction

Access to memory.

For the TSL directive, this article does a simple schematic description (although the teacher is more specific in class), want to enter

Learn more about the TSL directive and be able to take a look at the contents of this section of the book for specific elaboration.

4. Overview

To develop a programme that would enable it to avoid competitive conditions and ensure the efficiency of process execution and collaboration, 4 conditions must be met.

(1), no matter what two processes can not be in the critical section at the same time

(2), should not be the speed and quantity of the CPU do whatever assumptions

(3), the process of execution outside the critical area can not block other processes

(4), do not allow the process to wait indefinitely to enter the critical area

(model diagram to avoid competitive conditions)

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

Inter-process communication (i): competitive conditions and mutually exclusive programmes

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.