#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/msg.h>
#define BUFSIZE 2048
#define SHMFLG 0777
#define MSGKEY 4152
struct msgform
{
int mtype;
int mtext;
}msg1;
int main()
{
int shmid,msgid1;
char *shmadd;
struct shmid_ds buff;
msgid1 = msgget(MSGKEY,0666|IPC_CREAT);
/*Creat a shared-memory*/
if((shmid = shmget(IPC_PRIVATE,BUFSIZE,SHMFLG|IPC_CREAT)) < 0)
{
perror("shmget");
exit(1);
}
while(1)
{
msg1.mtype = 1;
msg1.mtext = shmid;
// msgsnd(msgid1,&msg1,128,0);
/*Attached shared-memory*/
if((shmadd = shmat(shmid,0,0)) < (char *)0){
perror("shmat");
exit(1);
}
shmctl(shmid,IPC_STAT,&buff);
printf("Please input your message: ");
fgets(shmadd,256,stdin);
printf("\n");
msgsnd(msgid1,&msg1,128,0);
if(!strncasecmp(shmadd,"quit",4)) break;
}
/*Deleted shared-momory*/
if((shmdt(shmadd)) < 0){
perror("shmdt");
exit(1);
}
shmctl(shmid,IPC_RMID,0);
msgctl(msgid1,IPC_RMID,0);
exit(0);
}
没有合适的资源?快使用搜索试试~ 我知道了~
Linux下共享内存通信演示(C程序)
4星 · 超过85%的资源 需积分: 10 36 下载量 80 浏览量
2011-12-29
17:16:55
上传
评论
收藏 781B GZ 举报
温馨提示
共2个文件
c:2个
一个简单的共享内存的通信程序,shm_eg1.c创建共享内存并写入数据,shm_eg2.c访问共享内存并读取打印数据。
资源推荐
资源详情
资源评论
收起资源包目录
共享内存通信演示.gz (2个子文件)
shm_eg1.c 1KB
shm_eg2.c 772B
共 2 条
- 1
kgdlte
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页