POSIX IPC Basic Concepts

Source: Internet
Author: User
Tags message queue posix semaphore

Note: Only for learning Exchange, reprint please specify the source

First, the preface

There are certain limitations to using signals and pipelines for process communication. For example, the use of signal transmission of information is limited, although the use of pipelines can transmit a certain amount of information, but can only transfer unformatted byte stream. To address these issues, in the 1970s, the System V version of Unix, released by At&t, introduced a new interprocess communication (IPC) mechanism in 3, namely Message Queuing, shared memory, and semaphores. In the POSIX standard, these inter-process communication mechanisms are programmed into posix:xsi. Linux systems support the POSIX standard and naturally support these (IPC) mechanisms.

Two, Basic concepts

1. Introduction to Message Queues

As the name implies, Message Queuing refers to the queue where messages are stored. A message is information that has a message type and data that is stored in a predefined message structure. Message types can be private (only for processes that create message queues and their child processes), or they can be shared (accessible to all processes in the system). For other processes in the system to access, a unique identifier must be used, which is called the key of the message in the message queue. Different processes implement data exchange between processes by writing messages to message queues or by reading messages.

2, Signal Volume introduction

The semaphore is used for process synchronization problems in multiple process situations. A semaphore is a resource that contains an integer value that the process uses to detect to ensure that other processes do not perform similar operations at a certain time.

3. Introduction to Shared Memory

In a Linux system, each process runs in its own virtual address space. The address space between processes is not accessible to other processes. Shared memory enables the sharing of resources and data by establishing a memory space that allows access to other processes. The process can read and write to the shared memory space. When multiple processes write at the same time, you need to use semaphores to control access to resources.

The 3 kinds of IPC communication mechanisms provided by POSIX IPC can only be used for process communication between native machines. To implement process communication between different hosts, you can use such methods as socket sockets or RPC.

Third, IPC Resources

Message Queuing, semaphores, and shared memory are all IPC resources and need to be created before the IPC resource is used. Similar to the information in file properties, such as file owners, access permissions, IPC resources are similar to properties. These properties are stored in the structure body ipc_perm, containing information such as the IPC keyword, the owner of the IPC resource, and the same group of users.

struct IPC_PERM

{

__kernel_key_t key; Key words

__kernel_uid_t uid; Owner ID

__kernel_gid_t GID; Owner-owned Group ID

__kernel_uid_t cuid; Creator ID

__kernel_gid_t Cgid; Group ID to which the creator belongs

__kernel_mode_t mode; Access rights

Unsignedshort seq;

};

Four, IPC identifiers and keywords

In the Linux kernel, an identifier for a non-negative integer is used in order to identify the IPC resource. An identifier-related IPC resource, such as Message Queuing, semaphore, or shared memory, is accessible as long as the identifier is used. This identification is called an IPC identifier.

To create an IPC identifier, you need to specify a keyword. The IPC keyword (__key in the ipc_perm structure) can be obtained by invoking the Ftok function. The specific information for the Ftok function is shown in the following table:

Ftok function

Header file

#include <sys/types.h>

#include <sys/ipc.h>

Function prototypes

key_t Ftok (const char *pathname, int proj_id);

return value

Success

Failed

Whether to set errno

The resulting IPC keyword

-1

Is

Description: The Ftok function converts the given pathname and proj_id to the IPC keyword. Parameter pathname must point to a file or directory that exists in the file system.

Error message:

Eacces: No permission to enter the directory in pathname.

Eloop: There is a loop when parsing pathname.

The enametoolong:pathname length exceeds the Path_max limit, or the length of a directory in the path exceeds the Name_max limit.

A directory in the Enoent:pathname is empty.

The Enotdir:pathname part is included in the table.

Instance:

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>

int main (void)
{
    key_t key;
    int proj_id;
    Char *pathname = "./program";
    
    proj_id = 1;
    
    Key = Ftok (pathname, proj_id);
    if (key = = 1)
    {
        perror ("Cannot generate the IPC key");
        return (1);
    }        
    
    printf ("proj_id =%d pathname =%s IPC key =%d\n", proj_id, pathname, key);
    
    PROJ_ID = 2;
    Key = Ftok (pathname, proj_id);
    if (key = = 1)
    {
        perror ("Cannot generate the IPC key");
        return (1);
    }
    
    printf ("proj_id =%d pathname =%s IPC key =%d\n", proj_id, pathname, key);
    
    
    return (0);
        
}    
Run Result:
[root@localhost test]#./ftok 
proj_id = 1 pathname =./program IPC key = 16912503 proj_id
= 2 Pathnam e =./program IPC key = 33689719


Five, Basic IPC command

To facilitate interprocess communication using the IPC communication mechanism, commands for viewing and deleting IPC resources are also defined in Posix:xsi. The IPCS command provides a way to view the IPC resources stored in the kernel, and IPCRM is used to remove the specified IPC resource from the kernel.

1, IPCS command

The IPCS command displays resource information related to Posix:xsi interprocess communication in the system. IPCS commands commonly used parameters include Q (Display Message Queuing information), s (display semaphore information), and M (display shared memory information). If you execute the IPCS command without any parameters, information about Message Queuing, semaphores, and shared memory will be displayed.

2, Ipcrm command

The POSIX standard also provides a system call to remove an IPC resource from the kernel, using IPCRM to manually remove the IPC resource.

The various usage parameters of IPCRM can be learned from the help information, as shown in the following table:

Parameters

Description

Parameters

Description

-Q msqid

Remove Msqid Message Queuing resources for a specified value

-Q MSKey

Remove the Message Queuing resource for key MSKey

-M Shmid

Remove Shmid Shared memory resource for a specified value

-M Shmkey

Remove the shared memory resource with key as Shmkey

-S Semid

To remove a semaphore resource that Semid is a specified value

-S Semkey

Remove the semaphore resource with key as Semkey

In practical use, often need to IPCS and Ipcrm together use. The specific steps to remove the Message Queuing resources in the system are as follows:

(1): Use IPCS to view the IPC resources in the system

(2): Deletes the specified resource using IPCRM according to the MSQID or key information displayed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.