91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 2KB
    文件類型: .gz
    金幣: 1
    下載: 0 次
    發布日期: 2021-05-25
  • 語言: 其他
  • 標簽: 共享內存??

資源簡介

共享內存的實驗,詳細看我的博客“Linux進程間通信(十)---綜合實驗之共享內存實驗”

資源截圖

代碼片段和文件信息

/*?customer.c?*/

#include“shm_com.h“
#include“sem_com.h“

int?main()
{
char?*shared_memory=NULL;
struct?shm_buff?*shm_buff_inst;
int?shmidsemid;

/*獲得信號量*/
semid=semget(ftok(“.“‘a‘)10666);
if(semid==-1)
{
perror(“Producer?isn‘t?exist“);
exit(1);
}
/*獲得共享內存*/
shmid=shmget(ftok(“.“‘c‘)sizeof(struct?shm_buff)0666|IPC_CREAT);
if(shmid==-1)
{
perror(“shmget“);
exit(1);
}

/*將共享內存地址映射到當前進程地址空間*/
shared_memory=shmat(shmid00);
if(shared_memory==(void?*)-1)
{
perror(“shmat“);
exit(1);
}
printf(“Memory?attached?at?%p\n“shared_memory);

/*獲得共享內存的映射地址*/

shm_buff_inst=(struct?shm_buff?*)shared_memory;
do
{
sem_p(semid);
printf(“Shared?memory?was?written?by?process?%d?:?%s“shm_buff_inst->pidshm_buff_inst->buffer);
if(strncmp(shm_buff_inst->buffer“quit“4)==0)
{
perror(“strncmp“);
exit(1);
}
shm_buff_inst->pid=0;
memset(shm_buff_inst->buffer0SHM_BUFF_SZ);
sem_v(semid);
}while(1);

/*刪除共享內存到當前進程地址空間中的映射*/
if(shmdt(shared_memory)==-1)
{
perror(“shmdt“);
exit(1);
}

/*刪除共享內存*/
if(shmctl(shmidIPC_RMIDNULL)==-1)
{
perror(“shmctl“);
exit(1);
}
exit(0);
}

評論

共有 條評論