Shared memory of POSIX shared memory

Overview:

    Shared memory is the fastest form of IPC available. Once such a memory area is mapped into the address space of the processes that share it, these interprocess data transfers no longer involve the kernel. However, some form of synchronization is required between processes that store or retrieve information from this shared memory area.

related functions:

  void *mmap(void *addr, size_t length, int prot, int flags,
                  int fd, off_t offset);

The mmap function maps a file or a POSIX shared memory area object into the address space of the calling process. This function is used for three purposes:

    (1) Use ordinary files to provide memory-mapped I/O;

    (2) Use special files to provide anonymous memory support.

    (3) Use shm_open to provide a POSIX shared memory area between unrelated processes.

Parameter Description:

    addr: You can specify the starting address of the in-process space where the descriptor fd should be mapped. It is usually specified as a null pointer, leaving the kernel to choose the starting address.

    length: The number of bytes mapped to the calling process space, which is counted from the offset byte from the beginning of the mapped file. offset is usually set to 0.

    prot: protection of the memory mapped area

        PROT_READ data can be read

        PROT_WRITE data can be written

        PROT_EXEC data executable

        PROT_NONE data not accessible

    flags: One of the two flags MAP_SHARED or MAP_PRIVATE must be specified, and can be selected or MAX_FIXED.

int munmap(void *addr, size_t length);

To remove a mapping from a process' address space, we call munmap. The addr parameter is the address returned by map, and len is the size of the mapped area.

Note: Refer to UNIX Network Programming Volume 2: Interprocess Communication

test program:

    Write data to shared memory:

#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#include <sys/stat.h>
#include <fcntl.h>

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/mman.h>
#define ERR_EXIT(m)				\
		do				\
		{				\
			perror(m);		\
			exit(EXIT_FAILURE);	\
		}while(0);			\

typedef struct stu
{
	char name[4];
	int age;
}STU;

int main(int argc,char *argv[])
{
	if (argc != 2)
	{
		fprintf(stderr,"User:%s <file>\n",argv[0]);
		exit(EXIT_FAILURE);
	}

	int fd;
	fd = open(argv[1],O_CREAT | O_RDWR | O_TRUNC, 0666);
	if(fd == -1)
		ERR_EXIT("open");
	lseek(fd,sizeof(STU)*5-1,SEEK_SET);
	write(fd,"",1);

	STU * p;
	p = (STU*)mmap(NULL,sizeof(STU)*5, PROT_READ | PROT_WRITE,MAP_SHARED,fd,0);
	if(p == NULL)
		ERR_EXIT("mmap");

	char ch = 'a';
	int i = 0;
	
	for(i=0; i<5; i++)
	{
		memcpy((p+i)->name,&ch,1);
		(p+i)->age = 20+i;
		ch++;
	}
	
	printf("initialize over\n");
	
	munmap(p,sizeof(STU)*5);
	printf("exit\n");

	return 0;
}





 Read data from shared memory:

#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#include <sys/stat.h>
#include <fcntl.h>

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/mman.h>
#define ERR_EXIT(m)				\
		do				\
		{				\
			perror(m);		\
			exit(EXIT_FAILURE);	\
		}while(0);			\

typedef struct stu
{
	char name[4];
	int age;
}STU;

int main(int argc,char *argv[])
{
	if (argc != 2)
	{
		fprintf(stderr,"User:%s <file>\n",argv[0]);
		exit(EXIT_FAILURE);
	}

	int fd;
	fd = open(argv[1],O_RDWR);
	if(fd == -1)
		ERR_EXIT("open");

	STU * p;
	p = (STU*)mmap(NULL,sizeof(STU)*5, PROT_READ | PROT_WRITE,MAP_SHARED,fd,0);
	if(p == NULL)
		ERR_EXIT("mmap");

	int i;
	for(i=0; i<5; i++)
	{
		printf("name = %s\nage=%d\n",(p+i)->name,(p+i)->age);
	}
	
	munmap(p,sizeof(STU)*5);
	printf("exit\n");

	return 0;
}






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324707057&siteId=291194637