Operating system: Inter-process interaction (multithreaded basis)

Source: Internet
Author: User

Interaction between processes two interactions are synchronized

Coordination of multiple related processes in the order of execution .

restriction Relationship: direct restriction.

: When a process performs an operation, another process must wait, in order to wait and reconcile , without contending for critical resources.

Mutually exclusive

Multiple processes because of contention for critical resources two mutually exclusive execution of the process is called the mutex of the process .

A critical resource: also known as an exclusive resource, refers to a resource that allows only one process to access it over a period of time.

Restriction Relationship: indirect restriction.

resolving problems with concurrent processes I. Lock Method--Spin lock

Ideas:

Set a shared variable W (lock) with an initial value of 0. When a process wants to enter its critical section (a program segment that involves critical resources in a process), it first
Test this lock first: if the value of the lock is 0, the process resets it to 1 and enters the critical section. If the lock is already 1, the process waits until it becomes 0.
implementation:
Ka Yuan: LOCK (W): L:if w=1 then goto L else w=1;
Shiyuan language: UNLOCK (W): w=0;

two. Signal volume and PV operation

Signal Volume:
? Description:
The entity that represents the resource--is a queue-related integer variable.

Description: Its value can only be accessed through the initialization operation and the P, v operation.

? Type:
Common semaphores: For mutual exclusion between processes, the initial value is typically 1.
Private semaphore: For synchronization between processes, the initial value is usually 0 or N.
? p operation (wait operation):
proberen--check. means that the request is assigned a unit resource .

s=s-1if(s<0) {    // Call process blocked, enter S waiting queue }


? v operation (signal operation):
Dutch "Verhogen"-The Meaning of "increment" means releasing/adding a unit of resources .

s=s+1; if (s<=0) {    // wakes a process from the wait queue in S to get it into a ready state}


? using PV for mutual exclusion

? using PV for process synchronization

IPC Classic QuestionsProducer Consumer issues (limited cache issues)

Description: producers and consumers share n buffers, producers produce products into buffers, and consumers take product consumption from buffers. Please write the code that correctly reflects their logical relationship

  

Two implied conditions:

1. The number of consumers and producers is not fixed.

2. Consumers and producers cannot use buffers at the same time.

Behavioral Analysis:

Producer: Production products, placing products (with empty buffers).

Consumer: Take out products (with products), consumer products.

Behavioral relationships:

Between producers: mutual exclusion (placement of products)

Between consumers: Mutex (remove product)

Between producer and consumer: mutual exclusion (put/take product) sync (place--remove)

Semaphore settings:

Semaphore Mutex =1;//Mutex

Semaphore empty=n;//Idle Quantity

Semaphore full=0;//Number of products

Pseudo code:

Semaphore mutex=1 //MutexSemaphore Empty=n//Buffer Idle numberSemaphore full=0 //Number of productsProducer: while(1) {product;//Productionp (empty);    P (mutex); Add to buffer;//Placing ProductsV (mutex); V (Full)} consumer: while(1) {p (full);    P (mutex); Get  fromBuffer//Remove ProductV (mutex); V (empty) conseume; //consumption}

Reader Writer's question

Description: A Data object (file, record) can be shared for multiple concurrent processes. Some of these processes only need to read the content, we call the "reader", and some processes are responsible for updating (reading and writing) the contents of which we call "writer".

Rule: " readers " can read shared data objects at the same time ; " The writer " cannot access shared data objects concurrently with any other process ."

  

Behavioral Analysis:

The behavior of the read process:

    • Multiple read processes in the system can access shared data at the same time.
    • We can divide them into three categories: the first to enter the read process (the owning resource), the last to leave the read process (freeing the resources) and other read processes.
    • We need to set a counter readnum to record the number of read processes.

The behavior of the write process: exclusive use of resources.

? Determine synchronization and mutex relationships:

Reader-Reader: mutually exclusive access Readnum

Reader-Writer: mutually exclusive access to data

Writer-Writer: mutually exclusive access to data

? Identify critical resources:

Data,readnum

Semaphore settings:

int readnum=0;

Semaphore mutex=1;//Common semaphore for mutual exclusion of readnum.

Semaphore write=1;//Common semaphore for mutual exclusion of data access.

Pseudo code:

intreadnum=0;//count, used to record the number of readersSemaphore mutex=1;//common semaphore for Readnum mutexSemaphore write=1;//common semaphore, which is used for mutual exclusion of data access,Reader: P (mutex)//mutual exclusion of Readnumreadnum++;if(readnum==1) P (write)//request to use the data resourceV (Mutext)//Release Readnumreading;p (Mutex)//mutual exclusion of Readnumreadnum--;if(readnum==0) V (write)//releasing the data resourceV (Mutext)//Release Readnumwritten by://P (Mutex)P (Write)//write itself is mutually exclusivewriting;v (Wirte)//V (Mutex)

Barber Questions

Description: There is a barber and a barber chair in the barber shop.   If there is no customer, the barber sleeps in the barber's chair, and when a customer arrives, the barber wakes him up when he sleeps, and if the barber is busy with a haircut, he sits in the chair and waits. Write a program that implements the correct description of hairdresser and customer behavior.

  

Behavioral Analysis:

Barber behavior: Sleep, haircut. No customers sleep, and customers have haircuts.

? Customer behavior: Haircuts or waiting.

? Reciprocal effects:

Between the Barber and the Customer: synchronization

Between customer and customer: No

Semaphore settings:

Semaphore customers=0; Customers says the number of customers waiting for a haircut

Semaphore barbers=0; Barbars says the number of barbers waiting for customers

Pseudo code:

Semaphore customers=0//customers indicates the number of customers waiting for a haircut semaphore barbers=0;  // Barbars says the number of barbers waiting for customers Barber Code:  while (1) {    // Check if there is a customer    V (barbers)  // Tell the customer there is a hairdresser      Cut_hair ();} Customer Code: V (Customers) p (barbers); Get_hair ();

This mainly embodies the synchronization of the process. If in doubt, see the synchronous implementation of the semaphore description.

added condition: The barber shop has n chairs, when the customer arrives if the barber is free to have a haircut, if the barber is busy, then see if there is a vacant seat on the chair, there is an empty position to wait, no empty location to leave.

Pseudo code:

Semaphore customers=0;//customers says the number of customers waiting for a haircutSemaphore barbers=0;//Barbars says the number of barbers waiting for customersintWaiting =0;//Number of waitingSemaphore mutex=1;//Mutex for waitingHairdresser Process: while(1) {p (Customers)//Check if there are customersP (mutex); Waiting=waiting-1;    V (mutex); V (Barbers) Cut_hair ();} Customer Process: P (Mutex)//The operation of the duty-free chair is mutually exclusive, i.e. aif(waiting<n) Then//If the seat is not full{Waiting=waiting+1;    V (mutex);    V (Customers); P (barbers); //Check if there is a hairdresserget_haircut ();}Else{V (mutex);//It means the seats are full.}
Precautions for using PV operations

1.P and V operations (for the same semaphore) are always paired ; They are in the same process when mutually exclusive operations are in progress; They are in a different process when synchronizing operations.

2. The setting of the initial value of the semaphore and the position and order of the P and V operations are key, so be very careful to set up, must maintain the correct logic relations and high execution efficiency.

Operating system: Inter-process interaction (multithreaded basis)

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.