linux:共享内存
2015-08-24 15:28
253 查看
#include <sys/ipc.h> #include <sys/shm.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; struct shared_use_st { int flags; char buff[1000]; }; int main() { void *shared_memory = NULL; int shmid = shmget((key_t)1234,sizeof(shared_use_st),0666|IPC_CREAT); if(shmid==-1) { fprintf(stderr,"shmget failed\n"); exit(0); } shared_memory = shmat(shmid,(void *)0,0); if(shared_memory==(void *)-1) { printf("Error"); exit(0); } printf("Memory attached at %x\n",(int)shared_memory); shared_use_st *shared_buff = (shared_use_st*)shared_memory; shared_buff->flags = 0; if(fork()==0) { while(1) { while(shared_buff->flags==0) { strncpy(shared_buff->buff,"liuhuiyan",10); shared_buff->flags = 1; sleep(1); } } } else { while(1) { while(shared_buff->flags==1) { char data_buff[100]; strcpy(data_buff,shared_buff->buff); cout<<data_buff<<endl; shared_buff->flags=0; } } } shmdt(shared_memory); return 0; }
相关文章推荐
- [转]CentOS_yum的详细使用方法
- 读书笔记之linux/unix系统编程手册(24)
- linux下权限修改命令及方法
- PostgreSQL 在centos 7下的安装配置
- linux 防火墙
- Linux内核动态加载模块
- 对Linux堆内存释放的总结
- snprintf 在 linux 和 windows 平台上的差别
- Erlang安装,Linux、Windows
- Ubuntu学习之Linux桌面系统
- Linux expect工具简单示例
- 主机与虚拟机无法互相ping通
- Linux中的一些查询指令
- 如何制作LINUX的patch文件及如何打patch
- Linux命令初识一
- 搭建zoneminder筆記
- 记一次Linux服务器上查杀木马经历
- Linux系统、版本、CPU、内存查看、硬盘空间
- linux连接状态查询
- Linux下常用开发工具