About Linux IPC (iv): POSIX shared memory

Source: Internet
Author: User
Tags posix

The previous section described opening a memory-mapped file by the Open function, and then mapping the resulting descriptor to the current process address space by the MMAP function. This section describes another similar shared memory approach, where the Shm_open function opens a posix.1 IPC name (perhaps a path name in the filesystem), and the returned descriptor is mapped by the function mmap to the current process address space.

POSIX shared memory similar to the previous section, you first need to make a name parameter call Shm_open to create a new shared memory object or open an existing shared memory object, and then call Mmap to map the shared memory area to the address space of the calling process.
The Shm_open function is prototyped as follows:
    1. #include <sys/mman.h>
    2. #include <sys/stat.h>/* for Mode constants */
    3. #include <fcntl.h>/* for o_* Constants */
    4. int Shm_open (const char *name, int oflag, mode_t mode);
    5. Link WITH-LRT.
The parameter name is the shared memory object that will be opened or created and needs to be specified as a/name format.
The parameter oflag must have o_rdonly (read-only), Flag o_rdwr (read-write), and you can specify O_creat (no shared object is created), O_EXCL (if O_creat is specified, but name does not exist, returns an error), O_ TRUNC (if the shared memory exists, the length is truncated to 0).
The parameter mode is the specified permission bit, used with the specified o_creat, and if no specified O_creat,mode can be specified as 0.

The function returns a descriptor, which is used in the MMAP function (see the previous section) in the fifth parameter.
    1. #define SHM_IPC_FILENAME "Sln_shm_file"
    2. #define Shm_ipc_max_len 1024

Ser
  1. int Sln_shm_get (char *shm_file, void **shm, int mem_len)
  2. {
  3. int fd;
  4. FD = Shm_open (shm_file, O_RDWR | O_creat, 0644);
  5. if (FD < 0) {
  6. printf ("Shm_open <%s> failed:%s\n", Shm_file, Strerror (errno));
  7. return-1;
  8. }
  9. Ftruncate (FD, Mem_len);
  10. *SHM = Mmap (NULL, Mem_len, Prot_read | Prot_write, map_shared, FD, 0);
  11. if (map_failed = = *shm) {
  12. printf ("mmap:%s\n", Strerror (errno));
  13. return-1;
  14. }
  15. Close (FD);
  16. return 0;
  17. }
  18. int main (int argc, const Char *argv[])
  19. {
  20. char *shm_buf = NULL;
  21. Sln_shm_get (Shm_ipc_filename, (void * *) &shm_buf, Shm_ipc_max_len);
  22. snprintf (Shm_buf, Shm_ipc_max_len, "IPC client Get:hello POSIX share memory ipc! This was write by server. ");
  23. return 0;
  24. }
The results of the compilation execution are as follows:

    1. #./server
    2. #./client
    3. IPC Client Get:hello POSIX share memory ipc! This is the write by server.
    4. #

The file location created by Shm_open is located in the/dev/shm/directory:
    1. # ll/dev/shm/
    2. -rw-r--r--1 root root 8193 Oct 14:13 sln_shm_file
    3. #

POSIX shared memory synchronization can be implemented using POSIX semaphores, which are explained in detail later in the synchronization related column.
Download the source code in this section:
http://download.csdn.net/detail/gentleliu/8140869
    • Related articles recommended:
    • Linux assembly gas Call C language function instance
    • Write the simplest kernel: HelloWorld
    • Display the contents of bmpstring in the terminal of Linux
    • This article comes from: Hobby Linux
    • This article link: http://www.ahlinux.com/c/9589.html

About Linux IPC (iv): POSIX shared memory

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.