ipc--Message Queuing bidirectional traffic

Source: Internet
Author: User

Message Queuing provides a way for a process to send chunks of data to another process, each of which is considered to have a type, and this type is represented by constants is_client_snd and IS_SERVER_SND.


The advantage of Message Queuing over pipelines is that it avoids blocking.


System Call Function:

    1. #include <sys/types.h>

      #include <sys/ipc.h>

Prototype: key_t ftok (const char* pathname,int proj_id);

Parameter: Pathname is the full path of an existing, available file (must be a file that already exists)

PROJ_ID for any low 8-bit non-0 number (because the low 8-bit to take it)

Ftok Algorithm

Return value: Successful return of a key value failed to return-1

2. #include <sys/types.h>

#include <sys/ipc.h>

#include <sys/msg.h>

Prototype: int msgget (key_t key,int MSGFLG);

Parameters: Key can be obtained by Ftok

Flag three values Ipc_creat ipc_excl ipc_creat| Ipc_excl

Ipc_creat creates a message queue and returns the identity of the queue, returning the identity directly if the queue already exists

IPC_EXCL itself does not mean much, usually used with ipc_creat

Ipc_creat| IPC_EXCL creates a message queue and returns a 1 error if the queue already exists, guaranteeing that the resource is new rather than open

Return value: Successfully returned queue identity _msg_id failed return-1

3. #include <sys/types.h>

#include <sys/ipc.h>

#include <sys/msg.h>

Receive message: ssize_t MSGRCV (int msgid, const void *msgp,size_t msgsz,int msgtyp,int msgflg);

Send message: int msgsnd (int msgid,const void *msgp,size_t msgsz,int msgflg));

Parameters: msg_id is the identity of the message queue, MSGP is a pointer to the message buffer (a common structure that can be developed by the user)

MSGSZ is the size of the message, Msftyp is the type of message taken from the queue, and 0 can fetch any message.

MSGFLG indicates that when there is no message in the message queue, 0 is blocked and ipc_nowait returns 1 directly;


The command to use:

IPCS-Q//viewing message queues present in the system

Ipcrm-q key value//delete message queue for the specified key value

//comm.h#ifndef _msg_queue_#define _msg_queue_#include< stdio.h> #include <sys/ipc.h> #include <sys/types.h> #include <stdlib.h> #include <sys/msg.h > #include <string.h> #define  _PATH_  "/tmp/.msg" #define  _proj_id_ 0x55#define _ Msg_size_ 1024extern const long is_server_snd;extern const long is_client_ snd;typedef struct _msg_info _msg_info;struct _msg_info{    int  mtype;    char mtext[_msg_size_];}; Static int comm_msg (Int flag); Int creat_msg_queue (); Int get_msg_queue ();int  Destroy (INT&NBSP;MSG_ID); #endif 
Comm.c#include "Comm.h" const long is_server_snd=1;const long is_client_snd=2;static  Int comm_msg (Int flag) {    key_t _key=ftok (_path_,_proj_id_);     if (_key<0) {        perror ("Ftok");         return -1;     }        int msg_id= msgget (_key,flag);     if (msg_id<0) {         perror ("Msgget");        return -1;      }       else         return msg_id;} Int creat_msg_queue () {    return comm_msg (ipc_creat| IPC_EXCL);} Int get_msg_queue () {    return comm_msg (ipc_creat);} Int destroy (int msg_id) {    return msgctl (msg_id,ipc_rmid,null);} 
Server.c#include "Comm.h" Int main () {    int _msg_id=creat_msg_queue ();     _msg_info msginfo;    while (1) {         msginfo.mtype=is_client_snd;        memset (Msginfo.mtext, ' (Msginfo.mtext),         if (MSGRCV (_msg_id,&msginfo, sizeof (Msginfo.mtext), is_client_snd,0) <0) {             perror ("MSGRCV");        }         else{            printf ("Client  say# %s ", Msginfo.mtext);        }         msginfo.mtype=is_server_snd;        memset ( Msginfo.mtext, ' Msgi ', sizeof (Nfo.mtext));         read (0,msginfo.mtext,sizeof (msginfo.mtext));         if (Msgsnd (_msg_id,&msginfo,sizeof (Msginfo), 0) <0) {             perror ("msgsnd");         }    }    if (Destory (_msg_id)!=0) {         perror ("destory failed!\n");    }     else        printf ("destory success!\n");     return 0;} Client.c#include "Comm.h" Int main () {    int _msg_id= get_msg_queue ();     _msg_info msginfo;    while (1) {         memset (Msginfo.mtext, ' n ', sizeof (Msginfo.mtext));        & NBsp;msginfo.mtype=is_client_snd;        if (Read (0,msginfo.mtext,sizeof (Msginfo.mtext)) >=0) {            if (msgsnd (_msg_id,&msginfo , sizeof (Msginfo.mtext), 0) <0) {                 perror ("msgsnd");             }         }        memset ( Msginfo.mtext, ' _msg_id ', sizeof (Msginfo.mtext)),         if (MSGRCV, &msginfo,sizeof (Msginfo), is_server_snd,0) <0) {             perror ("MSGRCV");        }         else{            printf (" server  say# %s ", Msginfo.mtext);        }    }        return 0; }      //makefile.phony: allall:server clientserver:server.c comm.c    gcc -o server  Server.c comm.cclient:client.c comm.c    gcc -o client client.c  comm.c.phony:cleanclean:    rm -rf server client


This article is from the "Zero Egg" blog, please be sure to keep this source http://lingdandan.blog.51cto.com/10697032/1763140

ipc--Message Queuing bidirectional traffic

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.