I need to write C code (not C# or C++) to get data from hardware analyzer (through 100Mb TCP/IP network) and write it to disk.
Why I say "Low latency", well, Hardware analyser have a 9KB internal buffer, this is 2ms of data storing, after that it runs into buffer overflow and I lose information packets.
My application is able to get this buffer without losing any packet, but I noticed I wasn't able to write data to the disk at this rate.
My code looks like this:
int main (int argc, char * argv [] )
{
pthread_t th_rx; /* Thread for receiving packets */
outputfile = fopen ("output.log", "wb");
link_open(); // open device link
pthread_create ( &th_rx, NULL, read_packets, 0 );
// running loop
fclose (outputfile);
pthread_exit(NULL);
link_close(); // close device link
return 0;
}
static thread_result read_packets (void *arg)
{
char rxbuf[40960];
while (receiving)
{
bytes_received = read_packet(); //read packet from device buffer
rxbuf = extract_data(bytes_received);
fwrite (rxbuf, 1, bytes_received, outputfile);
}
return thread_return;
}
What I need here are ideas to how to do that.
- 1: don't write packet upon received, create a circular? buffer
- 2: Creating 2 thread circular buffer?
Any ideas of how to improve my code and what I can do to get stable writes?
Code examples will be very appreciated because I'm feeling lost :(
Thanks