linux 进程间共享内存
2009-12-16 23:05
113 查看
可以采用sysV的shmget + shmat 实现。
但是我更喜欢shm_open + mmap 更简单。
#---------------------writer.c----------------------------
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/mman.h>
struct ofs_stat
{
int a;
int b;
int c;
};
int main(void)
{
int fd;
int count = 0;
struct ofs_stat stat={0};
struct ofs_stat *s;
void *region=NULL;
char *name;
if((fd=shm_open("ofs_mmm", O_TRUNC|O_CREAT|O_RDWR,0644))==-1) {
perror("open");
exit(1);
}
ftruncate(fd, sizeof(stat));
region = mmap(NULL, sizeof(struct stat), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if(region == (caddr_t)-1) {
perror("mmap");
shm_unlink("ofs_mmm");
return 1;
}
s = (struct ofs_stat *)region;
while(1) {
s->a=count++;
printf("%d /n",s->a);
sleep(1);
}
shm_unlink("ofs_mmm");
return 0;
}
#--------------------reader.c----------------------------
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/mman.h>
struct ofs_stat
{
int a;
int b;
int c;
};
int main(void)
{
int fd;
int count = 0;
struct ofs_stat stat={0};
struct ofs_stat *s;
void *region=NULL;
char *name;
if((fd=shm_open("ofs_mmm", O_CREAT|O_RDONLY,0644))==-1) {
perror("open");
exit(1);
}
ftruncate(fd, sizeof(struct stat));
region = mmap(NULL, sizeof(struct stat), PROT_READ, MAP_SHARED, fd, 0);
if(region == (caddr_t)-1) {
perror("mmap");
shm_unlink("ofs_mmm");
return 1;
}
s = (struct ofs_stat *)region;
while(1) {
printf("%d /n",s->a);
sleep(1);
}
shm_unlink("ofs_mmm");
return 0;
}
但是我更喜欢shm_open + mmap 更简单。
#---------------------writer.c----------------------------
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/mman.h>
struct ofs_stat
{
int a;
int b;
int c;
};
int main(void)
{
int fd;
int count = 0;
struct ofs_stat stat={0};
struct ofs_stat *s;
void *region=NULL;
char *name;
if((fd=shm_open("ofs_mmm", O_TRUNC|O_CREAT|O_RDWR,0644))==-1) {
perror("open");
exit(1);
}
ftruncate(fd, sizeof(stat));
region = mmap(NULL, sizeof(struct stat), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if(region == (caddr_t)-1) {
perror("mmap");
shm_unlink("ofs_mmm");
return 1;
}
s = (struct ofs_stat *)region;
while(1) {
s->a=count++;
printf("%d /n",s->a);
sleep(1);
}
shm_unlink("ofs_mmm");
return 0;
}
#--------------------reader.c----------------------------
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/mman.h>
struct ofs_stat
{
int a;
int b;
int c;
};
int main(void)
{
int fd;
int count = 0;
struct ofs_stat stat={0};
struct ofs_stat *s;
void *region=NULL;
char *name;
if((fd=shm_open("ofs_mmm", O_CREAT|O_RDONLY,0644))==-1) {
perror("open");
exit(1);
}
ftruncate(fd, sizeof(struct stat));
region = mmap(NULL, sizeof(struct stat), PROT_READ, MAP_SHARED, fd, 0);
if(region == (caddr_t)-1) {
perror("mmap");
shm_unlink("ofs_mmm");
return 1;
}
s = (struct ofs_stat *)region;
while(1) {
printf("%d /n",s->a);
sleep(1);
}
shm_unlink("ofs_mmm");
return 0;
}
相关文章推荐
- Linux 进程通信--共享内存
- linux 进程间的通信(五) 共享内存-2
- Linux进程通信--共享内存
- Linux 进程通信--共享内存
- [Linux学习]Linux下进程通讯之共享内存
- Linux进程通信-共享内存
- Linux进程间如何共享内存
- linux进程间的通信之 共享内存
- Linux C 进程间的IPC通信 之 共享内存(一)
- [转] linux下使用mmap实现进程间共享内存
- linux进程间同步和共享内存
- 将 Win32 C/C++ 应用程序迁移到 POWER 上的 Linux,第 1 部分: 进程、线程和共享内存服务 (转载)
- linux 进程通信之内存共享
- Linux进程通信-共享内存
- Linux进程间通信--进程,信号,管道,消息队列,信号量,共享内存
- 将 Win32 C/C++ 应用程序迁移到 POWER 上的 Linux,第 1 部分: 进程、线程和共享内存服务
- Linux进程间通信--进程,信号,管道,消息队列,信号量,共享内存
- Linux\Unix IPC进程通信实例分析(一):共享内存通信---文件映射mmap方式
- linux进程通信--共享内存
- linux进程间的通信(C): 共享内存