<转>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 ...
随机推荐
- 《DSP using MATLAB》示例 Example 9.4
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- Appium+python (3) 异常处理
有时候定位时会发现无法定位到具体的元素,右侧元素定位处只告诉你这是一个网页视图: 点击里面的具体元素是无法选中的,船长的做法是回到App里点一下元素,再返回要定位的页面,重新点一下Device Scr ...
- nginx 获取请求头,URL参数
获取url参数 在 ngx_lua 中访问 Nginx 内置变量 ngx.var.arg_PARAMETER 即可获得GET参数PARAMETER的内容. 在 nginx配置中,通过$arg_PARA ...
- anycast简单总结
一针见血,言简意赅的总结 bgp+anycast就是不同服务器用了相同的ip地址 anycast 技术特点 bgp+anycast就是多个主机使用相同ip地址的一种技术,当报文发给该地址时,根据路由协 ...
- cockpit 使用(集成docker && k8s 管理)
1. yum 安装 sudo yum install cockpit 2. 允许启动 sudo systemctl enable --now cockpit.socket 3. 可选的插件 cockp ...
- Cucumber 使用例子
1. junit 配置 @RunWith(Cucumber.class) @CucumberOptions(format ={"pretty","html:target/ ...
- 学习 Git 玩转 GitHub
原文地址:学习 Git 玩转 GitHub 博客地址:http://www.extlight.com 一.基本了解 1.1 什么是版本控制系统 版本控制系统是一种记录一个或若干个文件内容变化,以便将来 ...
- apache编译参数详解
常用编译参数: ./configure //配置源代码树–prefix=/usr/local/apache //体系无关文件的顶级安装目录PREFIX ,也就Apache的安装目录.–e ...
- 【备忘】mysql主从设置
主(master)192.168.1.10机器设置: [root@vm-vagrant mysql]# vi my.cnf [mysqld]节点下添加以下配置server-id=1log-bin=my ...
- erlang的websocket例子
创建工程 rebar-creator create-app websocket_demo 文件列表 route_helper.erl -module(route_helper). -export([g ...