views:

227

answers:

1

In one of my programs, there are multiple clients and each client has its own buffer. In an infinite loop, I check if any of the client has any data to be written to disk. If it does, then I do the same and continue.

Now, because the client writes data that is not really in my control (result of some computations), I need a dynamic buffer. So the pseudo code would 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
   }
}

or the other approach would be to use a simple message queue. I would keep adding whatever messages (strings) to an existing queue and then read them off. Is there some other better approach?

+2  A: 

I might not have fully understood your architecture, but my understanding is that a client calls you by passing it's ID and a char * and wants you to write it to disk.

Is there a reason you have to copy the original buffer? By doing that, you have everything in memory twice and an opportunity to mess up memory management. If possible, just work off of the original buffer.

Is there some threading happening here? If this is all single threaded (from the point of view of this "server" code at least... one thread to poll and write results), you don't really need a FIFO since things will just happen in the order you poll clients. If there is a thread (or multiple ones) to poll clients and a separate thread to write the results, a FIFO is a great way to organize thread communication.

Eric J.
Thanks... I will follow your suggestions... Will need to see how well I can implement that... :)
Legend