Linux environment programming shared memory Area (ii): POSIX shared memory area

Source: Internet
Author: User
Tags posix

The concept of shared memory areas is now extended to include memory areas that are shared between unrelated processes. POSIX provides two ways to share memory areas between unrelated processes :

1. Memory mapped file: Opened by the Open function, the resulting descriptor is mapped to a file in the current process address space by the MMAP function. (This is the technique in the previous section)

2. Shared Memory Area object: Opened by Shm_open a POSIX name (perhaps a pathname in the file system), the returned descriptor is mapped by the MMAP function to the address space of the current process. (contents of this section)

The POSIX shared memory area involves the following two step requirements:

1. Specify a name parameter to call Shm_open to create a new shared memory area object or open an existing shared memory area object.

2. Call Mmap to map this shared memory area to the address space of the calling process.

The name parameter passed to Shm_open is then used by any other process that wants to share the memory area.

#include <sys/mman.h>intshm_open (const char *name, int oflags, mode_t mode);//return value: Successful return of nonnegative descriptor, error -1intshm_unlink (const char *name);//return value: Successfully returned 0, error returned-1

The Oflag parameter must be alive with a o_rdonly (read-only) flag, or contain a o_rdwr (read-write) flag, or you can specify the following flag: O_creat, O_EXCL, or O_trunc.

The mode parameter specifies the permission bit, which is used if the O_CREAT flag is specified. Note: Unlike the Mq_open and Sem_open functions, the mode parameter of the Shm_open must always be specified. If the O_CREAT flag is not specified, the read parameter can be specified as 0.

The return value of Shm_open is an integer descriptor. The Shm_unlink function deletes the name of a shared memory area object. Like all other unlink functions, deleting a name does not affect an existing reference to its underlying support object until all references to that object are closed.


When processing mmap, the size of an ordinary file or shared memory area object can be modified by calling Ftruncate.

#include <unistd.h>intftruncate (int fd, off_tlength);

POSIX has the following differences in the processing definitions for normal files and shared memory area objects in this function:

1, Ordinary files : If the size of the file is larger than the length parameter, the additional data is discarded. If the size of the file is less than length, then the file is not modified and its size is not increased.

2. Shared Memory Area object : Ftruncate Sets the size of the object to length bytes.

We call Ftruncate to specify the size of the newly created shared memory area object, or to modify the size of an existing object. When you open an existing shared memory area object, you can call Fstat to get information about the object.

#include <sys/types.h> #include <sys/stat.h>intfstat (int fd, struct stat *buf);  Successfully returned 0, error returned-1
The sample program is as follows:

Shmcreate Program:

#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include < Sys/types.h> #define FILE_MODE (s_irusr| s_iwusr| s_irgrp| S_iroth) intmain (int argc, char **argv) {int C, FD, Flags;char *ptr;off_tlength;flags = O_rdwr | O_creat;while (c = getopt (argc, argv, "E"))! =-1) {switch (c) {case ' e '; flags |= O_excl;break;}} if (optind! = argc-2) {printf ("usage:shmcreate [-e] <name> <length>\n"); return-1;} Length = Atoi (Argv[optind] + 1), FD = Shm_open (Argv[optind], flags, File_mode), Ftruncate (fd, length);p tr = mmap (NULL, Lengt H, Prot_read | Prot_write, map_shared, FD, 0); exit (0);}
Shmunlink Program

#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include < Sys/types.h>intmain (int argc, char **argv) {if (argc! = 2) {printf ("Usage:shunlink <name>\n"); return-1;} Shm_unlink (argv[1]); exit (0);}
Shmwrite Program

#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include < Sys/types.h> #define FILE_MODE (s_irusr| s_iwusr| s_irgrp| S_iroth) intmain (int argc, char **argv) {int I, Fd;structstat stat;unsigned char *ptr;if (argc! = 2) {printf ("Usage:shwrite & Lt;name>\n "); return-1;} /*open, get size, map*/fd = Shm_open (argv[1], O_RDWR, File_mode), Fstat (FD, &stat);p tr = mmap (NULL, Stat.st_size, Prot_ READ | Prot_write); close (FD);/*set:ptr[0] = 0, ptr[1] = 1, etc.*/for (i = 0; i < stat.st_size; i++) *ptr++ = i% 256;exit (0);}
Shmread Program

#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include < Sys/types.h> #define FILE_MODE (s_irusr| s_iwusr| s_irgrp| S_iroth) intmain (int argc, char **argv) {int I, fd;struct stat stat;unsigned char C, *ptr;if (argc! = 2) {printf ("Usage:shmrea D <name>\n "); return-1;} /*open, get size, map*/fd = Shm_open (argv[1], o_rdonly, File_mode), Fstat (FD, &stat);p tr = mmap (NULL, Stat.st_size, PRO T_read, map_shared, FD, 0), close (FD),/*check that ptr[0] = 0, ptr[1] = 1, etc*/for (i = 0; i < stat.st_size; i++) {if (c = *ptr++)! = (i%)) printf ("ptr[%d] =%d", I, c);} Exit (0);}






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.