Process synchronization and Semaphores solutions

Source: Internet
Author: User
Tags mutex semaphore

Process synchronization is a time-series relationship between events occurring in multiple processes and requires co-operation to accomplish a task together. Specifically, when a process runs to a point, another partner process is required to provide it with information, and before the message is obtained, the process enters the blocking state and is awakened to the ready state after the message is received.

1 Producer/consumer issues

Problem Description:

1) Producer process produces some type of data placed in the buffer

2) The consumer takes data from the buffer and takes one item at a time

3) only one producer or consumer can operate the buffer

To resolve the issue:

1) The buffer is full and the producer no longer produces

2) buffer empty, consumers no longer take

3) Production and consumption cannot be operated simultaneously

1 #defineN 1002 intCount =0;3 voidProducervoid)4 {5     intitem;6      while(true) {7item =Produce_item ();8         if(Count = =N)9 sleep ();Ten Insert_item (item); Onecount++; A         if(Count = =1) - Wake_up (consumer); -     } the } -  - voidConsumervoid) - { +     intitem; -      while(true) { +         if(count==0) A sleep (); atitem =Remove_item (); -count--; -         if(Count = n1) - wake_up (producer); - Consume_item (item); -     } in}

What problems will arise?

When consumers interpret count==0 and before entering sleep, the CPU is switched off, and the producer produces 1 to awaken the consumer (not sleep) until the production is full. After that, two of the processes will not wake up in sleep state.

2 signal volume and p, v operation

Initially it was to solve the mutual exclusion (two-dollar semaphore), which was then generalized to the general semaphore (multi-value) to resolve the synchronization.

Only three operations can be performed on the semaphore: initialization, p and V operation.

1 structSemaphore2 {3     intcount;4 queuetype queue;5 };6 7 P (s)8 {9s.count--;Ten     if(S.count <0) { One the process state changes to a blocking state; A Insert the process at the end of the corresponding rank queue s.queue; - re-Dispatch; -     } the } -  - Q (s) - { +s.count++; -     if(S.count <=0) +     { A wake up a process in the corresponding level waiting queue s.queue; at change its state to a ready state and insert it into the ready queue; -     } -}

3 Resolving process mutexes with semaphores

1) Analyze critical activities of concurrent processes and classify critical areas

2) Set mutex mutex with initial value of 1

3) Pre-critical zone P (mutex)

4) After the critical section Q (Mutex)

4 solving producer/consumer problems with semaphores

1 #defineN 1002typedefintSemaphore;3Semaphore Mutex =1;4semaphore empty = -;5Semaphore full =0;6 7 voidProducervoid)8 {9     intitem;Ten      while(true) { Oneitem =Produce_item (); A P (empty); - P (mutex); - Insert_item (item); the V (mutex); - V (full); -     } - } +  - voidConsumervoid) + { A     intitem; at      while(true) { - P (full); - P (mutex); -item =Remove_item (); - V (mutex); - V (empty); in Consume_item (); -     } to}

5 solving reader/writer problems with semaphores

Question: 1) allow multiple readers to read at the same time 2) does not allow more than one writer to write at the same time 3) read and write cannot run concurrently.

First reader/writer question: readers first

If the reader executes:

1) No other reader or writer, readable by the reader

2) If a writer waits, but another reader reads, the reader can also read

3) If written by a writer, the reader must wait

If the writer executes;

1) No other reader or writer is written by the writer.

2) If a reader is reading, the writer, etc.

3) If any other writer is writing, then

1 voidReadervoid)2 {3      while(true) {4 P (mutex);5Rc++;6         if(rc==1)7P (w);//First Reader8 V (mutex);9 Ten Read Operation One  A P (mutex); -rc--; -         if(rc==0) theV (w);//One last reader - V (mutex); -     } -  + } -  + voidWritervoid) A { at      while(true) { - P (w); -  - Write Operations -  - V (w); in     } -}

Process synchronization and Semaphores solutions

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.