当前位置:网站首页>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 ;
}
边栏推荐
- C#课程设计之学生教务管理系统
- Creation and content of mapnode -- osgearth rendering engine series (2)
- P1596 [USACO10OCT]Lake Counting S
- Base64编码简介
- Unity Editor Extension - Outline
- Swagger document configuration
- Unity editor expansion - draw lines
- One dimensional array two dimensional array (sort Max insert sort)
- Chocolate installation
- Dealing with duplicate data in Excel with xlwings
猜你喜欢
Unity Editor Extension - drag and drop
Basic operation and process control 2
Xlua task list youyou
Student educational administration management system of C # curriculum design
[cloud native] introduction and use of feign of microservices
UE4 source code reading_ Bone model and animation system_ Animation process
图像处理8-CNN图像分类
jupyter远程服务器配置以及服务器开机自启
Dealing with duplicate data in Excel with xlwings
基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
随机推荐
[set theory] order relation (hastu example | divisive relation hastu | inclusive relation hastu | refinement relation hastu)
Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
php-fpm软件的安装+openresty高速缓存搭建
[audio and video] ijkplayer error code
P1596 [USACO10OCT]Lake Counting S
P1596 [USACO10OCT]Lake Counting S
What is BFC?
Sequence of map implementation classes
Explain sizeof, strlen, pointer, array and other combination questions in detail
Kwai 20200412 recruitment
Creation of osgearth earth files to the earth ------ osgearth rendering engine series (1)
了解小程序的笔记 2022/7/3
Three characteristics
Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template
数据分析练习题
Gradle's method of dynamically modifying APK package name
Unity change default editor
Get to know unity2 for the first time
Chocolate installation
Unity editor expansion - window, sub window, menu, right-click menu (context menu)