IPCS and Linux Shared memory example

Source: Internet
Author: User
Tags message queue semaphore

Shared memory-related knowledge

Shared memory is the common use of the same piece of physical memory space between multiple processes, and it is achieved by mapping the same piece of physical memory to the virtual space of different processes. Shared memory is highly efficient because different processes can be used directly in virtual spaces that map to different processes and do not need to replicate like message queues. Shared memory can be implemented through the mmap () mapping common file mechanism, or System V shared memory mechanism, which system V implements shared memory communication between processes by mapping files in the special file system SHM. This means that each shared memory area corresponds to a file in the special file system SHM.

Second, the principle of shared memory

System v Shared memory places all shared data in a shared memory area, and any process that wants to access the data must add an area of memory to the address space of this process to map the physical memory page that holds the shared data. System v Shared memory obtains or creates an IPC shared memory area through the Shmget function, and returns the corresponding identifier, which initializes the corresponding Shmid_kernel structure of the shared memory area by guaranteeing shmget to obtain or create a shared memory area. A file with the same name will also be created and opened in the special file system SHM, and the corresponding dentry and inode structure of the file is built in memory, and the newly opened file does not belong to any process, all of which are done by the system calling the Shmget function.

Third, sysctl.conf configuration file

The above two paragraphs are part of the understanding that is found from the Internet. As a Linux system maintenance personnel, the access to the shared memory related settings are mainly in the/etc/sysctl.conf of several configuration items. Specifically as follows:

Kernel.shmmax = 4398046511104
Kernel.shmall = 1073741824
Kernel.shmmni = 4096
The following is a section on shared memory provided by REDHAT6 's official documentation on Oracle installation, as follows:

LINUX Shared Memory

The full version of the Redhat official document can be viewed here.

Note: It is important to note that there is also a shared in the output of the Free-m command, but by looking at multiple hosts, this is 0, and then looking for data to confirm that the shared memory in the free command is obsolete and useless. Therefore, the view of shared memory cannot be confirmed by this item.

Iv. IPCS and Ipcrm

IPCs

IPCS is a tool for displaying the state of interprocess communication facilities under Linux. You can display Message Queuing, shared memory, and semaphore information. is useful for programmers, and ordinary system administrators generally do not use this directive.

$ipcs-M to view the IPC shared memory resources used by the system
$ipcs-Q View IPC queue resources used by the system
$ipcs-S To view the IPC semaphore resources used by the system
$ipcs-L View System parameter configuration
When the default parameter is not added, the parameter used is-a (all, show All)
The output example is as follows:

# IPCS
------Message Queues--------
Key Msqid owner perms used-bytes messages
------Shared Memory Segments--------
Key shmid owner perms bytes nattch Status
0x6c04c831 294912 Zabbix 600 219056 6
0x0112be9b 458753 Root 600 1000 7
0x0112be9d 491522 Root 600 1200712 7
------Semaphore Arrays--------
Key Semid owner Perms Nsems
0x7a04c831 1245184 Zabbix 600 13
0x00000000 1802241 Apache 600 1
0x00000000 1835010 Apache 600 1
Ipcrm

Use the IPCRM command to clear the IPC resource: This command also removes the data associated with the IPC object. Of course, only the root user, or the creator of the IPC object, has this right;

Ipcrm-m Shmkey removing shared memory segments created with Shmkey
Ipcrm-m shmid Remove shared memory segments identified with Shmid
Ipcrm-q msgkey Remove message queues created with Msqkey
Ipcrm-q msqid Remove message queues identified with Msqid
Ipcrm-s Semkey removes a signal created with Semkey
Ipcrm-s Semid Remove the signal from the Semid logo
Examples of the resources that IPCS and IPCRM work with to clean up are as follows:

Ipcs-q | awk ' {print ' ipcrm-q ' $} ' | SH >/dev/null 2>&1;
ipcs-m | awk ' {print ' ipcrm-m ' $} ' | SH >/dev/null 2>&1;
Ipcs-s | awk ' {print ' ipcrm-s ' $} ' | SH >/dev/null 2>&1;
Other applications of IPCS

You can also use IPCS to verify that a user has a backlog of message queues:

1. Query Message Queue

$ipcs-Q
------message queues--------
key        msqid       owner      perms      used-bytes   Messages
0x49060005 58261504   user1    660        0            0
0x4f060005 58294273   user1     660        0             0
2, find queues with messages greater than 0

$ ipcs-q |grep user1 |awk ' {if ($5>0) print $} '
0x00000000 1071579324 User1 644 1954530 4826
0x00000000 1071644862 User1 644 1961820 4844
0x00000000 1071677631 User1 644 1944810 4802
0x00000000 1071710400 User1 644 1961820 4844
V. Why you need to manually free shared memory

In Linux, the shared memory created by API function Shmget is typically released using SHMCTL in the program, but sometimes in order to debug the program, the developer may send an interrupt signal through CTRL + C to end the program, and the shared memory that the program requests cannot be released. Of course, if the program does not change, rerun the program will still use the last requested shared memory, but if we modify the program, due to the size of shared memory and other reasons cause the program request shared memory error. Therefore, we always want to release the requested shared memory at the end of each session.

There are two ways to free shared memory:

The first: If you always end up with Crtl+c, you can do a signal processor, when you receive this signal, first release the shared memory, and then exit the program.

Second: Regardless of the way you end the program, if the shared memory is still not released, you can use the Linux command Ipcrm SHM shmid to release, the command can be used before using the IPCS-M command to view the 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.