ipcs

 

介紹

Different Types of IPCS

  • Pipes
  • Shared Memory (-m)
  • Message Queue (-q)
  • Semaphores (-s)

Pipes

Provides a way for processes to communicate with each another by exchanging messages.

Named pipes provide a way for processes running on different computer systems to communicate over the network.

Shared Memory

Processes can exchange values in the shared memory.

One process will create a portion of memory which other process can access.

Message Queue

It is a structured and ordered list of memory segments where processes store or retrieve data.

Semaphores

POSIX semaphores allow processes and threads to synchronize their actions.

A semaphore is an integer whose value is never allowed to fall below zero. 

Two operations can be performed on semaphores:

 - increment the semaphore value by one (sem_post(3));

 - decrement the semaphore value by one (sem_wait(3)). 

If the value of a semaphore is currently zero, then a sem_wait(3) operation will block until the value becomes greater than zero.

No data is passed with a semaphore; it simply coordinates access to shared resources.

POSIX semaphores come in two forms:

named semaphores

/somename (up to 251)

unnamed semaphores

An unnamed semaphore does not have a name. 

Instead the semaphore is placed in a region of memory that is shared between multiple threads or processes

Shared Memory

There are two things that control shared memory in a container.

1. The shmpages setting for this container (check UBC).

This dictates how many pages (one page is usually 4K, see memory page for more details) are available to the container,

e.g. shmpages=16384 gives a limit of 64 MB of shared memory.

2. "/sbin/sysctl kernel.shmmax" This is the container's self-imposed limit in bytes of how much shared memory may be allocated in a single request.

You may check the shared memory configuration with "ipcs -l".

The HN imposes a limit on the container's total shared memory usage through shmpages,

and the container itself imposes a limit on the container's total shared memory usage through kernel.shmmax.

If the size of shmpages is less than kernel.shmmax, the database will not be able to allocate sufficient memory.

目錄

  • ipcs - provide information on IPC facilities
  • ipcrm - remove a message queue, semaphore set or shared memory id
  • Settings
  • ipcmk - create shared memory segments, message queues or semaphore arrays.
  • ipcmd

 


ipcs

 

ipcs

ipcs [-a]             # all (this is the default)

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status
0x00000000 0          root       644        80         2
0x00000000 32769      root       644        16384      2
0x00000000 65538      root       644        280        2
0x00000000 196611     lightdm    600        524288     2          dest

------ Semaphore Arrays --------
key        semid      owner      perms      nsems
0x000000a7 65536      root       600        1
0x002fa327 98305      root       666        2

ipcs -l                   # -l     limits

------ Shared Memory Limits --------
max number of segments = 4096
max seg size (kbytes) = 262144
max total shared memory (kbytes) = 1073741824
min seg size (bytes) = 1

------ Semaphore Limits --------
max number of arrays = 128                # SEMMNI
max semaphores per array = 250            # SEMMSL
max semaphores system wide = 32000        # SEMMNS (一般是: SEMMNI x SEMMSL)
max ops per semop call = 32               # SEMOPM
semaphore max value = 32767

------ Messages: Limits --------
max queues system wide = 1460
max size of message (bytes) = 8192
default max size of queue (bytes) = 16384

ipcs -u                  # Show usage / status summary

------ Messages Status --------
allocated queues = 0
used headers = 0
used space = 0 bytes

------ Shared Memory Status --------
segments allocated 4
pages allocated 134
pages resident  14
pages swapped   0
Swap performance: 0 attempts     0 successes

------ Semaphore Status --------
used arrays = 2
allocated semaphores = 3

查看類別

  • -m     shared memory segments
  • -q      message queues
  • -s      semaphore arrays
  • -i       specific resource id to be specified.

Default: -a all (this is the default)

Last touch

# -p                                               # pid

# cpid = Creator PIDs, lpid = Last-op PIDs

ipcs -m -p

------ Shared Memory Creator/Last-op --------
shmid      owner      cpid       lpid
0          zabbix     1946       2535

ipcs -s -t                  # The time of the last control operation

