System Vメッセージキューの概要

4994 ワード

System Vメッセージキューは、メッセージキュー識別子を用いる識別され、カーネル内の各メッセージキューについて、カーネルはヘッダファイルの情報構造.
struct msqid_ds   {     struct msqid_ds {     struct ipc_perm msg_perm;     struct msg *msg_first;      /* first message on queue,unused  */    struct msg *msg_last;      /* last message in queue,unused */    __kernel_time_t msg_stime;  /* last msgsnd time */    __kernel_time_t msg_rtime;  /* last msgrcv time */    __kernel_time_t msg_ctime;  /* last change time */    unsigned long  msg_lcbytes;/* Reuse junk fields for 32 bit */    unsigned long  msg_lqbytes;/* ditto */    unsigned short msg_cbytes;  /* current number of bytes on queue */    unsigned short msg_qnum;    /* number of messages in queue */    unsigned short msg_qbytes;  /* max number of bytes on queue */    __kernel_ipc_pid_t msg_lspid;  /* pid of last msgsnd */    __kernel_ipc_pid_t msg_lrpid;  /* last receive pid */
};
msgget関数:
新しいメッセージキューを作成するか、既存のメッセージキューにアクセスします.
#include
#include
#include
int msgget(key_t key, int msgflg);
keyはftokによる戻り値であっても、定数IPC_であってもよいPRIVATE.oflag値は読み書き権限値の組合せであり、IPC_CREATとIPC_EXCLはビットまたは.
msgsndとmsgrcv関数:
メッセージキューを開いたら、この関数を使用してメッセージを配置し、メッセージを読み込みます.
       #include
       #include
       #include
       int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
       ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,
                      int msgflg);
msgflgは0でもIPCでもよいNOWAIT.
typeのオプション:
0の場合、キューの最初のメッセージに戻ります.
0より大きい場合、タイプ値typeの最初のメッセージが返されます.
0未満の場合、タイプ値がtypeパラメータの絶対値より小さいメッセージのタイプ値が最も小さいことを返します.
msgctl関数:
1つのメッセージ・キュー上でさまざまな制御操作を提供
       #include
       #include
       #include
       int msgctl(int msqid, int cmd, struct msqid_ds *buf);
例:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <errno.h>

int main(int argc, char **argv)
{
    int c, oflag, mqid;
    oflag = IPC_CREAT | 00666;

    while ((c = getopt(argc, argv, "e")) != -1) {
        switch(c) {
            case 'e':
                oflag |= IPC_EXCL;
                 break;
        } 
    }
    printf("the optind is %d, the argc is %d
", optind, argc); if (optind != argc - 1) { printf("usage: msgcreate [-e] <pathname>"); exit(0); } printf("argv[optind] is %s
",argv[optind]); if ((mqid = msgget(ftok(argv[optind], 0), oflag)) < 0); { perror("msgget error
"); exit(0); } exit(0); }
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/msg.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <fcntl.h>
#include <errno.h>

struct msgbuf {
    long mtype;
    char mtext[1];
}; 

int main(int argc, char **argv)
{
     int mqid;
    size_t len;
    long type;
    struct msgbuf *ptr;
    int n;

    if (argc != 4) {
        printf("usage: msgsnd <pathmane> <#bytes> <type>");
        exit(0);
    }

    len = atoi(argv[2]);
    type = atoi(argv[3]);
    if ((mqid = msgget(ftok(argv[1],0), 00200)) == -1) {
        perror("msgget error");
        exit(0);
    }

    ptr = calloc(sizeof(long) + len, sizeof(char));
    ptr->mtype = type;

    n = msgsnd(mqid, ptr, len, 0);
    if (n < 0) 
        perror("msgsend erro");
    printf("the size of send is %d
", n); exit(0); }
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/msg.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <fcntl.h>

#define MAXMSG (8192 + sizeof(long))

struct msgbuf {
    long mtype;
    char mtext[1];
};

int main(int argc, char **argv) 
{
    int c, flag, mqid;
    long type;
    ssize_t n;
    struct msgbuf *buff;

    type = flag = 0;

    while ((getopt(argc, argv, "nt:")) != -1) {
        switch(c) {
            case 'n':
                flag |= IPC_NOWAIT;
                break;
            case 't':
                type = atol(optarg);
                break;
        }
    }

    if (optind != argc - 1) {
        printf("usage: msgrcv [-n] [-t type] <pathname>");
        exit(0);
    }

    if ((mqid = msgget(ftok(argv[optind], 0), O_RDONLY)) == -1) {
        printf("msgget error");
        exit(0);
    }

    buff = malloc(MAXMSG);

    n = msgrcv(mqid, buff, MAXMSG, type, flag);
    printf("read %d bytes, type = %ld
", n, buff->mtype); exit(0); }