映射写端
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <string.h>
#include<signal.h>
#include <strings.h>
// key_t ftok(const char *pathname, int proj_id);
#define shm_size 4096
#define shm_name "./shmfile"
void print_err(char *estr)
{
perror(estr);
exit(-1);
}
int shmid=-1;
void *shmaddr =NULL;
void shm(void){
int fd =0;
int ret=0;
key_t key =-1;
fd =open (shm_name ,O_RDWR|O_CREAT,0664);
if(fd ==-1)print_err("open fail");
key=ftok(shm_name ,'b');
if(key==-1)print_err("ftok fail");
//int shmget(key_t key, size_t size, int shmflg);
shmid=shmget(key,shm_size,0664|IPC_CREAT);
if(shmid==-1)print_err("shmget fail");
}
char buf[300]={"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
444444444444444444444444444444444444444444444444444444\
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj"};
void signal_fun(int signo){
shmdt(shmaddr);
shmctl(shmid,IPC_RMID,NULL);
exit(-1);
}
int main(){
signal(SIGINT,signal_fun);
void *shmaddr =NULL;
//int shmget(key_t key, size_t size, int shmflg);
shm();
//jian li ying she
//void *shmat(int shmid, const void *shmaddr, int shmflg);
shmaddr=shmat(shmid,NULL,0);
if(shmaddr==(void*) -1)print_err("shmat fail");
while(1){
//nei ron fu zhi
//void *memcpy(void *dest, const void *src, size_t n)
memcpy(shmaddr,buf,sizeof(buf));
}
// int shmdt(const void *shmaddr);
return 0;
}
映射读端
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <string.h>
#include<signal.h>
#include <strings.h>
// key_t ftok(const char *pathname, int proj_id);
#define shm_size 4096
#define shm_name "./shmfile"
void print_err(char *estr)
{
perror(estr);
exit(-1);
}
int shmid=-1;
void *shmaddr =NULL;
void shm(void){
int fd =0;
int ret=0;
key_t key =-1;
fd =open (shm_name ,O_RDWR|O_CREAT,0664);
if(fd ==-1)print_err("open fail");
key=ftok(shm_name ,'b');
if(key==-1)print_err("ftok fail");
//int shmget(key_t key, size_t size, int shmflg);
shmid=shmget(key,shm_size,0664|IPC_CREAT);
if(shmid==-1)print_err("shmget fail");
}
char buf[300]={"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
444444444444444444444444444444444444444444444444444444\
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj"};
void signal_fun(int signo){
shmdt(shmaddr);//qu xiao ying she
shmctl(shmid,IPC_RMID,NULL);//shan chu ying she
exit(-1);
}
int main(){
signal(SIGINT,signal_fun);
void *shmaddr =NULL;
//int shmget(key_t key, size_t size, int shmflg);
shm();
//jian li ying she
//void *shmat(int shmid, const void *shmaddr, int shmflg);
shmaddr=shmat(shmid,NULL,0);
if(shmaddr==(void*)-1)print_err("shmat fail");
while(1){
if(strlen((char *)shmaddr) !=0){
printf("%s\n",(char *)shmaddr);
bzero(shmaddr,shm_size);
sleep(3);
}
}
// int shmdt(const void *shmaddr);
return 0;
}