当前位置:网站首页>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 ;
}
边栏推荐
- Gradle's method of dynamically modifying APK package name
- Encoding and decoding of golang URL
- UE4 plug in development
- Mall management system of database application technology course design
- Osgearth topographic shading map drawing
- [set theory] order relation (the relation between elements of partial order set | comparable | strictly less than | covering | Haas diagram)
- Unity multi open script
- LinkList
- How to establish rectangular coordinate system in space
- CLion-Toolchains are not configured Configure Disable profile问题解决
猜你喜欢
Detailed explanation of all transfer function (activation function) formulas of MATLAB neural network
图像处理8-CNN图像分类
Base64 and base64url
Unity interactive water ripple post-treatment
Un système de gestion de centre commercial pour la conception de cours de technologie d'application de base de données
Easy touch plug-in
[cloud native] introduction and use of feign of microservices
C course design employee information management system
Base64编码简介
Markdown learning
随机推荐
UE4 source code reading_ Bone model and animation system_ Animation process
单调栈-42. 接雨水
Unity editor expansion - window, sub window, menu, right-click menu (context menu)
[set theory] order relation (hastu example | divisive relation hastu | inclusive relation hastu | refinement relation hastu)
Location of package cache downloaded by unity packagemanager
Detailed explanation of all transfer function (activation function) formulas of MATLAB neural network
[audio and video] ijkplayer error code
Mysql容器化(1)Docker安装MySQL
Introduction to hexadecimal coding
Golang time format sorting
One dimensional array two dimensional array (sort Max insert sort)
Downward compatibility and upward compatibility
[cloud native] introduction and use of feign of microservices
UE4 plug in development
Get to know unity2 for the first time
String class
C language - Introduction - essence Edition - take you into programming (I)
数据库原理期末复习
Golang json格式和结构体相互转换
CLion-Toolchains are not configured Configure Disable profile问题解决