[Detailed explanation of the usage of Linux ipcs command and ipcrm command]

1. Knowledge about shared memory

The so-called shared memory is the common use of the same physical memory space among multiple processes, which is achieved by mapping the same physical memory to the virtual space of different processes. Since it is mapped to the virtual space of different processes, different processes can use it directly, and it does not need to be copied like a message queue, so the efficiency of shared memory is very high. Shared memory can be implemented by mmap() mapping ordinary file mechanism, or it can be implemented by System V shared memory mechanism. System V realizes shared memory communication between processes by mapping files in special file system shm, that is, each shared memory. The memory area corresponds to a file in the special file system shm.

Second, the principle of shared memory

System V shared memory puts all shared data in the shared memory area. Any process that wants to access the data must add a new memory area in the address space of the process to map the physical memory pages that store the shared data. System V shared memory obtains or creates an IPC shared memory area through the shmget function, and returns the corresponding identifier. The kernel ensures that shmget obtains or creates a shared memory area and initializes the corresponding shmid_kernel structure of the shared memory area. A file with the same name is created and opened in the file system shm, and the corresponding dentry and inode structure of the file is established in the memory. The newly opened file does not belong to any process, all of which are completed by the system calling the shmget function.

3. sysctl.conf configuration file

The above two paragraphs explain the content of the understanding found from the Internet. As a Linux system maintainer, the settings related to shared memory that can be accessed are mainly several configuration items in /etc/sysctl.conf. details as follows:

 

  1. kernel.shmmax = 4398046511104
  2. kernel.shmall = 1073741824
  3. kernel.shmmni = 4096

The following is a partial introduction to shared memory in a document for installing oracle provided by redhat6, as follows:

 



 

 

 

ipcs provides information on some inter-process communication methods on linux/uinx, including shared memory, message queues, and signals

ipcs usage 

ipcs -a is the default output information that prints information about all inter-process communication methods in the current system

ipcs -m prints information about using shared memory for interprocess communication

ipcs -q prints information about interprocess communication using message queues

ipcs -s prints information about interprocess communication using signals

Control of output format

Detailed change time of ipcs -t output information

ipcs -u Output status information of various ipc modes under the current system (shared memory, message queue, signal)

 

 

 

ipcrm command 

Remove a message object. Or a shared memory segment, or a signal set, and the data associated with the ipc object chain will also be removed. Of course, only the super administrator, or the creator of the ipc object, has this right.

ipcrm usage 

ipcrm -M shmkey removes the shared memory segment created with shmkey

ipcrm -m shmid removes the shared memory segment identified by shmid

ipcrm -Q msgkey remove message queue created with msqkey

ipcrm -q msqid removes the message queue identified by msqid

ipcrm -S semkey removes signals created with semkey

ipcrm -s semid removes signals identified by semid

 

 

 

Interprocess Communication Overview

Inter-process communication has the following purposes: 1. Data transmission, one process needs to send its data to another process, and the amount of data sent is between one byte and several M; 2. Shared data, multiple processes want to To operate shared data, other processes should see the modification of data by one process; 3. Notification events, a process needs to send a message to another or a group of processes to notify them that something has happened; 4. Resource sharing, many The same resources are shared between processes. In order to do this, the kernel needs to provide a lock and synchronization mechanism; 5. Process control, some processes want to completely control the execution of another process (such as the Debug process), and the controlling process hopes to intercept all traps and exceptions of another process. , and can know its state change in time.

Linux interprocess communication is developed from the following parts:

Early UNIX interprocess communication: including pipes, FIFOs, and signals.

System V-based inter-process communication: including System V message queue, System V semaphore (Semaphore), and System V shared memory.

基于Socket进程间通信。

基于POSIX进程间通信:包括POSIX消息队列、POSIX信号灯、POSIX共享内存。

Linux中,与IPC相关的命令包括:ipcs、ipcrm(释放IPC)、

 

 

具体的用法总结如下:

1、显示所有的IPC设施

# ipcs -a

 

2、显示所有的消息队列Message Queue

# ipcs -q

 

3、显示所有的信号量

# ipcs -s

 

4、显示所有的共享内存

# ipcs -m

 

5、显示IPC设施的详细信息

# ipcs -q -i id

id 对应shmid、semid、msgid等。-q对应设施的类型(队列),查看信号量详细情况使用-s,查看共享内存使用-m。

 

6、显示IPC设施的限制大小

# ipcs -m -l

-m对应设施类型,可选参数包括-q、-m、-s。

 

7、显示IPC设施的权限关系

# ipcs -c

# ipcs -m -c

# ipcs -q -c

# ipcs -s -c

 

8、显示最近访问过IPC设施的进程ID。

# ipcs -p

# ipcs -m -p

# ipcs -q -p

 

9、显示IPC设施的最后操作时间

# ipcs -t

# ipcs -q -t

# ipcs -m -t

# ipcs -s -t

 

10、显示IPC设施的当前状态

# ipcs -u

Linux上的ipcs命令,不支持UNIX上的-b、-o指令,同样UNIX中不支持-l、-u指令,所以在编写跨平台的脚本时,需要注意这个问题。

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326414613&siteId=291194637