共享内存的代码

映射写端

#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;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值