<转>linux操作系统编程——共享内存读写(采用信号量进行同步互斥)
http://blog.csdn.net/yanghaoran321/article/details/7872722
程序要求:
创建一个写端和一个读端,写端写入数据后读端才开始读,读端读完数据后,写端才可以开始写,这样的同步采用信号机制实现,并且写端与读端打开顺序不同也能实现功能;
程序如下:
(1)write.c(写端)
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <errno.h>
- #include "sem.h"
- typedef struct
- {
- char buf[1024];
- }memory;
- int main(int argc, const char *argv[])
- {
- key_t key;
- memory *p = NULL;
- int shmid;
- int create_flag = 0;
- int sem_id;
- if ((key = ftok(".", 'a')) < 0)
- {
- perror("failed to get key");
- exit(-1);
- }
- if ((sem_id = semget(key, 1, 0666 | IPC_CREAT | IPC_EXCL)) < 0)
- {
- if (errno == EEXIST)
- {
- if ((sem_id = semget(key, 1, 0666)) < 0)
- {
- perror("failed to semget");
- exit(-1);
- }
- }
- }
- init_sem(sem_id, 0);
- if ((shmid = shmget(key, sizeof(memory), 0666 | IPC_CREAT | IPC_EXCL)) < 0)
- {
- if (errno == EEXIST)
- {
- if ((shmid = shmget(key, sizeof(memory), 0666)) < 0)
- {
- perror("failed to shmget memory");
- exit(-1);
- }
- }
- else
- {
- perror("failed to shmget");
- exit(-1);
- }
- }
- else
- create_flag = 1;
- if ((p = shmat(shmid, NULL, 0)) == (void *)(-1))
- {
- perror("failed to shmat memory");
- exit(-1);
- }
- while(1)
- {
- printf(">");
- fgets(p->buf, sizeof(p->buf), stdin);
- p->buf[strlen(p->buf) - 1] = 0;
- sem_v(sem_id);
- if (strncmp(p->buf, "quit", 4) == 0)
- break;
- }
- if (create_flag == 1)
- {
- if (shmdt(p) < 0)
- {
- perror("failed to shmdt memory");
- exit(-1);
- }
- if (shmctl(shmid, IPC_RMID, NULL) == -1)
- {
- perror("failed to delete share memory");
- exit(-1);
- }
- delete_sem(sem_id);
- }
- return 0;
- }
(2)read.c(读端)
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <errno.h>
- #include "sem.h"
- typedef struct
- {
- char buf[1024];
- }memory;
- int main(int argc, const char *argv[])
- {
- key_t key;
- int shmid;
- memory *p = NULL;
- int create_flag = 0;
- int sem_id;
- if ((key = ftok(".", 'a')) < 0)
- {
- perror("failed to get key");
- exit(-1);
- }
- if ((sem_id = semget(key, 1, 0666 | IPC_CREAT | IPC_EXCL)) < 0)
- {
- if (errno == EEXIST)
- {
- if ((sem_id = semget(key, 1, 0666)) < 0)
- {
- perror("failed to semget");
- exit(-1);
- }
- }
- }
- init_sem(sem_id, 0);
- if ((shmid = shmget(key, sizeof(memory), 0666 | IPC_CREAT | IPC_EXCL)) < 0)
- {
- if (errno == EEXIST)
- {
- if ((shmid = shmget(key, sizeof(memory), 0666)) < 0)
- {
- perror("failed to create share memory");
- exit(-1);
- }
- }
- else
- {
- perror("failed to shmget");
- exit(-1);
- }
- }
- else
- create_flag = 1;
- if ((p = shmat(shmid, NULL, 0)) == (void *)(-1))
- {
- perror("failed to shmat");
- exit(-1);
- }
- while(1)
- {
- sem_p(sem_id);
- if (strncmp(p->buf, "quit", 4) == 0)
- break;
- printf("recv: %s\n", p->buf);
- }
- if (create_flag == 1)
- {
- if (shmdt(p) < 0)
- {
- perror("failed to shmdt");
- exit(-1);
- }
- if (shmctl(shmid, IPC_RMID, NULL) == -1)
- {
- perror("failed to delete share memory");
- exit(-1);
- }
- delete_sem(sem_id);
- }
- return 0;
- }
关于封装信号量函数的头文件:
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/sem.h>
- #include <unistd.h>
- void init_sem(int , int );
- void delete_sem(int );
- void sem_p(int );
- void sem_v(int );
- union semun
- {
- int val;
- struct semid_ds *buf;
- unsigned short *array;
- };
- void init_sem(int sem_id, int init_value)
- {
- union semun sem_union;
- sem_union.val = init_value;
- if (semctl(sem_id, 0, SETVAL, sem_union) < 0)
- {
- perror("failed to init_sem");
- exit(-1);
- }
- return ;
- }
- void delete_sem(int sem_id)
- {
- union semun sem_union;
- if (semctl(sem_id, 0, IPC_RMID, sem_union) < 0)
- {
- perror("failed to delete_sem");
- exit(-1);
- }
- return ;
- }
- void sem_p(int sem_id)
- {
- struct sembuf sem_b;
- sem_b.sem_num = 0;
- sem_b.sem_op = -1;
- sem_b.sem_flg = SEM_UNDO;
- if (semop(sem_id, &sem_b, 1) < 0)
- {
- perror("failed to sem_p");
- exit(-1);
- }
- return;
- }
- void sem_v(int sem_id)
- {
- struct sembuf sem_b;
- sem_b.sem_num = 0;
- sem_b.sem_op = 1;
- sem_b.sem_flg = SEM_UNDO;
- if (semop(sem_id, &sem_b, 1) < 0)
- {
- perror("failed to sem_v");
- exit(-1);
- }
- return ;
- }
<转>linux操作系统编程——共享内存读写(采用信号量进行同步互斥)的更多相关文章
- Linux系统编程 —共享内存之mmap
共享内存概念 共享内存是通信效率最高的IPC方式,因为进程可以直接读写内存,而无需进行数据的拷备.但是它没有自带同步机制,需要配合信号量等方式来进行同步. 共享内存被创建以后,同一块物理内存被映射到了 ...
- Linux环境进程间通信: 共享内存
Linux环境进程间通信: 共享内存 第一部分 共享内存可以说是最有用的进程间通信方式,也是最快的IPC形式.两个不同进程A.B共享内存的意思是,同一块物理内存被映射到进程A.B各自的进程地址空间.进 ...
- Linux IPC之共享内存C 事例
Linux IPC之共享内存 标签: linuxrandomnull工作 2011-08-25 11:52 4123人阅读 评论(0) 收藏 举报 分类: Linux(3) 读书札记(3) 版权 ...
- Linux进程间通信—使用共享内存
Linux进程间通信-使用共享内存 转自: https://blog.csdn.net/ljianhui/article/details/10253345 下面将讲解进程间通信的另一种方式,使用共享内 ...
- VC++ 共享内存读写操作
此解决方案含两个工程文件,一个是写操作工程文件,即把任意字符串写入创建的共享内存里,另外一个读操作工程文件,则是读取共享内存里的数据,从而实现了进程之间的共享内存读写操作. 源码下载
- Linux 系统编程 学习:11-线程:线程同步
Linux 系统编程 学习:11-线程:线程同步 背景 上一讲 我们介绍了线程的属性 有关设置.这一讲我们来看线程之间是如何同步的. 额外安装有关的man手册: sudo apt-get instal ...
- Linux IPC POSIX 共享内存
模型 #include <unistd.h> //for fstat() #include <sys/types.h> //for fstat() #include <s ...
- linux进程间通信之共享内存篇
本文是对http://www.cnblogs.com/andtt/articles/2136279.html中共享内存(上)的进一步阐释说说明 1 共享内存的实现原理 共享内存是linux进程间通讯的 ...
- Linux下进程间通信--共享内存:最快的进程间通信方式
共享内存: 一.概念: 共享内存可以说是最有用的进程间通信方式,也是最快的IPC形式.两个不同进程A.B共享内存的意思是,同一块物理内存被映射到进程A.B各自的进程地址空间. 进程A可以即时看到进程B ...
随机推荐
- 字符串处理scanf("%d%*c",&n);
"*"表示该输入项读入后不赋予任何变量,即跳过该输入值.这在减小内存开支上面还是有一点用处的,不需要的字符直接跳过,免得申请没用的变量空间 你的例子中的%*c的作用是读入'\n', ...
- ubutun Sogou输入法安装
http://jingyan.baidu.com/article/ad310e80ae6d971849f49ed3.html 文章来自以上链接 安装搜狗输入法 下载搜狗http://pinyin.so ...
- Oracle SQL七次提速技巧
以下SQL执行时间按序号递减. 1,动态SQL,没有绑定变量,每次执行都做硬解析操作,占用较大的共享池空间,若共享池空间不足,会导致其他SQL语句的解析信息被挤出共享池. create or repl ...
- linux下如何添加一个用户并且让用户获得root权限【转载】
原文:http://www.cnblogs.com/johnw/p/5499442.html 1.添加用户,首先用adduser命令添加一个普通用户,命令如下: #adduser tommy //添加 ...
- @RequestParam和@RequestBody的区别-------springMVC
https://blog.csdn.net/qq_27093465/article/details/50519444 @RequestParam 1,用来处理Content-Type: 为 appli ...
- 解决EditPlus的默认编码方式有关问题(转)
http://blog.csdn.net/hzhsan/article/details/7911660 最近在使用英文版的Editplus写代码的时候,发现中文字符在调试过程中都变成了乱码, 发现是E ...
- redis+php实现微博功能(一)
(一).微博功能概况 微博用户账号注册 微博用户登录 微博发布 添加微博好友(粉丝) 微博推送 微博冷数据写入mysql数据库 (二).redis数据结构设计 这节分享微博用户注册与登录:我们完全采用 ...
- python is 和 == 的区别
一.is 和 == 的区别 == 比较 比较的俩边的值 is 比较 比较的是内存地址 id() 二.小数据池 数字小数据池的范围 -5 ~ 256 字符串中如果有特殊字符他们的内存地址就不一样 字符串 ...
- json工具性能比较:json-lib和jackson进行Java对象到json字符串序列化[转]
网上查找“java json”,发现大家使用最多的还是json-lib来进行java对象的序列化成json对象和反序列化成java对象的操作.但是之前在网上也看到过一往篇关于json序列化性能比较的文 ...
- Java实现HTML转换为PDF的常见方法
最近在自己的项目中需要动态生成融资单合同,这里需要把对应的html转换为对应的pdf融资合同.因此需要通过Java实现将HTML转PDF.自己之前没有接触过这一块的东西,所以上网查了一下,网上有很多的 ...