Inter-process communication

A process alone can do a certain things, but not everything. It will be a very useful and good resource utilization if two or more processes can communicate with each other in the form of sharing results, sending or receiving messages, and so on. In a Linux or Unix-based operating system, two or more processes can communicate with each other using IPC.

IPC is the technique by which processes communicate with each other and are managed by kernel.

IPC is possible to do by any of the following ways:

  • Named pipes: These allow processes to read from and write into it.
  • Shared memory: This is created by one process and is further available for read from and write to this memory by multiple processes.
  • Message queue: This is a structured and an ordered list of memory segments where processes store or retrieve data in queue fashion.
  • Semaphores: This provides a synchronizing mechanism for processes that are accessing the same resource. It has counters that are used to control the access to shared resources by multiple processes.

While discussing named pipes in Chapter 6, Working with Files, we learned how processes can communicate using named pipes.

Information on IPC using ipcs

The ipcs command provides information about IPC facilities for which a calling process has the read access. It can provide information on three resources: shared memory, message queue, and semaphore.

The syntax of using ipcs is as follows:

ipcs option

Where options are as follows:

Option

Description

-a

Displays information for all resources—shared memory, message queue, and semaphore

-q

Displays information about active message queues

-m

Displays information about active shared memory segments

-s

Displays information about active semaphore sets

-i ID

Shows the detailed information for an ID. Use it with the -q, -m or -s option.

-l

Shows resource limits

-p

Shows PIDs of the resource creator and last operator

-b

Prints sizes in bytes

--human

Print sizes in a human-readable format

Listing information provided by IPCs

We can use the ipcs command without an option or with –a:

$ ipcs

OR

$ ipcs -a
Listing information provided by IPCs

To see only the shared memory segment, we can use ipcs with the –m option:

$ ipcs -m --human
Listing information provided by IPCs

Here, the --human option made a size column in a more readable format by providing the size in KB and MB instead of giving it in bytes.

To find out detailed information about a resource ID, use ipcs with the -i option followed by the resource ID:

$ ipcs -m -i 393217
Listing information provided by IPCs

Knowing processes' PID who recently did IPCs

We can know the PID of the processes that have recently accessed a specific IPC resource using the -p option:

$ ipcs -m -p
Knowing processes' PID who recently did IPCs

Here, the cpid column shows pid of the processes that created the shared memory resource, and lpid refers to the PID of the processes that last accessed the shared memory resource.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.75.169