实现生产组和消费者的机制,利用共享内存实现文件的打开和读写操作,PV操作。
来源:互联网 发布:2017天猫双11销售数据 编辑:程序博客网 时间:2024/06/03 22:39
/*shm_com.h*/#include#include #include #include #include #include #include #define SHM_BUFF_SZ 2048struct shm_buff{int pid;char buffer[SHM_BUFF_SZ];};
/*sem_com.h*/#include#include #include #include #include union semun{int val;struct semid_ds *buf;unsigned short *array;};/*信号量初始化(赋值)函数*/int init_sem(int sem_id,int init_value);/*从系统中删除信号量的函数*/int del_sem(int sem_id);/*p操作函数*/int sem_p(int sem_id);/*V操作函数*/int sem_v(int sem_id);
/*sem_com.c*/#include"sem_com.h"/*信号量初始化(赋值)函数*/int init_sem(int sem_id,int init_value){union semun sem_union;sem_union.val=init_value; /*init_value为初始值*/if(semctl(sem_id,0,SETVAL,sem_union)==-1){perror("Initialize semaphore");return -1;}return 0;}/*从系统中删除信号量的函数*/int del_sem(int sem_id){union semun sem_union;if(semctl(sem_id,0,IPC_RMID,sem_union)==-1){perror("Delete semaphore");return -1;}}/*p操作函数*/int sem_p(int sem_id){struct sembuf sem_b;sem_b.sem_num=0; /*单个信号量的编号应该为0*/sem_b.sem_op=-1; /*取值为-1,表示p操作*/sem_b.sem_flg=SEM_UNDO; /*系统自动释放将会在系统中残留的信号量*/if(semop(sem_id,&sem_b,1)==-1){perror("P operationn");return -1;}return 0;}/*V操作函数*/int sem_v(int sem_id){struct sembuf sem_b;sem_b.sem_num=0; /*单个信号量的编号应该为0*/sem_b.sem_op=1; /*取值为+1,表示v操作*/sem_b.sem_flg=SEM_UNDO; /*系统自动释放将会在系统中残留的信号量*/if(semop(sem_id,&sem_b,1)==-1){perror("V operationn");return -1;}return 0;}
/*producer.c*/#include"shm_com.h"#include"sem_com.h"#includeint ignore_signal(void){ /*忽略一些信号,以免非法突出程序*/signal(SIGINT,SIG_IGN);signal(SIGSTOP,SIG_IGN);signal(SIGQUIT,SIG_IGN);return 0;}int main(){char *shared_memory;struct shm_buff *shm_buff_inst;char buffer[BUFSIZ];int shmid,semid;/*定义信号量,用于实现访问共享内存的进程间的互斥*/ignore_signal(); /*用于防止程序非正常退出*/semid=semget(ftok(".",'a'),1,0666|IPC_CREAT);/*创建一个信号量*/init_sem(semid,1); /*初始值为1*//*创建共享内存*/shmid=shmget(ftok(".",'c'),sizeof(struct shm_buff),0666|IPC_CREAT);if(shmid==-1){perror("shmget failed");del_sem(semid);exit(1);}/*将共享内存地址映射到当前进程地址空间*/shared_memory=shmat(shmid,0,0);if(shared_memory==(void*)-1){perror("shmat");del_sem(semid);exit(1);}printf("Memory attached at %p\n",shared_memory);/*获取共享内存的映射地址*/shm_buff_inst=(struct shm_buff *)shared_memory;do{sem_p(semid);printf("Emter some text to the shared memory(enter 'quit' to exit):");/*向共享内存写入数据*/if(fgets(shm_buff_inst->buffer,SHM_BUFF_SZ,stdin)==NULL){perror("fgets");sem_v(semid);break;}shm_buff_inst->pid=getpid();sem_v(semid);}while(strncmp(shm_buff_inst->buffer,"quit",4)!=0);/*删除信号量*/del_sem(semid);/*删除共享内存到当前进程地址空间中的映射*/if(shmdt(shared_memory)==-1){perror("shmdt");exit(1);}exit(0);}
/* customer.c */#include"shm_com.h"#include"sem_com.h"int main(){char *shared_memory=NULL;struct shm_buff *shm_buff_inst;int shmid,semid;/*获得信号量*/semid=semget(ftok(".",'a'),1,0666);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(shmid,0,0);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->pid,shm_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->buffer,0,SHM_BUFF_SZ);sem_v(semid);}while(1);/*删除共享内存到当前进程地址空间中的映射*/if(shmdt(shared_memory)==-1){perror("shmdt");exit(1);}/*删除共享内存*/if(shmctl(shmid,IPC_RMID,NULL)==-1){perror("shmctl");exit(1);}exit(0);}
阅读全文
0 0
- 实现生产组和消费者的机制,利用共享内存实现文件的打开和读写操作,PV操作。
- Linux 用pv操作和共享内存实现生产者与消费者机制
- Linux 用pv操作和共享内存实现生产者与消费者机制
- linux 使用共享内存实现生产者消费者,PV操作。
- 生产者和消费者(PV操作、共享内存)
- 用共享内存实现生产者和消费者的机制
- C++实现shapefile文件的读写和基本功能的操作
- Operating System-进程/线程内部通信-信号量、PV操作的实现和应用(解决哲学家进餐和生产者消费者问题)
- 进程同步-生产者和消费者(PV操作实现——java多线程模拟)
- java中notify和wait实现操作系统pv操作(单一生产者消费者)
- 用共享内存实现生产者和消费者
- PV操作和信号量机制实现进程同步(对多个临界资源的互斥访问)
- 用C语言实现--生产者与消费者的问题(PV操作)
- 关于网宿厦门研发中心笔试的一道PV操作题:利用java中的多线程实现生产者与消费者的同步问题
- 利用信号量和PV操作实现进程互斥和同步问题
- 关于操作系统中pv操作的实现
- 信号量实现线程之间的PV操作
- 共享内存和操作共享内存几个函数的用法
- 有限状态机求解字符串匹配问题
- 笔记---上传下载代码
- maven导入已有jar包
- 读书笔记总结(链接)
- java线程中断的理解
- 实现生产组和消费者的机制,利用共享内存实现文件的打开和读写操作,PV操作。
- 线程池详解
- 【LeetCode】442. Find All Duplicates in an Array
- 主流的 骨骼蒙皮动画 顶点变换方式的一个细节的思考
- QTransform Qt坐标转换
- H5---面试题八
- mAP,精确率,召回率,查重率,查准率的定义
- Linux操作系统上安装Java-学习笔记
- 68. Text Justification