Linux System V IPC 消息队列

Linux System V IPC 消息队列

模型:

  1. 获取key值 :ftok()
  2. 创建/获取消息队列 :msgget()
  3. 发消息到消息队列/从消息队列收信息 :msgsnd()/msgrcv()
  4. 删除消息队列 :msgctl()

使用的头文件

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

ftok()

//获取key值, key值是System V IPC的标识符,成功返回key,失败返回-1设errno
//同pathname+同 proj_id==>同key_t;
key_t ftok(const char *pathname, int proj_id);

pathname :文件名
proj_id: 1~255的一个数,表示project_id

key_t key=ftok(".",100);    //“.”就是一个存在且可访问的路径, 100是假设的proj_id
    if(-1==key)
        perror("ftok"),exit(-1);

msgget()

//创建/获取消息队列,成功返回shmid,失败返回-1
int msgget(key_t key, int msgflg);  //ATTENTION:用int msqid=msgget()比较好看

msgflg:具体的操作标志

  • IPC_CREAT 若不存在则创建, 需要在msgflg中"|权限信息"; 若存在则打开
  • IPC_EXCL若存在则创建失败
  • 0 获取已经存在的消息队列

消息队列的容量由msg_qbytes控制,在消息队列被创建的过程中,这个大小被初始化为MSGMNB,这个限制可以通过msgctl()修改

int msqid=msgget(key,IPC_CREAT|IPC_EXCL|0664);
if(-1==msqid)
    perror("msgget"),exit(-1);

msgsnd()

//向指定的消息队列发送指定的消息,如果消息队列已经满了,默认的行为是堵塞,直到队列有空间容纳新的消息,成功返回0,失败返回-1设errno
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);

msqid msgget()返回的消息队列的ID
msgp消息的的首地址, 消息的参考数据类型如下

struct msgbuf {
    long mtype;       /* message type, must be > 0 */   //消息的类型
    char mtext[1];    /* message data */                //消息的内容
};
ATTENTION:The  mtext field is an array (or other structure) whose size is specified by msgsz, a nonnegative integer value. 

msgsz消息的大小, 该参数用于指定消息内容的大小, 不包括消息的类型。只能sizeof(Msgbuf.mtext),不能sizeof(Msgbuf)
msgflg发送的标志, 默认给0即可

Msg msg1={1,"hello"};//消息的类型是1,内容是hello
int res=msgsnd(msqid,&msg2,sizeof(msg2.buf),0);
if(-1==res)
    perror("msgsnd"),exit(-1);

msgrcv()

//向指定的消息队列取出指定的消息,成功返回实际接受到的byte数,失败返回-1设errno
ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);

msqid: 消息队列的ID(returned by msgget)
msgp: 存放接收到消息的缓冲区首地址
msgsz: 消息的最大大小, 不包括消息的类型==>只能sizeof(Msgbuf.mtext),不能sizeof(Msgbuf)

  • 如果消息的长度>msgsz且msgflg里有MSG_NOERROR,则消息会被截断,被截断的部分会丢失
  • 如果消息的长度>msgsz且msgflg里没有MSG_NOERROR,那么会出错,报E2BIG。

msgtyp: 消息的类型

  • 0:读取消息队列中的第一个消息
  • >0:读取消息队列中第一个type为msgtype的消息, 除非msg_flg里有MSG_EXCEPT,此时队列中的第一个不是msgtyp的消息会被读取
  • <0读取消息队列中type<=|msgtype|的消息, 这里再优先读取最小的

msgflg: 发送的标志, 默认给0即可

Msg msg1;
int res=msgrcv(msqid,&msg1,sizeof(msg1.buf),1,0);
if(-1==res)
    perror("msgrcv"),exit(-1);

msgctl()

// 消息操作,成功返回0,失败返回-1设errno
int msgctl(int msqid, int cmd, struct msqid_ds *buf);

msqid :消息队列的ID,由msgget()
buf 结构体指针

