0

This is a very simple message queue that i am trying to understand

I am able to post the message to the queue but when i am trying to receive the message back i am not receiving.

Please guide what is the error. am a noob to C http://pastebin.com/UqLWKgub

            ####MSG.H#######
            #define MAX_MSG_SIZE 5120
            typedef struct
            {
                    int msgType;
                    int msgSize;
                    char msgBuf[MAX_MSG_SIZE];
            }STRUCT_MSG;

            ####msg.c####
            #include <glib.h>
            #include <Msg.h>

            G_LOCK_DEFINE_STATIC (queue_memchunk);
            static GMemChunk   *queue_memchunk = NULL;
            static GTrashStack *free_queue_nodes = NULL;

            STRUCT_MSG *txMsg;

            //mqd_t msgQueueId[6];
            GQueue *msgQueueId[6];

            int PostMessageQ(int channel, int msgType, char *msgBuf, int msgSize)
            {
                    txMsg = (STRUCT_MSG*)malloc(sizeof(STRUCT_MSG));
                    txMsg->msgType = msgType;
                    txMsg->msgSize = msgSize;
                    memcpy(&txMsg->msgBuf[0],&msgBuf[0],msgSize);
                    g_queue_push_head (&msgQueueId[channel], (char *)&txMsg);
                    return 0;
            }

            int PendMessageQ(int channel, STRUCT_MSG *rxMsgptr)
            {
                    gpointer rxMsg;
                    rxMsg = g_queue_pop_head (&msgQueueId[channel]);
                    memcpy(&rxMsgptr[0],&rxMsg[0],sizeof(STRUCT_MSG));
            }


            GQueue*
            g_queue_create (int channel)
            {
              G_LOCK (queue_memchunk);
              msgQueueId[channel] = g_trash_stack_pop (&free_queue_nodes);

              if (!msgQueueId[channel])
                    {
                      if (!queue_memchunk)
                            queue_memchunk = g_mem_chunk_new ("GLib GQueue chunk",
                                                                                              sizeof (GNode),
                                                                                              sizeof (GNode) * 128,
                                                                                              G_ALLOC_ONLY);
                      msgQueueId[channel] = g_chunk_new (GQueue, queue_memchunk);
                    }
              G_UNLOCK (queue_memchunk);

              msgQueueId[channel]->head = NULL;
              msgQueueId[channel]->tail = NULL;
              msgQueueId[channel]->length = 0;
              //msgQueueId[channel] = queue;
              return msgQueueId[channel];
            }

            void
            g_queue_free (GQueue *queue)
            {
              g_return_if_fail (queue != NULL);

              g_list_free (queue->head);

              G_LOCK (queue_memchunk);
              g_trash_stack_push (&free_queue_nodes, queue);
              G_UNLOCK (queue_memchunk);
            }

            void
            g_queue_push_head (GQueue  *queue,
                                               gpointer data)
            {
              g_return_if_fail (queue != NULL);

              queue->head = g_list_prepend (queue->head, data);
              if (!queue->tail)
                    queue->tail = queue->head;
              queue->length++;
            }

            void
            g_queue_push_head_link (GQueue *queue,
                                                            GList  *link)
            {
              g_return_if_fail (queue != NULL);
              g_return_if_fail (link != NULL);
              g_return_if_fail (link->prev != NULL);
              g_return_if_fail (link->next != NULL);

              link->next = queue->head;
              if (queue->head)
                    queue->head->prev = link;
              else
                    queue->tail = link;
              queue->head = link;
              queue->length++;
            }

            void
            g_queue_push_tail (GQueue  *queue,
                                               gpointer data)
            {
              g_return_if_fail (queue != NULL);

              queue->tail = g_list_append (queue->tail, data);
              if (queue->tail->next)
                    queue->tail = queue->tail->next;
              else
                    queue->head = queue->tail;
              queue->length++;
            }

            void
            g_queue_push_tail_link (GQueue *queue,
                                                            GList  *link)
            {
              g_return_if_fail (queue != NULL);
              g_return_if_fail (link != NULL);
              g_return_if_fail (link->prev != NULL);
              g_return_if_fail (link->next != NULL);

              link->prev = queue->tail;
              if (queue->tail)
                    queue->tail->next = link;
              else
                    queue->head = link;
              queue->tail = link;
              queue->length++;
            }

            gpointer
            g_queue_pop_head (GQueue *queue)
            {
              g_return_val_if_fail (queue != NULL, NULL);

              if (queue->head)
                    {
                      GList *node = queue->head;
                      gpointer data = node->data;

                      queue->head = node->next;
                      if (queue->head)
                            queue->head->prev = NULL;
                      else
                            queue->tail = NULL;
                      g_list_free_1 (node);
                      queue->length--;
                      printf("First in line is %s\n",data );
                      return data;
                    }

              return NULL;
            }

            GList*
            g_queue_pop_head_link (GQueue *queue)
            {
              g_return_val_if_fail (queue != NULL, NULL);

              if (queue->head)
                    {
                      GList *node = queue->head;

                      queue->head = node->next;
                      if (queue->head)
                            {
                              queue->head->prev = NULL;
                              node->next = NULL;
                            }
                      else
                            queue->tail = NULL;
                      queue->length--;

                      return node;
                    }

              return NULL;
            }

            gpointer
            g_queue_pop_tail (GQueue *queue)
            {
              g_return_val_if_fail (queue != NULL, NULL);

              if (queue->tail)
                    {
                      GList *node = queue->tail;
                      gpointer data = node->data;

                      queue->tail = node->prev;
                      if (queue->tail)
                            queue->tail->next = NULL;
                      else
                            queue->head = NULL;
                      queue->length--;
                      g_list_free_1 (node);

                      return data;
                    }

              return NULL;
            }

            GList*
            g_queue_pop_tail_link (GQueue *queue)
            {
              g_return_val_if_fail (queue != NULL, NULL);

              if (queue->tail)
                    {
                      GList *node = queue->tail;

                      queue->tail = node->prev;
                      if (queue->tail)
                            {
                              queue->tail->next = NULL;
                              node->prev = NULL;
                            }
                      else
                            queue->head = NULL;
                      queue->length--;

                      return node;
                    }

              return NULL;
            }

            gboolean
            g_queue_is_empty (GQueue *queue)
            {
              g_return_val_if_fail (queue != NULL, TRUE);

              return queue->head == NULL;
            }

            gpointer
            g_queue_peek_head (GQueue *queue)
            {
              g_return_val_if_fail (queue != NULL, NULL);

              return queue->head ? queue->head->data : NULL;
            }

            gpointer
            g_queue_peek_tail (GQueue *queue)
            {
              g_return_val_if_fail (queue != NULL, NULL);

              return queue->tail ? queue->tail->data : NULL;
            }

            ###main.c####

            int main(int argc, char *argv[])
            {
                    STRUCT_MSG test;
                    printf("MsgBuf Valusse is ::");
                    g_queue_create(1);
                    g_queue_create(2);
                    PostMessageQ(1,4,"testmesg",8);
                    PendMessageQ(1,&test);
                    printf("MIAN MsgBuf Value is :: %s\n",test.msgBuf);
                    PendMessageQ(2,&test);
                    printf("ENd of main");
                    return 0;
            }
4

1 回答 1

1

有大量 GQueue API 的复制/粘贴,混合了一些使用“通道”参数的新函数,但命名方式与真正的 GQueue 函数相同,这是一个糟糕的主意。

最后,似乎错误出现在您给我们的代码块的最后。您调用 PendMessageQ,但您没有在该队列中发布任何数据。因此,您尝试从该队列中弹出一个值,但只获得一个您尝试复制的 NULL 指针。为此,您会在 memcpy 中遇到分段错误。

下一次,使用调试器找出来,并请提供一个可以编译的代码,这里不是这样。

于 2013-08-15T10:54:28.633 回答