共享内存:shm
共享内存是在两个正在运行的进程之间共享和传递数据的一种非常有效的方式;
并未提供同步机制
申请
shmid = shmget(key_t , sizeof() , 0666|IPC_CREAT)-- 得到int shmid
销毁
shmctl(shmid, IPC_RMID, 0 );
连接映射
void * shm = shmat (shmid , 0 ,0) 系统自动分配,默认属性内存;
需要强制类型转换指向对应的对应对象
取消映射
shmdt(shm)
参考:http://blog.csdn.net/ljianhui/article/details/10253345
共享内存是在两个正在运行的进程之间共享和传递数据的一种非常有效的方式;
并未提供同步机制
申请
shmid = shmget(key_t , sizeof() , 0666|IPC_CREAT)-- 得到int shmid
销毁
shmctl(shmid, IPC_RMID, 0 );
连接映射
void * shm = shmat (shmid , 0 ,0) 系统自动分配,默认属性内存;
需要强制类型转换指向对应的对应对象
取消映射
shmdt(shm)
设置好之后就可以直接访问结构体中的成员了
实例:
shmdata.h
#ifndef _SHMDATA_H_HEADER
#define _SHMDATA_H_HEADER
#define TEXT_SZ 2048
struct shared_use_st
{
pid_t pid_w;
pid_t pid_r;
char text[TEXT_SZ];//记录写入和读取的文本
};
#endif
read.c
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/shm.h>
#include "shmdata.h"
#include <signal.h>
int running = 1;//程序是否继续运行的标志
struct shared_use_st *shared;//指向shm
void readfunc(int sig)
{
printf("You wrote: %s", shared->text);
//输入了end,退出循环(程序)
if(strncmp(shared->text, "exit", 4) == 0)
running = 0;
else
kill(shared->pid_w ,SIGUSR1);
}
int main()
{
void *shm = NULL;//分配的共享内存的原始首地址
int shmid;//共享内存标识符
//创建共享内存
shmid = shmget(ftok("/usr",'a'), sizeof(struct shared_use_st), 0666|IPC_CREAT); // 结构体方便控制内存大小
if(shmid == -1)
{
fprintf(stderr, "shmget failed\n");
exit(EXIT_FAILURE);
}
//将共享内存连接到当前进程的地址空间 attached
shm = shmat(shmid, 0, 0); //使用系统默认设置,得到系统自动分配的地址 , void *
if(shm == (void*)-1)
{
fprintf(stderr, "shmat failed\n");
exit(EXIT_FAILURE);
}
printf("\nMemory attached at %X\n", (int)shm);
//设置共享内存
shared = (struct shared_use_st*)shm; // 指明该基地址指向的对象
shared->pid_r = getpid();
signal(SIGUSR1,SIG_IGN);
signal(SIGUSR2,readfunc);
while(running)//读取共享内存中的数据
{
sleep(1);
}
//把共享内存从当前进程中分离
if(shmdt(shm) == -1) // deattach
{
fprintf(stderr, "shmdt failed\n");
exit(EXIT_FAILURE);
}
//删除共享内存
if(shmctl(shmid, IPC_RMID, 0) == -1)
{
fprintf(stderr, "shmctl(IPC_RMID) failed\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
write.c
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#include "shmdata.h"
#include <signal.h>
struct shared_use_st *shared = NULL;
int running = 1;
void writefunc(int sig)
{
char buffer[BUFSIZ + 1];//用于保存输入的文本
//向共享内存中写入数据
printf("Enter some text: ");
fgets(buffer, BUFSIZ, stdin);
strncpy(shared->text, buffer, TEXT_SZ);
//输入了end,退出循环(程序)
if(strncmp(buffer, "exit" ,4) == 0)
running = 0;
kill(shared->pid_r, SIGUSR2);
}
int main()
{
void *shm = NULL;
int shmid;
//创建共享内存
shmid = shmget(ftok("/usr",'a'), sizeof(struct shared_use_st), 0666|IPC_CREAT);
if(shmid == -1)
{
fprintf(stderr, "shmget failed\n");
exit(EXIT_FAILURE);
}
//将共享内存连接到当前进程的地址空间
shm = shmat(shmid, (void*)0, 0);
if(shm == (void*)-1)
{
fprintf(stderr, "shmat failed\n");
exit(EXIT_FAILURE);
}
printf("Memory attached at %X\n", (int)shm);
//设置共享内存
shared = (struct shared_use_st*)shm;
shared->pid_w = getpid();
signal(SIGUSR1,writefunc);
signal(SIGUSR2,SIG_IGN);
raise(SIGUSR1); // 发信号给自己,开始写
while(running)//向共享内存中写数据
{
printf("in running \n");
sleep(1);
}
printf("end running \n");
//把共享内存从当前进程中分离
if(shmdt(shm) == -1)
{
fprintf(stderr, "shmdt failed\n");
exit(EXIT_FAILURE);
}
sleep(2);
exit(EXIT_SUCCESS);
}
参考:http://blog.csdn.net/ljianhui/article/details/10253345