P, V operation and c++ code to realize the problem that father puts apples, mother puts oranges, two sons only eat oranges on the plate, and two daughters only eat apples on the plate

2. Comprehensive design of eating fruits

There is a plate on the table, which can hold up to two fruits, and only one fruit can be put in or taken out at a time. The father puts apples on the plate, and the mother puts oranges on the plate; the two sons wait to eat the oranges on the plate, and the two daughters wait to eat the apples on the plate. Please use P and V operations to realize the synchronization and mutual exclusion relationship between father, mother, son, and daughter.

[Design requirements]
Understand the semaphore mechanism, understand and master the process synchronization and mutual exclusion mechanism, be familiar with the operation function of the semaphore, and use the semaphore to realize the control of shared resources. Programming simulation realizes the program control of this problem, and analyzes the processing process.

1. 1. pv operation code

semaphore empty=2,mutex=1,apple=0,orange=0; 

void father(){
    
    
     do{
    
    
           P(empty);    //等待盘子为空
           P(metux);    //等待获取对盘子的操作
            爸爸向盘中放一个苹果;
           V(mutex);   //释放对盘子的操作
           V(apple);   //通知女儿可以来盘子中取苹果
    }while(TRUE);
}

void mather(){
    
                
     do{
    
    
           P(empty);		//等待盘子为空
           P(metux);		//等待获取对盘子的操作
            妈妈向盘中放一个桔子;
           V(mutex);		//释放对盘子的操作
           V(orange);		//通知儿子可以来盘子中取橘子
    }while(TRUE);
}

void son1(){
    
                            
     do{
    
    
           P(orange);       //判断盘子中是否有桔子
           P(metux);        //等待获取对盘子的操作
            儿子1取出盘中的桔子;
           V(mutex);      //释放对盘子的操作
           V(empty);      //盘子空了,可以继续放水果了
    }while(TRUE);
}
void son2(){
    
                            
     do{
    
    
           P(orange);       //判断盘子中是否有桔子
           P(metux);        //等待获取对盘子的操作
            儿子2取出盘中的桔子;
           V(mutex);      //释放对盘子的操作
           V(empty);      //盘子空了,可以继续放水果了
    }while(TRUE);
}


void daugther1(){
    
     
     do{
    
    
           P(apple);       //判断盘子中是否有苹果
           P(metux);        //等待获取对盘子的操作
            女儿1取出盘中的苹果;
           V(mutex);      //释放对盘子的操作
           V(empty);      //盘子空了,可以继续放水果了
    }while(TRUE);
}
void daugther2(){
    
     
     do{
    
    
           P(apple);       //判断盘子中是否有苹果
           P(metux);        //等待获取对盘子的操作
            女儿2取出盘中的苹果;
           V(mutex);      //释放对盘子的操作
           V(empty);      //盘子空了,可以继续放水果了
    }while(TRUE);
}

void main() {
    
                     //四个并发进程的同步执行
	cobegin
	   father(); mather(); 	son();son();daugther();daugther();
	coend
}

2. c++ program code

#include <stdio.h>
#include <stdlib.h>

#include <semaphore.h>
#include <errno.h>
  #include <unistd.h>
#define total 2
 
sem_t remain, apple, orange, mutex;
static unsigned int vremain = 2, vapple = 0, vorange = 0;
 
void *father(void *);
void *mather(void *);
void *son1(void *);
void *son2(void *);
void *daughter1(void *);
void *daughter2(void *);
void print_sem();
 
int main() {
    
    
	pthread_t fa, ma, so ,da;
	sem_init(&remain, 0, total);//总数初始化为2
	sem_init(&apple, 0, 0);//盆子中苹果数, 开始为0
	sem_init(&orange, 0, 0);//盆子中梨子数, 开始为0
	sem_init(&mutex, 0, 1);//互斥锁, 初始为1
 
	pthread_create(&fa, NULL, &father, NULL);
	pthread_create(&ma, NULL, &mather, NULL);
	pthread_create(&so, NULL, &son1, NULL);
	pthread_create(&da, NULL, &daughter1, NULL);
 	pthread_create(&so, NULL, &son2, NULL);
	pthread_create(&da, NULL, &daughter2, NULL);
	for(;;);
}
 
void *father(void *arg) {
    
    
	while(1) {
    
    
		sem_wait(&remain);
		sem_wait(&mutex);
		 vremain--;
 		vapple++;
		printf("父亲放苹果, 剩余空间=%u, 苹果数=%u\n", vremain, vapple);
		sem_post(&mutex);
		sem_post(&apple);
		sleep(1);
	}
}
 
void *mather(void *arg) {
    
    
	while(1) {
    
    
		sem_wait(&remain);
		sem_wait(&mutex);
	vremain--; vorange++;
		printf("母亲放橘子, 剩余空间=%u, 橘子数=%u\n", vremain, vorange);
		sem_post(&mutex);
		sem_post(&orange);
		sleep(2);
	}
}
 
void *son1(void *arg) {
    
    
	while(1) {
    
    
		sem_wait(&orange);
		sem_wait(&mutex);               
	vremain++; vorange--;
		printf("儿子吃橘子, 剩余空间=%u, 橘子数=%u\n", vremain, vorange);
		sem_post(&mutex);
		sem_post(&remain);
		sleep(3);
	}
}
 void *son2(void *arg) {
    
    
	while(1) {
    
    
		sem_wait(&orange);
		sem_wait(&mutex);               
vremain++; vorange--;
		printf("儿子2吃橘子, 剩余空间=%u, 橘子数=%u\n", vremain, vorange);
		sem_post(&mutex);
		sem_post(&remain);
		sleep(3);
	}
}
void *daughter1(void *arg) {
    
    
	while(1) {
    
    
		sem_wait(&apple);
		sem_wait(&mutex);
 vremain++; vapple--;
		printf("女儿1吃苹果, 剩余空间=%u, 苹果数=%u\n", vremain, vapple);
		sem_post(&mutex);
		sem_post(&remain);
		sleep(3);
	}
}
 
void *daughter2(void *arg) {
    
    
	while(1) {
    
    
		sem_wait(&apple);
		sem_wait(&mutex);
vremain++; vapple--;
		printf("女儿2吃苹果, 剩余空间=%u, 苹果数=%u\n", vremain, vapple);
		sem_post(&mutex);
		sem_post(&remain);
		sleep(3);
	}
}
 
void print_sem() {
    
    
	int val1, val2, val3;
	sem_getvalue(&remain, &val1);
	sem_getvalue(&apple, &val2);
	sem_getvalue(&orange, &val3);
	printf("Semaphore: remain:%d, apple:%d, orange:%d\n", val1, val2, val3);
}

3. Program simulation screenshot

Screenshot of the first simulation experiment
insert image description here

Screenshot of the second simulation experiment
insert image description here

Guess you like

Origin blog.csdn.net/qq_45808700/article/details/117432329