Typical program for PV operation in C language

Typical program for PV operation in C language

PV operation is one of the typical synchronization mechanisms. Connect a semaphore to a message. When the semaphore value is 0, it means that the desired message has not been generated; when the semaphore value is not 0, it means that the desired message already exists. When the PV operation is used to achieve process synchronization, the P operation is called to test whether the message arrives, and the V operation is used to send the message.

concept

Critical section ** refers to a program segment that accesses shared resources (for example, shared equipment or shared memory), and these shared resources cannot be accessed by multiple threads at the same time.

Scheduling principle of critical area:

1. If there are several processes that require access to an idle critical section, only one process is allowed to enter at a time.

2. There can be no more than one process in the critical zone at any time. If an existing process enters its own critical section, all other processes attempting to enter the critical section must wait.

3. The process of entering the critical section should exit within a limited time so that other processes can enter their critical section in time.

4. If the process cannot enter its own critical area, it should give up the CPU to avoid the process of "busy waiting" phenomenon.

Semaphore : can be used for inter-process synchronization or thread synchronization between the same process.

The semaphore has a usage counter. This usage counter is the difference between the maximum resource count of the semaphore and the current resource count.

When the value of the counter is greater than 0, it indicates the current number of available resources.

When the value of the counter is less than 0, the table waits for the number of processes using resources.

A counter value of 0 means that there are neither available resources nor processes waiting for resources

Suppose S is the value of the counter of the semaphore:

P operation : Performing a P operation means requesting the allocation of a unit of resources, so the value of S is reduced by 1. When S <0, it means that no resources are available. The requester must wait for another process to release this type of resource before it can continue. run.
V operation : Performing a V operation means releasing a resource, so the value of S is increased by 1. When S <0, it means that some process is waiting for a resource, so it is necessary to wake up a process in a waiting state to continue running.

Examples

To solve the problem of PV operation, a fixed code demo can be applied. It is a while with three parts: a P at the end, a V at the end, and a critical section in the middle.

1587368244396

1587368291998

Use PV operation to solve the problem: Dad puts apples, daughter takes apples, mom puts oranges, and son takes oranges.

It can be seen from the question that 4 processes are required, and the processes of dad and mom need to be performed. We can apply the model separately

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include<semaphore.h>


#define P sem_wait
#define V sem_post 
#define full_apple &fullA
#define full_orange &fullB
#define empty &empty_aha


sem_t fullA;
sem_t fullB;
sem_t empty_aha;
int num=0; 



void* Dad(void *p)
{
    while(num<50)
    { 
        P(empty);  
	    num++;
        printf("老爸放了个苹果%d\n",num);
        V(full_apple);
    }
}


void* Dangter(void *p)
{
    while(num<50)
    {
        P(full_apple);
		num++;               
         printf("女儿吃了个苹果%d\n",num);
        V(empty);
    }
}

void* Mum(void *p)
{
    while(num<50)
    { 
        P(empty);  
	    num++;
        printf("老妈放了个橘子%d\n",num);
        V(full_orange);
    }
}


void* Son(void *p)
{
    while(num<50)
    {
        P(full_orange);
		num++;               
        printf("儿子吃了个橘子%d\n",num);
        V(empty);
    }
}

int main()
{
    
    sem_init(full_apple, 0, 0);
    sem_init(full_orange, 0, 0);   
    sem_init(empty, 0, 1);   
 
    pthread_t tid0;
    pthread_t tid1;
    pthread_t tid2;
    pthread_t tid3;
    pthread_create(&tid0, NULL, Dad, NULL);
    pthread_create(&tid1, NULL, Mum, NULL);
    pthread_create(&tid2, NULL, Son, NULL);
    pthread_create(&tid3, NULL, Dangter, NULL);
    getchar();
    pthread_exit(0);
    return 0;
}

Guess you like

Origin www.cnblogs.com/lightice/p/12738215.html