在上一篇共享内存的基础上加上pv操作,也就是 A 进程去写 的时候要加上P 写完了 加个 V
pvread.cpp
#include "public.h"
union semun
{
int val;
struct semid_ds *buf;
unsigned short *array;
struct seminfo *__buf;
};
int main()
{
SNode student;
char buffer[20];
pid_t pid=0;
int i=1;
int ret=0;
//嵌入式信号量代码
union semun mysemun;
struct sembuf mysembuf;
int sem_no =0;
//创建信号
sem_no=semget(SEM_KEY,SEM_NUMBER,SEM_FLAG);
if (sem_no==-1)
{
printf("fail to semget SEM_KEY=%d \n",SEM_KEY);
exit(1);
}
//进行p操作准备执行关键代码
mysembuf.sem_num=SEM_INDEX;
mysembuf.sem_op=-1;
mysembuf.sem_flg=SEM_UNDO;
ret=semop(sem_no,&mysembuf,1);
if (ret==-1)
{
printf("fail to call semop p op\n");
}
mem_no=shmget(MEM_KEY,10*MEM_SIZE,MEM_FLAG);
//共享内存的周地址映射到本进程
pstudent=(StuNodes)shmat(mem_no,NULL,0);
//保留开始的位置方便后续读取
pstudent_first=pstudent;
pstudent_first2=pstudent;
//共享内存中读取数据
printf("开始从共享内存中读取数据。。。\n");
for (i=0;i<10;i++)
{
printf("student.no = %d student.username %s \n",pstudent_first->no,pstudent_first2->username);
pstudent_first++;
sleep(1);
}
//卸载共享内存
if (shmdt(pstudent_first2)==-1)
{
printf("fail to dattach share memory\n");
}
//printf("press any key to remove share momory...\n");
//getchar();
//删除共享内存
if (shmctl(mem_no,IPC_RMID,0)==-1)
{
printf("fail to IPC_RMID\n");
}
//关键代码执行完了之后,做一个v操作让别的进程的p
//能够进去
mysembuf.sem_num=SEM_INDEX;
mysembuf.sem_op=1;
mysembuf.sem_flg=SEM_UNDO;
ret=semop(sem_no,&mysembuf,1);
if (ret==-1)
{
printf("fail to semop v op\n");
}
printf("press anykey to exit \n");
getchar();
//删除sem_no
semctl(sem_no,SEM_INDEX,IPC_RMID);
return 0;
}