当前位置:网站首页>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 ;
}
边栏推荐
- GIS实战应用案例100篇(七十八)-多规合一数据库设计及数据入库
- Encoding and decoding of golang URL
- Compilation error: "not in executable format: file format not recognized"“
- Classes and objects
- Unity editor expansion - scrolling list
- Golang string segmentation, substitution and interception
- 【Rust 笔记】13-迭代器(上)
- Golang 字符串分割,替换和截取
- How does unity fixedupdate call at a fixed frame rate
- Cesium for unreal quick start - simple scenario configuration
猜你喜欢

Solution détaillée de toutes les formules de fonction de transfert (fonction d'activation) du réseau neuronal MATLAB

Some understandings of 3dfiles

C course design employee information management system

Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)

matlab神经网络所有传递函数(激活函数)公式详解

数据分析练习题

Storage of data

UE4 source code reading_ Bone model and animation system_ Animation process

Installation of PHP FPM software +openresty cache construction

KunlunBase MeetUP 等您来!
随机推荐
VIM learning notes from introduction to silk skating
Unity Editor Extension - drag and drop
[set theory] order relation (the relation between elements of partial order set | comparable | strictly less than | covering | Haas diagram)
Advanced OSG collision detection
基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
Introduction to Base64 coding
【Rust 笔记】13-迭代器(上)
图像处理8-CNN图像分类
Notes on understanding applets 2022/7/3
Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
Osgearth north arrow display
796 · 开锁
Development experience and experience
【更新中】微信小程序学习笔记_3
GIS实战应用案例100篇(七十八)-多规合一数据库设计及数据入库
Talking about: is the HashSet set ordered or disordered /hashset set unique, why can we store elements with the same content
Map的实现类的顺序性
Redis的数据结构
KunlunBase MeetUP 等您来!
Student educational administration management system of C # curriculum design