linux 共享内存
来源:互联网 发布:js实现隐藏div 编辑:程序博客网 时间:2024/06/07 13:03
/*
共享内存的写程序
1.首先调用ftok()函数将一个路径名转化为key
2.调用shmget();函数,来返回一个共享内存地址
3.调用shmat()函数映射共享内存到本进程内存空间
4.调用shmdt()函数删除共享内存.
*/
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<sys/stat.h>
#include<fcntl.h>
#define SIZE 4096
typedef struct
{
char name[4];
int age;
}people;
int main(int argc,char **argv)
{
int shm_id,i,fd;
key_t key;
char temp;
people *p_map;
char *name="/home/program/shm/shm.txt";
if((fd=open("/home/program/shm/shm.txt",O_RDWR|O_CREAT))<0)//保证文件的存在,否则ftok()会出错
{
perror("open");
exit(1);
}
key=ftok(name,0);
if(key==-1)
{
perror("ftok error");
exit(1);
}
shm_id=shmget(key,SIZE,IPC_CREAT);
if(shm_id==-1)
{
perror("shmget");
exit(1);
}
p_map=(people *)shmat(shm_id,NULL,0);
temp='a';
for(i=0;i<10;i++)
{
memcpy((*(p_map+i)).name,&temp,1);
(*(p_map+i)).age=20+i;
temp+=1;
}
if(shmdt(p_map)==-1)
perror("detach error");
return 0;
}
/*程序 read_shm.c*/
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#define SIZE 4096
typedef struct
{
char name[4];
int age;
}people;
int main(int argc,char **argv)
{
int shm_id,i;
key_t key;
char temp;
people *p_map;
char *name="/home/program/shm/shm.txt";
key=ftok(name,0);
if(key==-1)
perror("ftok error");
shm_id=shmget(key,SIZE,IPC_CREAT);
p_map=(people *)shmat(shm_id,NULL,0);
for(i=0;i<10;i++)
{
printf("name:%s \n",(*(p_map+i)).name);
printf("age %d \n",(*(p_map+i)).age);
}
if(shmdt(p_map)==-1)
perror("detach error");
return 0;
}
共享内存的写程序
1.首先调用ftok()函数将一个路径名转化为key
2.调用shmget();函数,来返回一个共享内存地址
3.调用shmat()函数映射共享内存到本进程内存空间
4.调用shmdt()函数删除共享内存.
*/
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<sys/stat.h>
#include<fcntl.h>
#define SIZE 4096
typedef struct
{
char name[4];
int age;
}people;
int main(int argc,char **argv)
{
int shm_id,i,fd;
key_t key;
char temp;
people *p_map;
char *name="/home/program/shm/shm.txt";
if((fd=open("/home/program/shm/shm.txt",O_RDWR|O_CREAT))<0)//保证文件的存在,否则ftok()会出错
{
perror("open");
exit(1);
}
key=ftok(name,0);
if(key==-1)
{
perror("ftok error");
exit(1);
}
shm_id=shmget(key,SIZE,IPC_CREAT);
if(shm_id==-1)
{
perror("shmget");
exit(1);
}
p_map=(people *)shmat(shm_id,NULL,0);
temp='a';
for(i=0;i<10;i++)
{
memcpy((*(p_map+i)).name,&temp,1);
(*(p_map+i)).age=20+i;
temp+=1;
}
if(shmdt(p_map)==-1)
perror("detach error");
return 0;
}
/*程序 read_shm.c*/
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#define SIZE 4096
typedef struct
{
char name[4];
int age;
}people;
int main(int argc,char **argv)
{
int shm_id,i;
key_t key;
char temp;
people *p_map;
char *name="/home/program/shm/shm.txt";
key=ftok(name,0);
if(key==-1)
perror("ftok error");
shm_id=shmget(key,SIZE,IPC_CREAT);
p_map=(people *)shmat(shm_id,NULL,0);
for(i=0;i<10;i++)
{
printf("name:%s \n",(*(p_map+i)).name);
printf("age %d \n",(*(p_map+i)).age);
}
if(shmdt(p_map)==-1)
perror("detach error");
return 0;
}
浏览(44) 评论 编辑 删除
- linux 共享内存--系统V共享内存
- linux 共享内存--System V共享内存
- linux 共享内存与nginx共享内存
- linux内存共享
- Linux的共享内存
- linux 内存共享
- Linux 共享内存
- Linux 共享内存
- Linux共享内存
- Linux共享内存
- linux共享内存
- linux 内存共享mmap
- Linux 共享内存
- LINUX共享内存mmap
- linux共享内存
- Linux共享内存
- linux 内存共享举例
- Linux 共享内存
- xilinx时序约束
- VisualC++利用CRegKey和WIN32 API获取系统硬件信息
- 统计文本中每个单词的序列 和 出现次数
- Hibernate List 集合属性的配置
- UVa 11129 - An antiarithmetic permutation
- linux 共享内存
- java生成验证码图片
- HDU1863(最小生成树)
- hdu 2575 Count Problem (水)
- JS面向对象教程
- UVa 10245 - The Closest Pair Problem
- for循环中i++与++i
- js中createElement的使用
- hdu 1860 统计字符 (水)