How to exchange binary data between processes on Linux

I need to create a linux application that would conduct a wireless network scan, put the result in a structure and send it in some way to another, main application that will use the data. My initial idea was to create a pipe in the main application, fork, and start another process using execl, which can write to the pipe. Something like that:

pid_t pid = NULL; int pipefd[2]; FILE* output; char line[256]; pipe(pipefd); pid = fork(); if (pid == 0) { // Child close(pipefd[0]); dup2(pipefd[1], STDOUT_FILENO); dup2(pipefd[1], STDERR_FILENO); execl("/sbin/wifiscan", "/sbin/wifiscan", (char*) NULL); } //Only parent gets here. Listen to what the wifi scan says close(pipefd[1]); output = fdopen(pipefd[0], "r"); while(fgets(line, sizeof(line), output)) { //Here we can listen to what wifiscan sends to its standard output } 

This, however, will not work with binary data if the binary value 0 appears on the output. Therefore, I could either format the output of the wifiscan application into text, send it to the handset and parse in the main application, or do it in a more smart way, which I still I do not know.

What are other ways to reliably exchange data between processes on Linux?

+6
c linux posix fork
source share
4 answers

I suspect what happens when fgets() reads NUL characters correctly, but you interpret the first as the end of the line that was read. This is tricky because fgets() is for text input, and it uses "\ 0" as a sentinel, rather than returning the number of characters read. Even if you know that each line ends with \n , there is a possibility that the raw binary data embedded in the line will also contain \n . So switch to fread() , which is for binary. You can put a message size of a fixed length (e.g. 2 bytes, 4 bytes) at the beginning of each message so that the other side can read it first and then fread() for the exact size of the message, avoiding the messy problems with the partial message being read.

If you really want to keep some strange hybrid of text and binary in it, you can try using ftell() after fgets() to find out how much further downstream you are, and therefore how many characters should be in your buffer but I never saw a serious system do something so hacked.

For the record, without doing something clearly ineffective, like the hexadecimal encoding of each individual character in binary data, you can simply encode the nasty character (s). For example, C escape lines can be used, with \0 representing NUL and \\ one \ . Although less easily visually verified, it may be easier to programmatically encode / decode non-printable characters using, for example, octal \NNN . If there are many non-printable characters, then the base-64 uuencode approach, such as used in MIME email attachments, is another suitable but completely unreadable encoding.

+7
source share

There are many...

I would read Stevens.

http://www.kohala.com/start/unpv22e/unpv22e.html

+3
source share

Typically, fread and fwrite can be used to exchange binary structures between processes. It works great for fixed-length records, and there is no problem sending zeros, etc.

I have found during my long career that in most applications it is much better to exchange ascii strings, it is easy to parse (for example, on scanf ) instead of binary data. In this way, messages can be monitored and recorded, and individual processes can be tested using telnetting and typing (usually insertion). It just makes development easier if the programmer can just read the message traffic.

+2
source share

Reliable data exchange? It makes me think about the 0MQ library (ZeroMQ): http://zeromq.org

Look, and as a bonus, your processes will be able to talk even on remote computers.

Welcome to the cloud.

0
source share

All Articles