Operating System Learning Notes (8) An implementation algorithm for mutual exclusion and synchronization

Source: Internet
Author: User
Tags mutex

============================
Process Mutex and synchronization:

How to coordinate the competition and sharing of resources between multiple processes.
How to solve the problem that the execution result is abnormal because of competing resources, even cause the system instability, failure and so on.

It is very important to control the concurrency of many processes by operating system with multi-channel program concurrent design technique.

Concurrency control:
(Competitive resources)
1. When concurrent processes compete to use the same resource, conflicts occur between them.
2. Critical resources cannot be used at the same time and must be used mutually exclusive.
3. To achieve mutual exclusion, there must be blocking.
4. The blocked process will never get the requested resource, which is a deadlock.

Critical Zone usage principles:
1. Busy is waiting.
2. Limited waiting. (cannot be unlimited to occupy each critical resource, so that the process outside the critical zone waits indefinitely)
3. Idle let in.
4. Right to wait. (Cannot block waiting for an event for a long period of time in the critical section)

Competitive Resources:
Hunger

Concurrency control:
(Co-ordination)
Make sure that the write operation is mutually exclusive.

Concurrency control:
(Communication Collaboration)

Mutually exclusive and synchronous resolution strategies for processes:
1. The Software method (the process executes the corresponding program instruction itself, it is difficult to control the process synchronization and mutual exclusion, increase the system overhead.) )
2. Hardware methods (shielding interrupts, hardware binding conditions)
3. Semaphore method *
4. Pipe-Path method
5. Message Delivery Methods

Software method:
。 Dekker algorithm, Peterson algorithm
Initial assumption: Turn into the critical section
var turn:0,1//shared Global variables

============p0=================
while (Turn! = 0) {
Nothing
}
< critical area >
turn = 1;

==============p1===============
while (Turn! = 1) {
Nothing
}
< critical area >
turn = 0;

Analysis: A tentative idea
Issue 1: "Busy waiting" phenomenon.
Issue 2: Critical areas are not fully utilized, which seriously affects progress and reduces system performance.
Issue 3: The process does not modify the value of turn, which will affect other process propulsion.

To modify the algorithm:
1. Set critical section State
var flag = Array[0,1]:ture|false

============p0===============
while (Flag[1]) {

}
Flag[0] = true;
< critical area >
Flag[0] = false;

=============p1================
while (Flag[0]) {

}
FLAG[1] = ture;
< critical area >
FLAG[1] = false;
Analysis: This algorithm cannot achieve mutual exclusion.
Second-time improvements:
============p0===============
Flag[0] = true;
while (Flag[1]) {

}
< critical area >
Flag[0] = false;

=============p1================
FLAG[1] = ture;
while (Flag[0]) {

}
< critical area >
FLAG[1] = false;
Analysis: Deadlock.
Third-time improvements:
============p0===============
Flag[0] = ture;
while (Flag[1]) {
Flag[0] = false;
< delay for some time >;
Flag[0] = true;
}
< critical area >;
Flag[0] = false;

=============p1================
FLAG[1] = ture;
while (Flag[0]) {
FLAG[1] = false;
< delay for some time >;
FLAG[1] = ture;
}
< critical area >;
FLAG[1] = false;
Analysis: Deadlock.

Dekker algorithm:
var flag[2] = {true, false}; Critical section State
var turn:0,1//order to enter the critical section

============p0==============
Flag[0] = ture;
while (Flag[1]) {
if (turn = 1) {
Flag[0] = false;
while (turn = 1) {

}
Flag[0] = true;
}
}
< critical area >;
turn = 1;
Flag[0] = false;
============p1==============
FLAG[1] = ture;
while (Flag[0]) {
if (turn = 0) {
FLAG[1] = false;
while (turn = 0) {

}
Flag[1] = true;
}
}
< critical area >;
turn = 0;
FLAG[1] = false;


Mutex and synchronous Workaround: (Hardware method)
1. Shielded interrupts:
It costs too much.
2. Dedicated Machine instructions:
Testset directive
Boolean testset (int i)
{
if (i = 0) {
i = 1;
return true;
}else
return false;
}

P (int i)
{
while (Testset (Bolt)) {
Do nothing;
}
< critical area >;
Bolt = 0;
}

int main ()
{
Bolt = 0;
P (1);
P (2);
...
P (n);
return 0;
}




Exchange directives:

Procedure Exchange (register R, memory m)
{
Swap variables.
}

Procedure P (int i)
{
int key;
while (true) {
key = 1;
while (key = 1) {
Exchange (key, Bolt);
}
< critical area >;
Exchange (key, Bolt);
}
}

int main ()
{
Bolt = 0;
P (1);
P (2);
...
P (n);
return 0;
}
Analysis: "Busy" still exists, there may be "hunger and thirst" phenomenon. There may be a deadlock phenomenon.

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.