Operating System-Implementation and Application of processes/Threads internal communication-semaphores and PV operations (solving the dining and producer and consumer problems of philosophers), system-pv

Source: Internet
Author: User

Operating System-Implementation and Application of processes/Threads internal communication-semaphores and PV operations (solving the dining and producer and consumer problems of philosophers), system-pv

Main content of this article:

  • Semaphore implementation
  • Use semaphores to solve the dining problem of philosophers
  • Use semaphores to solve producer and consumer problems
I. Implementation of semaphores 1.1 semaphore Structure
typedef struct{    int value;    struct process * list} semaphore;

Value indicates the quantity that can be used by the current semaphore, and list indicates the process waiting on the current semaphore.

1.2 P operation implementation
P(semaphore * s){    s.value--;    if(s.value < 0)    {        add current process to s.list;        block();    }}

If s. value is <0. No resources are available. The current process is added to the list (waiting for Process list) and the current process is blocked.

1.3 V Operation implementation
V(semaphore * s){    s.value++;    if(s.value <= 0)    {        remove a process A from s.list;        wakeup(A);    }}

S ++ indicates that a resource is released. If s. value ++ is still <= 0. It indicates that at least one process is waiting for resources. At this time, several processes are taken out of the list and the process is wakeup, in this way, the process can start to use the resources that have just been released (this process may not start running immediately, at least in ready state, and its running depends on the CPU scheduling algorithm ).

1.4 busy waiting

The previous Peterson solution achieved mutex access between processes through a busy wait. PV operations are unavoidable. The premise of PV operations is that no two processes simultaneously perform PV operations on a semaphore. At this time, semaphores are semaphores and are critical regions. How can P and V Operations be mutually exclusive to semaphores?

The operating system also waits for this (when a process operates on the semaphore, other processes remain waiting ). However, the busy wait will eventually avoid minimizing the busy wait time at the semaphore level, because the pv operation time is very fast.

Ii. demonstration of how to solve the dining problem of philosophers by using semaphores 2.1

Five philosophers, as shown in:

  • Every two philosophers have a pair of chopsticks, a total of five
  • Philosophers are thinking, and when they are hungry, they will pick up the chopsticks on both sides for dinner.
  • You can only start eating after getting two chopsticks.

Obviously, if a philosopher wants to eat, if any of his neighbors are eating, he cannot eat, there are not enough chopsticks (either only one or none ).

2.2 Solutions
void philosopher(i){    while(true)    {        P(chopstick[i]);        P(chopstick[( i + 1 ) % 5]);        eat();        V(chopstick[i]);        V(chopstick[( i + 1 ) % 5]);        think();    }}

I Represents the I-th philosopher ). The core idea of the program is to use the PV operation to keep the philosophers waiting until the chopsticks on both sides of the program are idle and then start to eat, and then put the chopsticks back after eating.

Number of philosophers and chopsticks

When philosopher 0 wants to eat, waiting for 0, 1 chopsticks, and philosopher 2 wants to eat, waiting for 2, 3 chopsticks ..

2.3 questions

The preceding solution has the following Disadvantages:

When all philosophers want to eat at the same time, a deadlock will occur, always waiting (starvation ):

The next blog post specifically introduces how to solve starvation.

Iii. Use semaphores to solve producer and consumer problems

The previous article introduced the producer and consumer modes, but this mode is problematic and wakeup may be lost. In fact, the core issue is still due to) no process mutex access is performed for access to the buffer, and no mutex access is performed for access to the buffer. This problem can be solved through the PV operation.

Set three semaphores:

  • Full indicates the number of items in the buffer. The default value is 0.
  • Empty indicates the number of idle buffers. The default value is the buffer capacity n.
  • Mutex, which provides mutex operations on the buffer. 1 indicates that the buffer can be accessed. 0 indicates that the buffer cannot be accessed. The initial value is 1;
Void producer () {while (true) {produce_item (); P (empty); // idle capacity-1. If there is no idle capacity, wait, go to the block list P (mutex); // check whether the buffer can access insert_item (); V (mutex); // release the buffer access right V (full ); // number of items + 1. If there is a consumer waiting, wake up a consumer} void consumer () {while (true) {P (full); // number of items-1, if no item can be consumed, go to the block list p (mutex); // check whether the buffer can access remove_item (); V (mutex ); // release the buffer access permission V (empty); // idle capacity + 1. If there is a producer waiting, wake up a producer consume_item ();}}

 

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.