Shared memory is the memory that may be accessed by multiple processes; i.e. a memory region that can be shared between different processes and a better way of passing data between two processes. Shared memory is the fastest form of Inter-Process Communication which is currently available.
Assume that the program will create a memory portion, which another process can access (if permitted). A shared segment can be attached multiple times by the same process. Whenever the memory is mapped into the address space of the process, i.e. sharing the common memory region, the kernel will not involve while passing data between the processes. Many applications, like Oracle SGA requires shared memory settings, use this feature.
Let's check some IPCS command through some examples.
Print active shared memory segments using
"dest" status means memory segment is marked to be destroyed
"nattach" field shows how many application pid's are still attached to the shared memory
# ipcs -m ------ Shared Memory Segments -------- key shmid owner perms bytes nattch status 0x00000000 65536 root 600 393216 2 dest 0x00000000 98305 root 600 393216 2 dest
The following command print information about active shared memory segments:
# ipcs -q ------ Message Queues -------- key msqid owner perms used-bytes messages
Print information about active shared memory queues:
# ipcs -s ------ Semaphore Arrays -------- key semid owner perms nsems
Print information about semaphores that is accessible semaphores. The ipcs -l shows limit of shared memory, semaphores and messages.
# ipcs -l ------ Shared Memory Limits -------- max number of segments = 4096 max seg size (kbytes) = 4194303 max total shared memory (kbytes) = 1073741824 min seg size (bytes) = 1 ------ Semaphore Limits -------- max number of arrays = 128 max semaphores per array = 250 max semaphores system wide = 32000 max ops per semop call = 32 semaphore max value = 32767 ------ Messages: Limits -------- max queues system wide = 16 max size of message (bytes) = 65536 default max size of queue (bytes) = 65536
The below command shows the maximum size of the single memory segment that Linux process can allocate in its virtual address space. You can limit the maximum size of the single memory segment by executing following command.
# cat /proc/sys/kernel/shmmax 4294967295
You can set shmmax value by echoing to the concerned /proc file as below. The following command will set maximum size (in terms of bytes) single memory segment is set to 8388698:
# echo 8388608 > /proc/sys/kernel/shmmax
In a similar fashion, you can set the maximum allowable size of any single message in a System V IPC message queue, in bytes.
# echo 8192 > /proc/sys/kernel/msgmax
You can check the current kernel parameter for semaphore with the following command.
# cat /proc/sys/kernel/sem 250 32000 32 128 where max number of arrays = 128 max semaphores per array = 250 max semaphores system wide = 32000 max ops per semop call = 32 semaphore max value = 32767
In this tutorial, we learned Linux ipcs command to check shared memory information. I hope you enjoyed reading and please leave your suggestions in the below comment section.