C语言共享内存-基本操作
2013-08-05 20:14
351 查看
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <error.h>
#include <string.h>
#define SIZE 4096
#define BUF_SIZE 128
void w_shm(char *pstr);
void r_shm(char *pstr);
void del_shm(int shmid);
void display_shm(int shmid);
int main(int argc,char **argv)
{
key_t key=get_key();
int shmid;
char *pstr;
shmid=shmget(key,SIZE,IPC_CREAT|0600);
if(shmid==-1)
{
perror("shmget error");
exit(1);
}
printf("创建共享内存成功,shmid=%d\n",shmid);
if((pstr=shmat(shmid,NULL,0))==(void *)(-1))
{
perror("shmat error");
exit(1);
}
printf("连接共享内存成功,地址是%p\n",pstr);
w_shm(pstr);
display_shm(shmid);
//del_shm(shmid);
//shmdt(pstr);
return 0;
}
int get_key()
{
int key;
key=ftok(".",'k');
return key;
}
void w_shm(char *pstr)
{
char buffer[BUF_SIZE];
printf("请输入要写入的内容\n");
fgets(buffer,BUF_SIZE,stdin);
strcpy(pstr,buffer);
}
void r_shm(char *pstr)
{
printf("共享内存中的数据是\n");
printf("%s\n",pstr);
}
void del_shm(int shmid)
{
if(shmctl(shmid,IPC_RMID,0)==-1)
{
perror("rm shm error");
exit(1);
}
}
void display_shm(int shmid)
{
struct shmid_ds *buf;
buf=(struct shmid_ds *)malloc(sizeof(struct shmid_ds));
if(shmctl(shmid,IPC_STAT,buf)==-1)
{
perror("shmctl error");
exit(1);
}
printf("shm_cpid=%d\n",buf->shm_cpid);
}
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <error.h>
#include <string.h>
#define SIZE 4096
#define BUF_SIZE 128
void w_shm(char *pstr);
void r_shm(char *pstr);
void del_shm(int shmid);
void display_shm(int shmid);
int main(int argc,char **argv)
{
key_t key=get_key();
int shmid;
char *pstr;
shmid=shmget(key,SIZE,IPC_CREAT|0600);
if(shmid==-1)
{
perror("shmget error");
exit(1);
}
printf("创建共享内存成功,shmid=%d\n",shmid);
if((pstr=shmat(shmid,NULL,0))==(void *)(-1))
{
perror("shmat error");
exit(1);
}
printf("连接共享内存成功,地址是%p\n",pstr);
w_shm(pstr);
display_shm(shmid);
//del_shm(shmid);
//shmdt(pstr);
return 0;
}
int get_key()
{
int key;
key=ftok(".",'k');
return key;
}
void w_shm(char *pstr)
{
char buffer[BUF_SIZE];
printf("请输入要写入的内容\n");
fgets(buffer,BUF_SIZE,stdin);
strcpy(pstr,buffer);
}
void r_shm(char *pstr)
{
printf("共享内存中的数据是\n");
printf("%s\n",pstr);
}
void del_shm(int shmid)
{
if(shmctl(shmid,IPC_RMID,0)==-1)
{
perror("rm shm error");
exit(1);
}
}
void display_shm(int shmid)
{
struct shmid_ds *buf;
buf=(struct shmid_ds *)malloc(sizeof(struct shmid_ds));
if(shmctl(shmid,IPC_STAT,buf)==-1)
{
perror("shmctl error");
exit(1);
}
printf("shm_cpid=%d\n",buf->shm_cpid);
}
相关文章推荐
- Linux下的C语言编程——线程编程基本操作
- 『黑马程序员』---java--IO--分段读写-操作基本类型-内存为源
- 内存共享基本演示
- 单向链表基本操作(C语言实现)
- linux 文件操作函数 通过例子来解释 父子间文件描述符共享 内存映像图
- c语言内存操作函数汇总
- c语言单链表的基本操作
- 用C语言编写一个包含链表的初始化、插入、删除、查找等基本操作的程序。
- 基本文件流操作(C语言)
- C语言消息队列--基本操作
- C语言实现双向非循环链表(带头结点尾结点)的基本操作
- 共享内存创建shmget控制操作shmat,shmctl
- C语言来实现链表的相加基本操作 桂林电子科技大学 大二实验
- Mohican_4/1 C语言 移位运算基本操作 计算二进制的个数
- c语言队列基本操作代码
- C语言(数据结构) - 链表的基本操作
- 单链表的基本操作C语言实现
- C语言线性表循环队列的基本操作
- c语言基本数据类型在内存中所占字节
- C++实现矩阵类型的基本操作:创建矩阵(M x N)、释放内存、获取某一元素的值、修改某一元素的值、乘法操作