当前位置:网站首页>Eating fruit
Eating fruit
2022-07-03 08:31:00 【Spring and autumn sword armor Li Chungang】
Father feeds fruit to his daughter and son . Dad randomly selects oranges or apples and puts them on the plate . My daughter only eats oranges , My son only eats apples . The plate can hold 2 Fruit . Use semaphores to coordinate processes , Print to show that you have eaten the corresponding fruit
Topic analysis
This is a classic communication problem between processes
Dad randomly put the fruit on the plate , It can be abstracted as a producer ,
daughter , Son eats fruit , It can be understood as two consumers , What is special is , Two consumers will only consume the specified products
So we can define three processes , Namely : producer , Consumer one , Consumer number two .
The content in the process can be defined as : Producers produce 0 or 1, Consumer one will only consume 1, Consumer number two will only consume 0.
The above is the main code framework , Next is PV operation , According to the number of producers and consumers , We define 4 A semaphore , Respectively used to control producers , consumer 1, consumer 2, And the control signal written to the cache , The semaphore corresponding to the producer and buffer should be initialized to 1, Only in this way can we ensure that the process can proceed normally , And it can block normally . For the other 0, Of course, this is to let producers produce first , According to normal logic , You set one of the consumers to 1, It can also run
After arranging the semaphore reasonably , You need to create a shared memory , It just needs to open up a space , To ensure that the three processes can read or input the corresponding values in memory .
The above is the general idea , As for function and code calls , Baidu .
Code implementation
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include <sys/shm.h>
#include<stdio.h>
#include <sys/sem.h>
union semun
{
int val;
struct semid_ds *buf;
unsigned short *arry;
};
int main()
{
int semid1 = semget((key_t)1234, 1 , 0640|IPC_CREAT);
union semun sem_union1;
sem_union1.val = 1;
semctl(semid1,0,SETVAL,sem_union1);
struct sembuf sem_b;
int semid2 = semget((key_t)1235, 1 , 0640|IPC_CREAT);
union semun sem_union2;
sem_union2.val = 0;
semctl(semid2,0,SETVAL,sem_union2);
struct sembuf sem_a;
int semid3 = semget ((key_t)1111,1, 0640|IPC_CREAT);
union semun sem_union3;
em_union3.val = 0;
semctl(semid3,0,SETVAL,sem_union3);
struct sembuf sem_c ;
int semid4 = semget((key_t)1110,1,0640|IPC_CREAT);
union semun sem_union4;
sem_union4.val = 1semctl(semid4, 0, SETVAL,sem_union4);
struct sembuf sem_d ;
int shmid = shmget(IPC_PRIVATE , 2, 0640|IPC_CREAT);
if(shmid <0)
{
printf("error!");
}
char *ptext = 0 ;
ptext = (char*)shmat(shmid,0,0);
pid_t pa = 0;
pa = fork();
if(pa<0)
{
printf("erorr!");
}
else if (pa==0)
{
pid_t pb = 0;
pb = fork();
if(pb<0)
{
printf("erorr!");
}
else if(pb ==0)
{
ptext = (char*)shmat(shmid,0,0);
while(1)
{
sleep(random()%10);
sem_a.sem_num = 0;
sem_a.sem_op = -1;
sem_a.sem_flg = SEM_UNDO;
semop(semid2, &sem_a, 1);
sem_d.sem_num = 0;
sem_d.sem_op = -1;
sem_d.sem_flg = SEM_UNDO;
semop(semid4, &sem_d, 1);
printf("I am girl , get %s\n",ptext);
sem_d.sem_num = 0;
sem_d.sem_op = 1;
sem_d.sem_flg = SEM_UNDO;
semop(semid4, &sem_d, 1);
sem_b.sem_num = 0;
sem_b.sem_op = 1;
sem_b.sem_flg = SEM_UNDO;
semop(semid1, &sem_b, 1);
}
}
else
{
ptext = (char*)shmat(shmid,0,0);
while(1){
sleep(random()%10);
sem_c.sem_num = 0;
sem_c.sem_op = -1;
sem_c.sem_flg = SEM_UNDO;
semop(semid3, &sem_c, 1);
sem_d.sem_num = 0;
sem_d.sem_op = -1;
sem_d.sem_flg = SEM_UNDO;
semop(semid4, &sem_d, 1);
printf("I am son get %s\n",ptext);
sem_d.sem_num = 0;
sem_d.sem_op = 1;
sem_d.sem_flg = SEM_UNDO;
semop(semid4, &sem_d, 1);
sem_b.sem_num = 0;
sem_b.sem_op = 1;
sem_b.sem_flg = SEM_UNDO;
semop(semid1, &sem_b, 1);}
}
}
else
{
ptext = (char*)shmat(shmid,0,0);
struct sembuf sem_b;
int product;
while(1){
sleep(random()%10);
sem_b.sem_num = 0;
sem_b.sem_op = -1;
sem_b.sem_flg = SEM_UNDO;
semop(semid1, &sem_b, 1);
sem_d.sem_num = 0;
sem_d.sem_op = -1;
sem_d.sem_flg = SEM_UNDO;
semop(semid4, &sem_d, 1);
product = random()%2;
printf("Parent put fruit %d\n",product);
sprintf(ptext,"%d",product);
if(product == 1)
{printf("orange\n");
sem_d.sem_num = 0;
sem_d.sem_op = 1;
sem_d.sem_flg = SEM_UNDO;
semop(semid4, &sem_d, 1);
sem_a.sem_num = 0;
sem_a.sem_op = 1;
sem_a.sem_flg = SEM_UNDO;
semop(semid2, &sem_a, 1);
}
else
{
printf("apple\n");
sem_d.sem_num = 0;
sem_d.sem_op = 1;
sem_d.sem_flg = SEM_UNDO;
semop(semid4, &sem_d, 1);
sem_c.sem_op = 1;
sem_c.sem_num = 0;
sem_c.sem_flg = SEM_UNDO;
semop(semid3,&sem_c,1 );
}
}
}
return 0 ;
}
边栏推荐
- 【Rust 笔记】12-闭包
- [set theory] order relation (hastu example | divisive relation hastu | inclusive relation hastu | refinement relation hastu)
- Youyou1 of xlua knapsack system
- 数据库应用技术课程设计之商城管理系统
- CLion-Toolchains are not configured Configure Disable profile问题解决
- Find the intersection of line segments
- 使用base64编码传图片
- Golang 中string和int类型相互转换
- Redis cluster series 4
- What is BFC?
猜你喜欢

