当前位置:网站首页>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
边栏推荐
- Minimap plug-in
- UE4 source code reading_ Bone model and animation system_ Animation compression
- UE4 source code reading_ Bone model and animation system_ Animation process
- Xlua task list youyou
- Why can void * be a general pointer
- Basic operation and process control 2
- Some understandings of 3dfiles
- [cloud native] introduction and use of feign of microservices
- String class
- Graphics_ Learnopongl learning notes
猜你喜欢
Graphics_ Learnopongl learning notes
Graphics_ Games101/202 learning notes
Unity interactive water ripple post-treatment
Jupyter remote server configuration and server startup
C#课程设计之员工信息管理系统
Xlua task list youyou
Redis的数据结构
How to establish rectangular coordinate system in space
Notes on understanding applets 2022/7/3
Three characteristics
随机推荐
Golang time format sorting
Redis data structure
Golang 中string和int类型相互转换
Data analysis exercises
go 解析身份证
How does unity fixedupdate call at a fixed frame rate
Swagger document configuration
单调栈-42. 接雨水
UE4 source code reading_ Bone model and animation system_ Animation node
Pit & ADB wireless debugging of vivo real machine debugging
Downward compatibility and upward compatibility
UE4 source code reading_ Mobile synchronization
Kunlunbase meetup is waiting for you!
Intersectionpicker in osgearth
Kwai 20200412 recruitment
Exe file running window embedding QT window
Simply start with the essence and principle of SOM neural network
Encoding and decoding of golang URL
Notes on understanding applets 2022/7/3
About the problem that the editor and the white screen of the login interface cannot be found after the location of unityhub is changed