当前位置:网站首页>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
边栏推荐
- Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)
- About Wireshark's unsuccessful installation of npcap
- Golang中删除字符串的最后一个字符
- Simple demo of solving BP neural network by gradient descent method
- swagger文档配置
- php-fpm软件的安装+openresty高速缓存搭建
- Encoding and decoding of golang URL
- Clion toolchains are not configured configure disable profile problem solving
- Flex flexible box layout
- Redis data structure
猜你喜欢

Data analysis exercises

单调栈-503. 下一个更大元素 II

了解小程序的笔记 2022/7/3

UE4 source code reading_ Mobile synchronization

Unity editor expansion - controls, layouts

About Wireshark's unsuccessful installation of npcap

Constraintlayout's constraintset dynamically modifies constraints

Un système de gestion de centre commercial pour la conception de cours de technologie d'application de base de données

【更新中】微信小程序学习笔记_3

jupyter远程服务器配置以及服务器开机自启
随机推荐
Detailed explanation of all transfer function (activation function) formulas of MATLAB neural network
VIM learning notes from introduction to silk skating
Visual Studio (VS) shortcut keys
Solution détaillée de toutes les formules de fonction de transfert (fonction d'activation) du réseau neuronal MATLAB
Advanced OSG collision detection
About Wireshark's unsuccessful installation of npcap
[audio and video] ijkplayer error code
UE4 source code reading_ Bone model and animation system_ Animation node
Unity change default editor
单调栈-84. 柱状图中最大的矩形
Un système de gestion de centre commercial pour la conception de cours de technologie d'application de base de données
ArrayList
Chocolate installation
Base64和Base64URL
C#课程设计之学生教务管理系统
MySQL containerization (1) docker installation MySQL
GIS实战应用案例100篇(七十八)-多规合一数据库设计及数据入库
Unity interactive water ripple post-treatment
Golang string segmentation, substitution and interception
Abstract classes and interfaces