zoukankan      html  css  js  c++  java
  • 消息队列

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/msg.h>
    #include <unistd.h>
    #include <sys/ipc.h>
    
    void msg_show_attr(int msg_id, struct msqid_ds msg_info)
    {
    int ret = -1;
    sleep(1);
    ret = msgctl(msg_id, IPC_STAT, &msg_info);
    if( -1 == ret)
    {
    printf(“获得消息信息失败\n”);
    return ;
    }
    
    printf(“\n”);
    printf(“现在队列中的字节数:%d\n”,msg_info.msg_cbytes);
    printf(“队列中消息数:%d\n”,msg_info.msg_qnum);
    printf(“队列中最大字节数:%d\n”,msg_info.msg_qbytes);
    printf(“最后发送消息的进程pid:%d\n”,msg_info.msg_lspid);
    printf(“最后接收消息的进程pid:%d\n”,msg_info.msg_lrpid);
    printf(“最后发送消息的时间:%s”,ctime(&(msg_info.msg_stime)));
    printf(“最后接收消息的时间:%s”,ctime(&(msg_info.msg_rtime)));
    printf(“最后变化时间:%s”,ctime(&(msg_info.msg_ctime)));
    printf(“消息UID是:%d\n”,msg_info.msg_perm.uid);
    printf(“消息GID是:%d\n”,msg_info.msg_perm.gid);
    }
    int main(void)
    {
    int ret = -1;
    int msg_flags, msg_id;
    key_t key;
    struct msgmbuf{
    int mtype;
    char mtext[10];
    };
    struct msqid_ds msg_info;
    struct msgmbuf msg_mbuf;
    
    int msg_sflags,msg_rflags;
    char *msgpath = “/ipc/msg/”;
    key = ftok(msgpath,’a');
    if(key != -1)
    {
    printf(“成功建立KEY\n”);
    }
    else
    {
    printf(“建立KEY失败\n”);
    }
    
    msg_flags = IPC_CREAT;
    msg_id = msgget(key, msg_flags|0666);
    if( -1 == msg_id)
    {
    printf(“消息建立失败\n”);
    return 0;
    }
    msg_show_attr(msg_id, msg_info);
    
    msg_sflags = IPC_NOWAIT;
    msg_mbuf.mtype = 10;
    memcpy(msg_mbuf.mtext,”测试消息”,sizeof(“测试消息”));
    ret = msgsnd(msg_id, &msg_mbuf, sizeof(“测试消息”), msg_sflags);
    if( -1 == ret)
    {
    printf(“发送消息失败\n”);
    }
    msg_show_attr(msg_id, msg_info);
    
    msg_rflags = IPC_NOWAIT|MSG_NOERROR;
    ret = msgrcv(msg_id, &msg_mbuf, 10,10,msg_rfla
    


  • 相关阅读:
    Codeforces Round #502 (in memory of Leopoldo Taravilse, Div. 1 + Div. 2) E. The Supersonic Rocket
    Codeforces Round #500 (Div. 2) D
    AtCoder Grand Contest 026 D
    Codeforces Round #495 (Div. 2) Sonya and Matrix
    AtCoder Regular Contest 100 E
    1013 数素数
    1010 一元多项式求导(用while接收输入)
    1009 说反话(字符串、栈)
    L2-006 树的遍历 (后序中序求层序)
    L2-004 这是二叉搜索树吗?
  • 原文地址:https://www.cnblogs.com/yuzaipiaofei/p/4124392.html
Copyright © 2011-2022 走看看