编码环境如下
系统环境:linux
信号量:使用Linux操作系统的SystemV信号量
生产者代码如下
#include <iostream>
#include <sys/sem.h>
#include <sys/shm.h>
#include <string.h>
#define SEM_KEY 0x5678
#define SHM_KEY 0xABCD
#define SHM_SIZE 1024
union semun{
int val;
struct semid_ds* buf;
unsigned short *array;
};
int main()
{
key_t sem_key = SEM_KEY;
key_t shm_key = SHM_KEY;
char* shm_ptr = NULL;
int sem_id = 0;
int shm_id = 0;
//sem create
sem_id = semget(sem_key,1,IPC_CREAT |0666);
if(-1 == sem_id)
{
printf("semget error\n");
return -1;
}
//sem init
union semun arg;
arg.val = 1;
if(semctl(sem_id,0,SETVAL,arg) == -1)
{
printf("semctl error\n");
return -1;
}
//create shm
shm_id = shmget(shm_key,SHM_SIZE,IPC_CREAT|0666);
if(-1 == shm_id)
{
printf("shmget error\n");
return -1;