Interprocess communication shared memory and signal ---

1. Check the shared memory segment system

-m ipcs
2. Delete the system of shared memory segment

-m ipcrm [shmid]
3.shmget (): create a shared memory

shmget int (key_t Key, size_t size, int shmflg);
[Parameters key]: IPC unique resource identifier generated by ftok key, identification system.

[Parameter size]: need to apply for shared memory size. In the operating system, the application of the minimum unit of memory pages, one is 4k bytes, the memory size in order to avoid memory fragmentation, we generally apply to an integer multiple of the page.

[Parameters shmflg]: If you want to create a new shared memory, you need to use IPC_CREAT, IPC_EXCL, if it already exists, or can be used directly pass IPC_CREAT 0.

[Return value]: Returns a new or existing shared memory identifier if successful, depending on the parameters of shmflg. Return -1 fails and an error code.

4.shmat (): Mounting a Shared Memory

* the shmat void (int the shmid, by shmaddr const void *, int shmflg);
[Parameters shmid]: identifier of the shared memory segment.

[Parameter * shmaddr]: shmaddr = 0, then the memory segments can be connected to a first address selected by the kernel (recommended).

[Parameter shmflg]: If the specified bit SHM_RDONLY as read-only connect this section, this section or is connected to read-write.

[Return value]: successful return pointer (virtual address) to a shared memory segment, and will make a kernel associated with the shared memory segment shmid_ds structure shm_nattch counter is incremented by 1 (similar reference count); error return -1.

5.shmdt (): to associate the shared memory

When a process does not require shared memory, you need to associate. This function does not delete the specified shared memory area, but the good before you connect the shared memory area from the current process with the shmat function.

shmdt int (const void * by shmaddr);
[Parameter * shmaddr]: After connecting the return address.

[Return value]: return 0 if successful, and the structural body shm_nattch shmid_ds counter by 1; -1 error.

6.shmctl (): the destruction of shared memory

the shmctl int (the shmid int, int cmd, struct the shmid_ds * buf);
[Parameters shmid]: shared memory segment identifier.

[Parameters cmd]: perform the specified operation can remove the shared memory setting indicates when IPC_RMID.

[Parameter * buf]: can be set to NULL.

[Return value]: return 0 if successful, -1 failure.
---------------------
Author: YPT_victory
Source: CSDN
Original: https: //blog.csdn.net/ypt523/article/details/79958188
Disclaimer: This article as a blogger original article, reproduced, please attach Bowen link!

 

reference:

https://blog.csdn.net/ypt523/article/details/79958188

https://www.cnblogs.com/LUO77/p/5816326.html

Guess you like

Origin www.cnblogs.com/vivid-zhang/p/11134900.html