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:
- #include <sys/mman.h>
- #include <sys/stat.h>/* for Mode constants */
- #include <fcntl.h>/* for o_* Constants */
- int Shm_open (const char *name, int oflag, mode_t mode);
- 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.
- #define SHM_IPC_FILENAME "Sln_shm_file"
- #define Shm_ipc_max_len 1024
Ser
- int Sln_shm_get (char *shm_file, void **shm, int mem_len)
- {
- int fd;
- FD = Shm_open (shm_file, O_RDWR | O_creat, 0644);
- if (FD < 0) {
- printf ("Shm_open <%s> failed:%s\n", Shm_file, Strerror (errno));
- return-1;
- }
- Ftruncate (FD, Mem_len);
- *SHM = Mmap (NULL, Mem_len, Prot_read | Prot_write, map_shared, FD, 0);
- if (map_failed = = *shm) {
- printf ("mmap:%s\n", Strerror (errno));
- return-1;
- }
- Close (FD);
- return 0;
- }
- int main (int argc, const Char *argv[])
- {
- char *shm_buf = NULL;
- Sln_shm_get (Shm_ipc_filename, (void * *) &shm_buf, Shm_ipc_max_len);
- snprintf (Shm_buf, Shm_ipc_max_len, "IPC client Get:hello POSIX share memory ipc! This was write by server. ");
- return 0;
- }
The results of the compilation execution are as follows:
- #./server
- #./client
- IPC Client Get:hello POSIX share memory ipc! This is the write by server.
- #
The file location created by Shm_open is located in the/dev/shm/directory:
- # ll/dev/shm/
- -rw-r--r--1 root root 8193 Oct 14:13 sln_shm_file
- #
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