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?
c linux posix fork
Patryk
source share