当前位置:网站首页>producer consumer problem
producer consumer problem
2022-07-03 08:31:00 【Spring and autumn sword armor Li Chungang】
This program has a producer process , There are two consumer processes , Producer production 1-20 Of 20 Number , Two consumers retrieve data from shared memory
Code implementation :
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include <sys/shm.h>
#include<sys/wait.h>
#include<stdio.h>
#include <sys/sem.h>
int i;
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 semid4 = semget((key_t)1110,1,0640|IPC_CREAT);
union semun sem_union4;
sem_union4.val = 1;
semctl(semid4, 0, SETVAL,sem_union4);
struct sembuf sem_d ;
int shmid = shmget(IPC_PRIVATE , 1024, 0640|IPC_CREAT);
if(shmid <0)
{
printf("error!");
}
int *ptext = 0 ;
ptext = (int*)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 = (int *)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 consumer1 get %d\n",ptext[i]);
i++;
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 = (int *)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 consumer2 get %d\n",ptext[i]);
i++;
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 = (int *)shmat(shmid,0,0);
struct sembuf sem_b;
int product;
while(1){
sleep(random()%10);
struct sembuf sem_b;
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()%100;
ptext[i] = product;
printf("position is %d\n",i++);
printf("Producer produce %d\n ",product);
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);
}
}
return 0 ;
}
// It just presents the core idea of the code , Didn't handle the details
边栏推荐
- Redis cluster series 4
- matlab神经网络所有传递函数(激活函数)公式详解
- Unity change default editor
- Osganimation library parsing
- Encoding and decoding of golang URL
- CLion-Toolchains are not configured Configure Disable profile问题解决
- Storage of data
- Conversion between string and int types in golang
- Golang中删除字符串的最后一个字符
- Youyou1 of xlua knapsack system
猜你喜欢

图像处理8-CNN图像分类

How to establish rectangular coordinate system in space

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

Kwai 20200412 recruitment

Notes on understanding applets 2022/7/3

Base64编码简介

Graphics_ Games101/202 learning notes

Storage of data

Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template

【云原生】微服务之Feign的介绍与使用
随机推荐
Golang time format sorting
Development material set
Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
Conversion between string and int types in golang
Go resolve ID card
UE4 source code reading_ Bone model and animation system_ Animation process
C course design employee information management system
Chocolate installation
详解sizeof、strlen、指针和数组等组合题
【更新中】微信小程序学习笔记_3
Mysql容器化(1)Docker安装MySQL
Osgearth topographic shading map drawing
100 GIS practical application cases (78) - Multi compliance database design and data warehousing
[cloud native] introduction and use of feign of microservices
Classes and objects
Base64 and base64url
Basic operation and process control
go 解析身份证
Unity interactive water ripple post-treatment
Basic operation and process control 2