C: Is there anything better than implementing a FIFO queue for this requirement?

One of my programs has several clients, and each client has its own buffer. In an endless loop, I check to see if any of the clients have any data that will be written to disk. If so, then I do the same and continue.

Now, since the client is writing data that is actually not under my control (the result of some calculations), I need a dynamic buffer. Thus, the pseudocode will look like this:

//If data is ready
//Append(client_id, line)

void Append(int client_id, char *line) {
   if(client_id.buffer == NULL) {
      buffer = (char*)malloc(BUFFERSIZE * sizeof(char));
      //Copy line into buffer
   } else {
      //Realloc the buffer if insufficient space and append this
      //line to the existing buffer
   }
}

and another approach is to use a simple message queue. I will add all the messages (lines) to the existing queue and then read them. Is there any other better approach?

+3
1

, , , , ID char * , .

, ? , . , .

- ? ( "" ... ), FIFO, , . ( ) , FIFO - .

+3

Source: https://habr.com/ru/post/1730956/


All Articles