Linux interprocess communication Learning: How to use shared memory

Source: Internet
Author: User
Tags semaphore linux

Here's another way to communicate between processes, using shared memory.

One, what is shared memory

As the name implies, shared memory allows two unrelated processes to access the same logical memory. Shared memory is a very efficient way to share and pass data between two running processes. Memory that is shared between different processes is usually scheduled to be the same segment of physical memory. Processes can connect the same segment of shared memory to their own address space, and all processes can access addresses in shared memory as if they were allocated by malloc with the C language function. If a process writes data to shared memory, the changes will immediately affect any other processes that can access the same shared memory.

Special reminder: Shared memory does not provide a synchronization mechanism, that is, there is no automatic mechanism to prevent the second process from starting to read it before the first process ends the write operation on the shared memory. So we usually need to use other mechanisms to synchronize access to shared memory, such as the semaphore mentioned earlier. For more on semaphores, check out my other article: Linux interprocess Communication--using semaphores

Second, the sharing of memory to make

As with semaphores, a set of functional interfaces is provided in Linux to use shared memory, and shared interfaces are very similar to semaphores, and are simpler than those that use semaphores. They are declared in the header file sys/shm.h.

1. Shmget function

This function is used to create shared memory, and its prototype is:

int Shmget (key_t key, size_t size, int shmflg);

The first parameter, like the Semget function of semaphores, requires that the program provide a parameter key (not 0 integers) that effectively names the shared memory segment, and returns a shared memory identifier (Non-negative integer) associated with the key when the Shmget function succeeds, for subsequent shared memory functions. Call failed to return-1.

Unrelated processes can access the same shared memory through the function's return value, which represents a resource that the program might want to use, and the program's access to all shared memory is indirect, and the program first calls the Shmget function and provides a key, A corresponding shared memory identifier (the return value of the Shmget function) is generated by the system, and only the Shmget function uses the semaphore key directly, and all other semaphore functions use the semaphore identifier returned by the Semget function.

Second argument, size specifies the amount of memory to be shared in bytes

The third argument, SHMFLG, is the permission flag, which acts like the mode parameter of the Open function, and can be done or manipulated with ipc_creat if it is created if the shared memory of the key identity does not exist. The permissions flag for shared memory is the same as the read and write permissions of a file, for example, 0644, which indicates that a process created by the creator of the shared memory is allowed to read and write data to the shared memory, while other users create processes that can read only shared memory.

2. Shmat function

When shared memory is created for the first time, it cannot be accessed by any process, and the Shmat function is used to initiate access to the shared memory and connect the shared memory to the current process's address space. Its prototype is as follows:

void *shmat (int shm_id, const void *shm_addr, int shmflg);

The first parameter, shm_id, is the shared memory identity returned by the Shmget function.

The second parameter, SHM_ADDR specifies that the shared memory is connected to the address location in the current process, usually empty, indicating that the system chooses the address of the shared memory.

Third argument, SHM_FLG is a set of flag bits, usually 0.

Returns a pointer to the first byte of shared memory if the call succeeds, or returns 1 if the call fails.

3. Shmdt function

This function is used to detach shared memory from the current process. Note that separating shared memory does not remove it, except that the shared memory is no longer available to the current process. Its prototype is as follows:

void *shmat (int shm_id, const void *shm_addr, int shmflg);

The parameter shmaddr is the address pointer returned by the SHMAT function, which returns 0 when the call succeeds, and returns 1 when it fails.

4. Shmctl function

Like the Semctl function of semaphores, used to control shared memory, its prototype is as follows:

void *shmat (int shm_id, const void *shm_addr, int shmflg);

The parameter shmaddr is the address pointer returned by the SHMAT function, which returns 0 when the call succeeds, and returns 1 when it fails.

4. Shmctl function

Like the Semctl function of semaphores, used to control shared memory, its prototype is as follows:

struct Shmid_ds  
{  
    uid_t shm_perm.uid;  
    uid_t Shm_perm.gid;  
    mode_t shm_perm.mode;  
};

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.