【问题标题】:Can't delete old messages in message queue C无法删除消息队列 C 中的旧消息
【发布时间】:2017-12-16 04:02:52
【问题描述】:

我创建了一个 msq 让两个进程相互通信。问题是我遇到了一些问题,因为在我运行我的代码几次之后,出现了一些以前执行的旧消息。这让我很困扰,因为我需要对当前数据执行控制,因此无法做到。我尝试了 irpcm -q msqid 但它没有做任何事情,除了在我下次运行它时关闭我的程序。我什至尝试对一些键进行硬编码,认为它会有所帮助但没有任何帮助。在我完成使用队列后还尝试了 msgctl(msqid, IPC_RMID, 0) 但没有。希望您能帮助我并提前感谢。

这是我的代码:

sender.c

#define MAXSIZE     1024


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


void die(char *s)
{
    perror(s);
    exit(1);
}

int msqid1;
int msgflg = IPC_CREAT | 0666;
key_t keymq1;
struct msgbuf sbuf;
size_t buflen;
keymq1 = 668;
sbuf.mtype = 1;
if ((msqid1 = msgget(keymq1, msgflg )) < 0)
    die("msgget");
sbuf.mtype = 1;

  strcpy(sbuf.mtext,"my message");
    buflen = strlen(sbuf.mtext) + 1 ;
    if (msgsnd(msqid1, &sbuf, buflen, IPC_NOWAIT) < 0)
    {
        printf ("%d, %ld, %s, %zu\n", msqid1, sbuf.mtype, sbuf.mtext, buflen);
        die("msgsnd");
    }

    else {
        printf("Message sent\n");
 }

receiver.c

#define MAXSIZE     1024


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


void die(char *s)
{
    perror(s);
    exit(1);
}

int msqid;
key_t keymq1;
struct msgbuf rcvbuffer;
keymq1 = 668;
if ((msqid = msgget(keymq1, 0666)) < 0)
    die("msgget()");

    if (msgrcv(msqid, &rcvbuffer, MAXSIZE, 1, 0) < 0)
        die("msgrcv");

    printf("Message received: %s\n", rcvbuffer.mtext);

【问题讨论】:

  • 首先应该是root(su) 模式。您无法删除哪条消息queue id
  • 队列 id 是 65536,我已经尝试通过 ipcrm 删除它但它仍然给我带来问题
  • 使用ipcrm -q 65536 否则使用key 删除?

标签: c key message-queue


【解决方案1】:

要删除带有消息queue Idmessage queue,请使用

ipcrm -q id

或使用key 值作为删除message queue

ipcrm -Q key_num

来自手册页“要删除此类对象,您必须是超级用户,或者创建者 对象的创建者或所有者。"

最后你可以在msgctl()调用中使用IPC_RMID标志删除消息队列

main()
{
        int total_mq,i,msqid;
        struct msqid_ds ds; //dataStructure holding complete info for indexed message que
        struct msginfo msginfo; //general buff copying data from MSG_INFO, has info of how many message que present right now
        /* Obtain size of kernel 'entries' array */
        total_mq = msgctl(0, MSG_INFO, (struct msqid_ds *) &msginfo); //copy kernel MSGQ_INFO to local buff
        //returns count of active message Q
        if (total_mq < 0)
        {
                perror("msgctl");
                return 0;
        }
        printf("no of active message queue(KEY) : %d\n", total_mq+1);
        /* Retrieve meaasge Queue id's */
        for (i = 0; i <= total_mq; i++)
        {
                msqid = msgctl(i, MSG_STAT, &ds); //from each index using MSG_STAT -> ds, return msgqid
                if (msqid <0 )
                {
                        perror("msgctl");
                        return 0;
                }
                /* using msgqid remove the message queue */
                if ( msgctl(msqid,IPC_RMID,0) < 0 )
                {
                        perror("msgctl");
                        return 0;
                }
        }
        printf("all message queues removed\n");
        return 0;
}

在运行上述代码之前,创建一些消息队列,然后删除它们。

【讨论】:

    猜你喜欢
    • 2015-10-18
    • 2016-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    • 2016-02-22
    相关资源
    最近更新 更多