------ Semaphore Operation/Change Times --------
semid    owner      last-op                    last-changed
0        root        Fri Jan 28 11:12:30 2022   Fri Jan 28 11:12:28 2022
32769    root        Fri Jan 28 11:12:32 2022   Fri Jan 28 11:12:31 2022
5373954  apache      Not set                    Fri Jan 28 11:58:16 2022
98307    zabbix      Fri Feb  4 12:16:28 2022   Fri Jan 28 11:12:52 2022
5406724  apache      Fri Jan 28 11:58:46 2022   Fri Jan 28 11:58:16 2022
11665413 apache      Not set                    Fri Jan 28 12:47:29 2022
11698182 apache      Fri Jan 28 12:48:00 2022   Fri Jan 28 12:47:29 2022
41811975 apache      Not set                    Fri Jan 28 16:42:58 2022
41844744 apache      Fri Jan 28 16:43:29 2022   Fri Jan 28 16:42:58 2022
...

 


Settings

 

/etc/sysctl.conf

# Increases the maximum number of message queue identifiers
# Default: 1460
kernel.msgmni = 512

# Semaphore limits
# 'ipcs -l'時的"Semaphore Limits"
# SEMMSL SEMMNS SEMOPM SEMMNI
# Default: 250 32000 32 128
kernel.sem = 250 128000 32 512

kernel.sem

  • semmsl:   semaphores per set
  • semmns:  total semaphores               # system wide (SEMMNS = SEMMSL x SEMMNI)
  • semopm:  maximum operations per semop call
  • semmni:   total semaphores sets

 


ipcrm 應用

 

Apache Error Log

No space left on device: AH00023: Couldn't create the ssl-cache mutex
AH00016: Configuration Failed

原因:

semaphores are used for communicating between the active processes of a certain application.

In the case of Apache, they’re used to communicate between the parent and child processes.

If Apache can’t write these things down, then it can’t communicate properly with all of the processes it starts.

i.e.

killall -9 httpd

解決方案:

[1]

/etc/sysctl.conf

kernel.msgmni = 512
kernel.sem = 250 128000 32 512

sysctl -p

[2]

ipcs -s                  # -s, --semaphores                     Write information about active semaphore sets.

------ Semaphore Arrays --------
key        semid      owner      perms      nsems
0x7a00c994 65536      zabbix     600        13
0x00000000 163841     apache     600        1
0x00000000 131074     apache     600        1

ipcrm -s N            # -s, --semaphore-id semid           Remove the semaphore identified by semid

Batch Job

ipcs -s | grep apache | awk '{ print $2 }' | xargs -n 1 ipcrm -s

Or

ipcs -s | awk -v user=apache '$3==user {system("ipcrm -s "$2)}'

ipcrm usage

# Remove by id

  • -m shmid
  • -s semid
  • -q msgid

 


ipcmk

 

allows you to create shared memory segments, message queues or semaphore arrays.

-M size             # shared memory segment of size bytes

-S nsems          # semaphore array with nsems elements

-Q                    # message queue

-p mode           # permission for the resource (default is 0644)

i.e.

ipcs -su

------ Semaphore Status --------
used arrays = 129
allocated semaphores = 141

ipcmk -S 32

Semaphore id: 1289355393

ipcs -su

------ Semaphore Status --------
used arrays = 130
allocated semaphores = 173

ipcs -s -i 1289355393

Semaphore Array semid=1289355393
uid=0    gid=0   cuid=0  cgid=0
mode=0644, access_perms=0644
nsems = 32
otime = Not set
ctime = Fri Feb  4 12:42:30 2022
semnum     value      ncount     zcount     pid
0          0          0          0          0
1          0          0          0          0
2          0          0          0          0
...
31         0          0          0          0

 


ipcmd

 

Command-line interface to XSI (SysV) semaphores and message queues

Tools: https://github.com/nathanweeks/ipcmd

Help: ipcmd SUBCOMMAND -h

 


 

 

 

Creative Commons license icon Creative Commons license icon