当前位置:网站首页>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
边栏推荐
- Unity learning notes
- Vscode, idea, VIM development tool shortcut keys
- Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)
- Base64和Base64URL
- Golang 时间格式整理
- String class
- Youyou1 of xlua knapsack system
- Cesium service deployment, and import and display local 3dfiles data
- KunlunBase MeetUP 等您来!
- Sequence of map implementation classes
猜你喜欢

Xlua task list youyou

Storage of data

數據庫應用技術課程設計之商城管理系統

Redis的数据结构

Chocolate installation

OpenGL learning notes

Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation

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

Installation of PHP FPM software +openresty cache construction

Constraintlayout's constraintset dynamically modifies constraints
随机推荐
Abstract classes and interfaces
Golang time format sorting
String class
go 解析身份证
Golang 中string和int类型相互转换
Easy touch plug-in
使用base64编码传图片
C#课程设计之员工信息管理系统
数据分析练习题
Jupyter remote server configuration and server startup
Solution détaillée de toutes les formules de fonction de transfert (fonction d'activation) du réseau neuronal MATLAB
數據庫應用技術課程設計之商城管理系統
Un système de gestion de centre commercial pour la conception de cours de technologie d'application de base de données
数据库原理期末复习
matlab神经网络所有传递函数(激活函数)公式详解
Minimap plug-in
matlab神經網絡所有傳遞函數(激活函數)公式詳解
Unity multi open script
Pit & ADB wireless debugging of vivo real machine debugging
Animation_ IK overview