Communication basis between processes in Linux Network Programming (II): Introduction to deadlocks, semaphores, and PV primitives

Source: Internet
Author: User

I. deadlock

(1) deadlock refers to a phenomenon in which multiple processes wait for each other's resources, but do not release their own resources before obtaining the other's resources. If all processes are waiting for an impossible event, the process will be deadlocked.

(2) Conditions for deadlock:

Mutex

A process schedules its resources, that is, a resource is only occupied by a process within a period of time.

Request and persistence Conditions

When a process is blocked due to a resource request, the obtained resources are retained.

Deprivation conditions

Resources obtained by a process cannot be deprived before they are used up. You can only release them when they are used up.

Loop wait Condition

Each process forms a closed ring chain, and each process waits for the resources occupied by the next process.


(3) methods to prevent deadlocks

One-time resource allocation: (damage requests and maintenance conditions)
Resources that can be deprived: (damage the conditions that cannot be deprived)
Orderly Resource Allocation Method: (damage the cyclic wait condition)


(4) Deadlock Avoidance

Several deadlock prevention policies can seriously damage system performance. Therefore, when avoiding deadlocks, we must apply weak restrictions to achieve satisfactory system performance.
Because the process is allowed to dynamically apply for resources in a deadlock-free policy. Therefore, the system calculates the security of resource allocation in advance before allocating resources. If this allocation does not cause the system to enter an insecure state, the resource will be allocated to the process; otherwise, the process will wait. The most representative Deadlock Avoidance algorithm is the bankers algorithm.


(5) Bankers Algorithm

Bankers stipulate that:
* A customer can accept the customer if the maximum demand for funds does not exceed the banker's existing funds;
* Customers can make loans by installment, but the total number of loans cannot exceed the maximum demand.
* When the banker's existing funds cannot meet the loan amount that the customer needs, the loan to the customer can be postponed, but the customer can always get the loan within a limited period of time.
* After obtaining all the required funds, the customer will be able to return all the funds within a limited period of time.


(6) Dining Philosophers

Five philosophers eat at a round table. Each person must pick up two chopsticks before dining. when each person picks up the left chopsticks first and waits for the right chopsticks, a deadlock will occur.

Dining Philosophers

Waiter Solution
Up to four philosophers
A philosopher is allowed to take chopsticks only when both sides are available.
Number all philosophers. Philosophers with odd numbers must first take the chopsticks on the left. Philosophers with even numbers are vice versa.


Ii. semaphores and PV primitives

(1) semaphores

Semaphores and P and V primitives proposed by Dijkstra (dijela)

Struct semaphore
{

Int value;

Pointer_pcb queue;

};

Semaphores

Mutual Exclusion: P and V are in the same process.
Synchronization: P and V are in different processes.

Signal Value Meaning

S> 0: s indicates the number of available resources.
S = 0: no available resources and no waiting process
S <0: | S | Number of processes in the waiting queue



(2) P primitive pseudocode

P (S)
{

S. value = S. Value --;

If (S. value <0)

{

This process is in the waiting status.

Insert the PCB pointer of the process into the end of the corresponding waiting queue S. Queue

}

}

Note that PV operations are atomic operations, either all or all, and return after blocking is a complete process. However, if the ipc_nowait option is set, when the resource is temporarily unavailable, an error is returned directly. At this time, the semaphore operation is not executed.


(3) V primitive pseudocode

V (s)
{

S. value = S. Value ++;

If (S. value <= 0)

{

Wake up a process in the waiting queue S. Queue

Change its status to ready state

And insert it into the ready queue

}

}


(4) use PV primitives to solve driver and conductor Problems



(5) use PV primitives to solve the civil aviation ticket sales problem


Each client is executing the PV operation process.


(6) use PV primitives to solve the car rental problem

A car rental company has two convertible cars for rent. If four customers have to rent a convertible car at the same time, there will certainly be two people who cannot rent it.


Each customer is performing an operation like PV.


Refer:

Chapter 1 TCP/IP details

UNP

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.