Destroying IPC Resources

IPC resources can outlive your process. When a process terminates for any reason, all files are closed and its shared memory is detached, but its IPC resources will continue to exist. If IPC resources are no longer required, they must be explicitly destroyed.

There are system calls to perform this function:

  • msgctl(3) for message queues

  • semctl(2) for semaphores

  • shmctl(2) for shared memory

The following chapters cover the specifics of these operations. There are, however, some system-wide implications of destroying IPC resources that should be noted here.

When a message queue or a semaphore set is destroyed, they are destroyed immediately. Since IPC resources are not opened like files, they do not stay open until closed. When a message queue or semaphore set is destroyed, the UNIX kernel immediately discards them. If a message queue or semaphore operation is subsequently attempted on the destroyed IPC ID, the error EIDRM is returned.

Note

EIDRMIdentifier removed This error is returned when an IPC ID is used in an IPC operation after the resource has been destroyed.


Shared memory is handled differently. When shared memory is used, it must be attached to the current process at a specific address. When the shared region is no longer required, or when the process terminates, the shared region is detached from the current process. Due to this behavior, when a process destroys shared memory, the shared memory region exists until the last process detaches it.

The following general points can now be summarized:

  • IPC resources exist until destroyed.

  • Message queues and semaphores are immediately destroyed.

  • When shared memory is destroyed, its destruction occurs when the last process detaches from it.

Note

Under many operating systems, the IPC resources discussed in this chapter are optional. They are available only if they are configured or compiled into the kernel.

Additionally, note that IPC resources normally have system-configured limits for the number of messages queued, the maximum size of a message, the maximum number of semaphores in a set, the maximum amount of shared memory available, and so on.

Check your system documentation to find out how to configure these values to suit your application needs.


Tip

When you are debugging programs that fail to destroy IPC resources when they should, use the ipcs(1) command to display the resources and the ipcrm(1) command to remove them.


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

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