在多个队列中发送和接收消息-C编程

问题描述 投票:2回答:1

我有一个任务,我必须创建多个队列。目前,我很困惑,因为我不了解如何将消息发送到特定队列以及如何将输入的消息发送/接收到该特定队列。这是我编写的代码:

typedef struct _node {
    const char *message;
    struct _node *next;
} node_t;

typedef struct {
    char qName;
    node_t *front, *rear;
} msg_queue_t;


typedef struct {
    msg_queue_t **queues;
}MsgQs_t;

返回指向MsgQs_t结构的指针,通过该指针可以随后创建多个消息队列。每个单独的消息队列将由唯一的标识符标识。

MsgQs_t * initializeMsgQs(){
    MsgQs_t *msg_queues;
    msg_queues = malloc(sizeof(MsgQs_t));

    if(msg_queues < 0)
        exit(EXIT_FAILURE);

    return(msg_queues);
}

放弃MsgQs_t.当前拥有的所有资源指向相关MsgQs_t的指针设置为NULL

void unloadMsgQs(MsgQs_t **msg_queues){
    if(msg_queues != NULL){
        free(*msg_queues);
        *msg_queues = NULL;
    }
}

这是一个用于创建新链表节点的实用程序功能

struct _node* newNode(const char *m){
    struct _node* temp = malloc(sizeof(struct _node));
    temp -> message = m;
    temp -> next = NULL;
    return temp;
}

创建一个新队列,并为其分配一个作为参数传递的标识符。指定当前分配的标识符将返回错误。

msg_queue_t* createQ(){
    msg_queue_t* queue = malloc(sizeof(msg_queue_t));

    queue -> front = queue -> rear = NULL;
    return queue;
}

根据是否指定队列标识符,将消息发送到单个或所有队列。如果队列标识符不存在,则返回错误。

void sendMessage(msg_queue_t* queue, const char *m){

    struct _node* temp = newNode(m);

    if (queue -> rear == NULL){
        queue -> front = queue -> rear = temp;
        return;
    }

    queue -> rear -> next = temp;
    queue -> rear = temp;
    printf("\nMessage sent to queue: %s\n", m);
}

从指定队列的开头读取指定数量的消息,或者直到队列为空。

每个已读消息将从队列中删除。如果队列标识符不存在,则返回错误。

 struct _node* receiveMessage(msg_queue_t* queue){
    if (queue -> front == NULL)
        return NULL;

    struct _node* temp = queue -> front;
    free(temp);

    queue -> front = queue -> front -> next;

    if (queue -> front == NULL)
        queue -> rear = NULL;
    return temp;
}

以下是在其他源文件中找到的主要功能。在这个主要功能中,我有一个带有多个选项的switch语句,这些语句将创建/更改队列中的内容。我没有包含所有带有switch语句的主菜单,因为它太长了,并且不属于我的问题的一部分,而是显示该问题所涉及的内容。

int main(){
    msg_queue_t *msg_queues;
    msg_queues = initializeMsgQs();


switch (choice) {
            case '1':
                printf("\nCreating a queue\n");
                msg_queues = createQ();
                queueCount++;
                break;
            case '4':
                printf("\nSending a message\nEnter message to send:");
                scanf("%s", msg);
                sendMessage(msg_queues, msg);
                break;
            case '5':
                printf("\nReceiving a message\n");
                printf("Message received: %s\n", receiveMessage(msg_queues->front));
                break; 

         unloadMsgQs(&msg_queues);
}

用户需要为队列发送/接收消息的标识符。然后,取决于哪种情况,应该从具有指定标识符的队列之一中存储/拉出消息。

mercurial
1个回答
1
投票

我认为这段代码产生了预期的行为:

typedef struct msg_queue_node {
    char * message;
    struct msg_queue_node * next;
} msg_queue_node_t;


typedef struct msg_queue {
    msg_queue_node_t * head, * tail;
} msg_queue_t;


int main(void)
{
    int i, id;      // indentifies the message queue in the array
    msg_queue_t * msg_queues = malloc(NUM_QUEUES * sizeof(msg_queue_t*));

    for (i = 0; i < NUM_QUEUES; i++)
        msg_queues[i] = createQ();

    // ...

    while ( /*condition*/ ) {

        scanf("%c %d", choice, id);

        switch (choice) {
            case '4':
                printf("\nSending a message\nEnter message to send:");
                scanf("%s", msg);
                sendMessage(msg_queues[id-1], msg);
                break;
            case '5':
                printf("\nReceiving a message\n");
                printf("Message received: %s\n", receiveMessage(msg_queues[id-1]));
                break; 
        }
    }

    // ...
}

msg_queues是大小为NUM_QUEUES的数组。

在我的解决方案中,我们希望用户输入他希望向/从/向其发送消息的队列的标识符。我假定每个队列的标识符对应于其在数组+ 1中的索引,即标识符为1,...,NUM_QUEUES。

© www.soinside.com 2019 - 2024. All rights reserved.