Mall management system of database application technology course design

二进制转十进制,十进制转二进制

php-fpm软件的安装+openresty高速缓存搭建

Introduction to hexadecimal coding

UE4 source code reading_ Bone model and animation system_ Animation compression

Chocolate installation

单调栈-503. 下一个更大元素 II

梯度下降法求解BP神经网络的简单Demo

CLion-Toolchains are not configured Configure Disable profile问题解决

How to establish rectangular coordinate system in space
随机推荐
Redis的数据结构
Gradle's method of dynamically modifying APK package name
Huawei interview summary during the epidemic
数据库应用技术课程设计之商城管理系统
数据分析练习题
[set theory] order relation (hastu example | divisive relation hastu | inclusive relation hastu | refinement relation hastu)
Cesium for unreal quick start - simple scenario configuration
Youyou1 of xlua knapsack system
OpenGL learning notes
Mall management system of database application technology course design
One dimensional array two dimensional array (sort Max insert sort)
数据库原理期末复习
CLion-Toolchains are not configured Configure Disable profile问题解决
Un système de gestion de centre commercial pour la conception de cours de technologie d'application de base de données
[updating] wechat applet learning notes_ three
Redis data structure
GIS实战应用案例100篇(七十八)-多规合一数据库设计及数据入库
Mysql容器化(1)Docker安装MySQL
How does unity fixedupdate call at a fixed frame rate
Map的实现类的顺序性