这是一个非常简单的消息队列,我试图理解
我能够将消息发布到队列中,但是当我尝试接收消息时,我没有收到消息。
请指导错误是什么。我是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;
}
答案 0 :(得分:1)
GQueue API有很多复制/粘贴,使用“channel”参数混合了一些新函数,但命名方式与真正的GQueue函数相同,这是一个糟糕的主意。
最后,似乎错误发生在你给我们的代码块的最后。您调用PendMessageQ,但您没有在该队列中发布任何数据。因此,您尝试从该队列中弹出一个值,但只获取一个您尝试复制的NULL指针。并且你在memcpy中得到了一个分段错误。
下次,使用调试器找出它,并提供一个编译的代码,这不是这里的情况。