struct msqid_ds {
    struct ipc_perm msg_perm;       /* Ownership and permissions */
    time_t          msg_stime;      /*Time of last msgsnd(2) */
    time_t          msg_rtime;      /* Time of last msgrcv(2) */
    time_t          msg_ctime;      /* Time of last change */
    unsigned long   __msg_cbytes;   /* Current number of bytes in queue (nonstandard) */
    msgqnum_t       msg_qnum;       /* Current number of messages in queue */
    msglen_t        msg_qbytes;     /* Maximum number of bytes allowed in queue */
    pid_t           msg_lspid;      /* PID of last msgsnd(2) */
    pid_t           msg_lrpid;      /* PID of last msgrcv(2) */
};
struct ipc_perm {
    key_t           __key;      /* Key supplied to msgget(2) */
    uid_t           uid;        /* Effective UID of owner */
    gid_t           gid;        /* Effective GID of owner */
    uid_t           cuid;       /* Effective UID of creator */
    gid_t           cgid;       /* Effective GID of creator */
    unsigned short  mode;       /* Permissions */
    unsigned short  __seq;      /* Sequence number */
};

cmd

  • IPC_STAT从内核相关结构体中拷贝消息队列相关的信息到buf指向的结构体中
  • IPC_SET把buf指向的结构体的内容写入到内核相关的结构体中,同时更显msg_ctimer成员,同时以下成员也会被更新:msg_qbytes, msg_perm.uid, msg_perm.gid, msg_perm.mode。调用队列的进程的effective UID必须匹配队列所有者或创建者的msg_perm.uid或msg_perm.cuid或者该进程拥有特权级别,
  • IPC_RMID立即销毁消息队列,唤醒所有正在等待读取或写入该消息队列进程,调用的进程的UID必须匹配队列所有者或创建者或者该进程拥有足够的特权级别
  • IPC_INFO (Linux-specific)返回整个系统对与消息队列的限制信息到buf指向的结构体中

    //_GNU_SOURCE
    //<sys/msg.h>
    struct msginfo {
        int msgpool;/*Size in kibibytes of buffer pool used to hold message data; unused within kernel*/
        int msgmap; /*Maximum number of entries in message map; unused within kernel*/
        int msgmax; /*Maximum number of bytes that can be written in a single message*/
        int msgmnb; /*Maximum number of bytes that can be written to queue; used to initialize msg_qbytes  during queue creation*/
        int msgmni; /*Maximum number of message queues*/
        int msgssz; /*Message segment size; unused within kernel*/
        int msgtql; /*Maximum number of messages on all queues in system; unused within kernel*/
        unsigned short int  msgseg; /*Maximum number of segments; unused within kernel*/
    };
    int res=msgctl(msqid,IPC_RMID,NULL);
    if(-1==res)
    perror("msgctl"),exit(-1);

例子

//Sys V IPC msg
#include<sys/types.h>
#include<sys/ipc.h>
#include<signal.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct{
    long mtype;     //消息的类型
    char buf[20];       //消息的内容
}Msg;
int msqid;          //使用全局变量,这样就可以在fa中使用msqid了
void fa(int signo){
    printf("deleting..\n");
    sleep(3);
    int res=msgctl(msqid,IPC_RMID,NULL);
    if(-1==res)
        perror("msgctl"),exit(-1);
    exit(0);
}
int main(){
    //ftok()
    key_t key=ftok(".",150);
    if(-1==key)
        perror("ftok"),exit(-1);
    printf("key%#x\n",key);
    //msgget()
    msqid=msgget(key,IPC_CREAT|IPC_EXCL|0664);
    if(-1==msqid)
        perror("msgget"),exit(-1);
    printf("msqid%d\n",msqid);
    //msgsnd()
    Msg msg1={1,"hello"};//消息的类型是1,内容是hello
    Msg msg2={2,"world"};
    int res=msgsnd(msqid,&msg2,sizeof(msg2.buf),0);
    if(-1==res)
        perror("msgsnd"),exit(-1);
    res=msgsnd(msqid,&msg1,sizeof(msg1.buf),0);
    if(-1==res)
        perror("msgsnd"),exit(-1);
    //msgctl()
    //Ctrl+C delete msq
    printf("Press CTRL+C to delete msq\n");
    if(SIG_ERR==signal(SIGINT,fa))
        perror("signal"),exit(-1);
    while(1);
    return 0;
}

猜你喜欢

转载自www.linuxidc.com/Linux/2016-10/135807.htm