How to receive dynamic length data from a message queue?

前端 未结 5 699
陌清茗
陌清茗 2020-12-30 18:23

I have to send and receive dynamic data using a SysV message queue for a university project.

The length of the data is transmitted in a separate message, size<

5条回答
  •  一生所求
    2020-12-30 18:25

    Here is an example for SyS. I hope it will help.

    The way you are using malloc seems to be correct but you should be very careful when making memory allocations for IPC. You should check how the other process is managing memory (byte alignment, size, platform...)

    In your code, What is the purpose of the mtype? Does the size you receive takes this mtype into account? Or is it only the size of mdata?

    Update: Is mtype part of the message?

    If so:

    msgsize = size * sizeof(char) + sizeof(long)

    pmsg = malloc(msgsize);

    msgrcv(MSGQ_ID, pmsg, msgsize, MSQ_ID, 0);

    if not

    msg.data = (char *)malloc(size * sizeof(char));

    msgrcv(MSGQ_ID, msg.data, size, MSQ_ID, 0);

    The mtype is alocated on the stack while the data is allocated on the heap. If the msgreceive makes a kind of memcpy on the given pointer, it will cause some troubles.

提交回复
热议问题