POSIX shared memory and series functions for Linux network programming

Source: Internet
Author: User
Tags file system posix

Before introducing the System V shared memory knowledge, let's take a look at POSIX shared memory and series functions a little bit.

Shared memory is simply a real area of physical memory that can be mapped to a process's address space using some functions, while POSIX shared memory differs from System V shared memory by using a virtual file system (TMPFS) that is already mounted on/DEV/SHM Below. Mans 7 Shm_overview

Let's look at the series functions, compile time with the-LRT option, that is, connect the LIBRT library (real Time library)

Function: Used to create or open a shared memory object

Prototype int Shm_open (const char *name, int oflag, mode_t mode);

Parameters

Name: The names of the shared memory objects must be in/beginning, and subsequent cannot have other/, the shape such as/somename length can not exceed Name_max (255)

Oflag: Similar to the Open function, can be o_rdonly, O_RDWR, can also be bitwise or up o_creat, O_EXCL, O_trunc, and so on.

Mode: This parameter always needs to be set, and if Oflag does not specify O_creat, you can specify 0

Return value: Successful return of nonnegative integer file descriptor; failed return-1

Note that there is no so-called shm_close function that can be used directly to close the file descriptor.

Function: Modify the shared memory object size, Shm_open Unlike shmget you can set the size of shared memory, but you can use Ftruncate to set the size.

Prototype int ftruncate (int fd, off_t length);

Parameters

FD: File descriptor

Length: Lengths

Return value: successful return 0; failure return-1

Function: Get shared Memory object information

Prototype

int fstat (int fd, struct stat *buf);

Parameters

FD: File descriptor

BUF: Return to shared memory status

Return value: successful return 0; failure return-1

struct STAT can refer to here.

Similar to Shm_ctl (, Ipc_stat,);

Function: Delete a shared memory object

Prototype int Shm_unlink (const char *name);

Parameters

Name: Names of shared memory objects

Return value: successful return 0; failure return-1

Shm_unlink similar to Shm_ctl (, Ipc_rmid,);

Function: Maps a shared memory object to the process address space.

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

Parameters

Addr: The starting address to map, usually specified as NULL, allowing the kernel to automatically select

Len: Number of bytes mapped to the process address space

Prot: Mapping Area protection mode

Flags: Flags

FD: File descriptor

Offset: Offsets starting from file headers

Return value: Successfully returns the starting address of the mapped memory area; failed return-1

In the previous introduction of the MMAP function to map files to the process address space, in fact it can also map the shared memory object to the process address space, similar to the role of Shmat, but the incoming file descriptor FD is Shm_open returned. Similarly, Munmap can be used to remove mappings, similar to the SHMDT function.

Related Article

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.