"Linux program" XSI IPC

Source: Internet
Author: User
Tags message queue semaphore

three kinds of IPC this is called the XSI IPC, each of the rooms:
    • Message Queuing
    • Signal Volume
    • Shared memory
The following sections describe the use of three IPC methods.
1. Message Queuing Message Queuing is a linked table of messages, with the following function interfaces, for example:
    • Msgget: Creates a new queue or opens an existing queue.
    • MSGSND: Joins the message to the end of the queue.
    • MSGRCV: Fetching messages from the queue.
We are able to define a struct that represents a message, which consists of a type field and the actual data:
struct mest_t {    long type;          Message type    char text[512];     Message content};


There is a message type. When we use the MSGRCV function to get the message, we do not have to be in the FIFO order, but we can take the message according to the message type. Here is a simple test code:
#include <stdio.h> #include <sys/msg.h> #include <sys/types.h> #include <string.h> #include    <errno.h> struct mest_t {long type; Char text[512];};    int main (void) {pid_t pid;    int mq_id;     struct mest_t msg; /* Ipc_private used to create a new queue * Set the IPC_EXCL and set the Ipc_creat. Return error when file exists */mq_id = Msgget (ipc_private, Ipc_creat |    IPC_EXCL);  if (mq_id = = eexist) return-1;    /* Return eexist indicates IPC already exists */if ((PID = fork ()) < 0) return-1;                     else if (PID = = 0) {/* child process */Msg.type = 123;   /* Message type */strcpy (Msg.text, "Hello world!");  /* Message content */* Non-blocking method put message into message queue * Queue is full return Eagain */while (MSGSND (mq_id, (long *) &AMP;MSG, 512,    ipc_nowait) = = Eagain) sleep (1); } else {/* non-blocking method fetching messages from the queue * assumes there are no messages of the specified type.            The function returns -1,errno set to Enomsg */while (MSGRCV (mq_id, (long *) &msg, +, 123, ipc_nowait) = =-1) { if (errno = = enomsg) {printf ("There is no this type message!\n");            Sleep (1);    }} printf ("%s\n", Msg.text); } return 0;}


When the parent process needs to take the same message type and the child process sends the same message type. The results of the execution are as follows:

The parent process can receive messages that are sent to the message queue very quickly by the child process.

However, after changing the MSGRCV message type parameters, the result of the execution is as follows:

The parent process does not get the desired message, and the error message is printed all the time.
2, Semaphore signal volume is a counter. Used for multi-process access to shared data objects. Steps such as the following:

    • Test to control the semaphore of the resource.
    • If the value of this semaphore is positive. The process is able to use the resource. The process reduced the semaphore value by 1, indicating that it used a resource unit.
    • If the value of this semaphore is 0. The process goes into hibernation until the semaphore is greater than 0. After the process is awakened. It returns to the first step.
When a process no longer uses a shared resource controlled by a semaphore, the semaphore value increases by 1. Assume that a process is sleeping waiting for this semaphore to wake them up.
3. Shared storage shared storage agrees that multiple processes share a given storage area. Is the fastest kind of IPC. Test code such as the following:
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/shm.h> #define Shm_ SIZE 100#define Shm_mode (Shm_w | Shm_r |  ipc_creat) int main () {    int shmid;    char *shmptr;    pid_t pid;     /* Get the Shared storage identifier *    /if ((Shmid = Shmget (Ipc_private, Shm_size, Shm_mode)) < 0)        return-1;     if (PID = fork ()) < 0)        return-1;    else if (PID = = 0)    {        shmptr = Shmat (shmid, 0, 0);    /* 2 for 0 indicates the shared space is allocated by the kernel *        /printf ("Child attached shared memory is:%lx\n", (unsigned long) shmptr);        SHMDT (shmptr);                  /* Leave the process out of the space *    /} else    {        waitpid (PID, NULL, 0);         Shmptr = Shmat (shmid, 0, 0);        printf ("Parent attached shared memory is:%lx\n", (unsigned long) shmptr);        SHMDT (shmptr);         Shmctl (Shmid, Ipc_rmid, 0);  /* Delete the shared storage segment */    }     return 0;}


Execution Result:

As can be seen from the experimental results above, the parent and child processes share the same bucket.

It is important to note that the SHMDT function simply keeps the process out of the shared bucket, but the bucket still exists and shmid is still valid. It corresponds to the Shmat. There is also a function shmctl to actually delete the shared segment when the Ipc_rmid parameter is used.
The mmap functions in shared bucket and storage map I/O are similar, and the difference between them is that the mmap mapped bucket is associated with a file, and XSi shared bucket has no such association.
Reference: "UNIX Programming Environment" p415-p432.

"Linux program" XSI IPC

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.