Mastering Linux IPC Creation with the ipcmk Command

Overview

When testing or administering inter-process communication (IPC) on a Linux host, ipcmk provides a straightforward way to allocate resources directly from the shell. This walkthrough demonstrates how to provision shared memory segments, message queues, and semaphore arrays while tailoring their access permissions. You will also see how ipcmk pairs with ipcs and ipcrm to form a complete lifecycle—from inspecting active IPC objects to tearing them down cleanly.

Checking the Tool Version, Options, and Baseline IPC State

Start by verifying which release of ipcmk is installed on the machine with ipcmk -V.
The returned string, util-linux 2.39.3, confirms that ipcmk ships as part of the standard util-linux suite.

Checking the ipcmk version

Next, inspect the built-in usage summary and flags by running ipcmk -h.
Whenever you need to spin up System V IPC primitives—shared memory, semaphore sets, or message queues—from the command line, ipcmk handles the allocation using the following options:
⚙️ [Key Options]

-M, --shmem <size> : Allocates a shared memory segment of the specified byte size.
-S, --semaphore <number> : Creates a semaphore array containing the given number of elements.
-Q, --queue : Initializes a new message queue.
-p, --mode <mode> : Sets the octal permission mode on the created object (defaults to 0644).
-h, --help : Displays usage instructions and option descriptions.
-V, --version : Prints the utility version and exits.

Displaying ipcmk help options

Before creating any objects, run ipcs to audit the system’s existing IPC table.
Because the message queue, shared memory, and semaphore sections all return empty, you have a clean slate for the exercises ahead.

Verifying an empty baseline with ipcs

Provisioning Shared Memory and Configuring Permissions

To allocate a 4096-byte shared memory segment, execute ipcmk -M 4096.
Omitting the permission flag leaves the default 0644 mode in place, and the terminal prints Shared memory id: 0 to identify the new segment.

Creating a 4096-byte shared memory segment

If an application requires stricter isolation, pass -p alongside the size—for example, ipcmk -M 8192 -p 0600 allocates an 8192-byte segment readable and writable solely by its owner (0600).
Upon completion, the command outputs Shared memory id: 1 for this restricted segment.

Creating an 8192-byte shared memory segment with 0600 permissions

Verify both allocations by listing active shared memory entries with ipcs -m.
In the table, shmid 0 appears with 4096 bytes and mode 644, whereas shmid 1 shows 8192 bytes and mode 600, matching the requested parameters.

Listing shared memory segments with ipcs -m

Creating Message Queues and Semaphore Arrays

You can initialize a message queue in a single step by running ipcmk -Q.
Without an explicit mode flag, the kernel assigns the default 0644 permissions and reports Message queue id: 0.

Creating a message queue with ipcmk -Q

To set up a semaphore array containing 5 elements with group-accessible read/write rights (0660), run ipcmk -S 5 -p 0660.
The system allocates the array and returns Semaphore id: 0.

Creating a 5-element semaphore array with 0660 permissions

Inspect the message queue subsystem using ipcs -q.
The listing displays msqid 0 with mode 644, while both used-bytes and messages remain at 0 since no data has been enqueued yet.

Checking message queues with ipcs -q

Similarly, query the semaphore table with ipcs -s.
Here, semid 0 is registered under mode 660 with nsems set to 5, confirming the requested array size.

Checking semaphore arrays with ipcs -s

Auditing All IPC Objects and Scripting Cleanup

Running ipcs -a consolidates every active IPC facility into a single view.
At a glance, you can verify that msqid 0, shmid 0 and 1, and semid 0 are all live in the kernel tables.

Listing all IPC resources with ipcs -a

In automated scripts, you often need to capture the newly assigned ID for later teardown. Running SHM_ID=$(ipcmk -M 1024 | awk '{print $NF}') && echo "Created SHM ID: $SHM_ID" creates a 1024-byte test segment, extracts the trailing identifier field, and stores it in SHM_ID.
The output Created SHM ID: 2 confirms that ID 2 was captured in the shell variable.

Capturing a new shared memory ID in a shell variable

Confirm the new allocation by running ipcs -m.
Below segments 0 and 1, shmid 2 now appears with a 1024-byte allocation and 644 permissions.

Verifying the newly created shared memory segment

Finally, pass the saved variable to ipcrm and re-check the table with ipcrm -m "$SHM_ID" && ipcs -m.
Segment shmid 2 vanishes from the output, leaving only segments 0 and 1 intact.

Removing the test shared memory segment with ipcrm

With ipcmk, ipcs, and ipcrm in your toolkit, you can rapidly prototype, inspect, and clean up Linux IPC primitives right from the terminal. Try running these commands in your own test environment to build familiarity with shared memory, message queues, and semaphores.