当前位置:网站首页>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 ;
}
边栏推荐
- Unity editor expansion - window, sub window, menu, right-click menu (context menu)
- Redis cluster series 4
- MySQL 8
- Kunlunbase meetup is waiting for you!
- LinkList
- 简易入手《SOM神经网络》的本质与原理
- Data analysis exercises
- UE4 source code reading_ Mobile synchronization
- 数据分析练习题
- UE4 source code reading_ Bone model and animation system_ Animation compression
猜你喜欢
Redis data structure
[set theory] order relation (the relation between elements of partial order set | comparable | strictly less than | covering | Haas diagram)
Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template
Xlua task list youyou
Mall management system of database application technology course design
二进制转十进制,十进制转二进制
Solution détaillée de toutes les formules de fonction de transfert (fonction d'activation) du réseau neuronal MATLAB
[cloud native] introduction and use of feign of microservices
How to establish rectangular coordinate system in space
matlab神经网络所有传递函数(激活函数)公式详解
随机推荐
Talking about: is the HashSet set ordered or disordered /hashset set unique, why can we store elements with the same content
Get to know unity2 for the first time
单调栈-42. 接雨水
swagger文档配置
Redis的数据结构
Base64和Base64URL
UE4 plug in development
GIS实战应用案例100篇(七十八)-多规合一数据库设计及数据入库
Delete the last character of the string in golang
Graphics_ Learnopongl learning notes
[public key cryptography] ECC elliptic cryptosystem (implementing ElGamal encryption method)
Unity editor expansion - controls, layouts
Dotween plug-in
Osgearth target selection
[audio and video] ijkplayer error code
Find the intersection of line segments
【Rust 笔记】13-迭代器(上)
Base64 and base64url
Gradle's method of dynamically modifying APK package name
What is BFC?