IPC --- using shared memory

(1) pipeline (the simplest): pipe, anonymous pipes, can not be repeated read / fifo named pipe can be repeatedly read (for inter-process communication unrelated)
principle: the kernel buffer mechanism, realized in the form circular queue
limitations: 1. the process can not write their own reading
2. data read can not be repeated
3. one-way communication, the half-duplex
4. there can only be used with a common ancestor between processes
(2) signal (minimum overhead)
(3) shared memory (which may unrelated): mmap ------- munmap (release distribution out of shared memory, the address must first release from the beginning)
void * mmap (void * addr, size_t length, Prot int, int flags int fd, off_t offset)
return value: the successful return address mapping to create the first, failed to return MAP_FAILED macro
parameters: 1.add, the establishment of the first address mapping area, designated by the kernel, NULL direct use;
2.length, want to create a map of the area size
3.prot, mapping area authority, PROT_READ, PROT_WRITE, PROT_READ | PROT_WRITE
4.flags, flag parameter, MAP_SHARED (the operation done by mapping area reflected on the disk)
to modify MAP_PRIVATE (mapping area made Will not be reflected on the disk)
5.fd, used to build the mapping file descriptor area (read and write files must be opened because 4 will be shared by the memory write to disk)
integer 6. offset mapping file (4K of times)
(4) local socket (most stable)

Shared memory communication (blood communication) between parent and child

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<sys/mman.h>
  4 #include<unistd.h>
  5 #include<fcntl.h>
  6 #include<string.h>
  7 
  8 int main()
  9 {
 10         int fd = open("mytest.txt",O_RDWR | O_CREAT,0644);
 11         if(fd == -1 )
 12         {
 13                 perror("open error");
 14                 exit(1);
 15         }
 16 
 17         int len = ftruncate(fd,4);
 18         if(len == -1)
 19         {
 20                 perror("ftruncate error");
 21                 exit(1);
 22         }
 23         char * p=NULL;
 24         p = mmap(NULL,4,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
 25         if( p == MAP_FAILED )
 26         {
 27                 perror("mmap error");
 28                 exit(1);
 29         }
 30         strcpy(p,"abvc\n");
 31 
 32         int ret = munmap(p,4);
 33         if(ret == -1)
 34         {
 35                 perror("mummap error");
 36                 exit(1);
 37         }
 38         close(fd);
 39         return 0;
 40 }

No communication process between blood relations
mmap_r.c (read end of the process)

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<sys/mman.h>
  4 #include<unistd.h>
  5 #include<fcntl.h>
  6 #include<string.h>
  7 struct student{
  8 
  9         int id;
 10         char name[20];
 11         char sex;
 12 };
 13 int main()
 14 {
 15         int fd = open("file_shared.txt",O_RDWR | O_CREAT,0644);
 16         if(fd == -1 )
 17         {
 18                 perror("open error");
 19                 exit(1);
 20         }
 21 
 22         struct student st;
 23         int len = ftruncate(fd,sizeof(st));
 24         if(len == -1)
 25         {
 26                 perror("ftruncate error");
 27                 exit(1);
 28         }
 29         struct student * p=NULL;
 30         p = mmap(NULL,sizeof(st),PROT_READ,MAP_SHARED,fd,0);
 31         if( p == MAP_FAILED )
 32         {
 33                 perror("mmap error");
 34                 exit(1);
 35         }
 36 
 37         while(1)
 38         {
 39                 sleep(2);
 40                 printf("正在读取中.....\n");
 41                 printf("id=%d,name=%s, sex=%c\n",p->id,p->name,p->sex);
 42 
 43         }
 44         int ret = munmap(p,sizeof(st));
 45         if(ret == -1)
 46         {
 47                 perror("mummap error");
 48                 exit(1);
 49         }
 50         close(fd);
 51         return 0;
 52 }

mmap_w.c().

Published 38 original articles · won praise 13 · views 4334

Guess you like

Origin blog.csdn.net/YanWenCheng_/article/details/103935650