Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

popen() alternative

My question is extension of this one: popen creates an extra sh process

Motives:

1) My program need to create a child which does tail on a file. I need to process the output line by line. That is why I am using popen because it returns FILE *. I can easily fetch single line, do what I need to do and print it.

One problem with popen is that you do not get pid of child (tail command in my case).

2) My program should not exit before its child is done. So I need to do wait; but without pid, I cannot do it.

How can I achieve both the goals?

A possible (kludge) solution: do execvp("tail -f file > tmpfile") and the keep reading that tmpfile. I am not sure how good this solution is, though.

like image 839
hari Avatar asked Jul 19 '11 07:07

hari


Video Answer


2 Answers

Why aren't you using pipe/fork/exec method?

pid_t pid = 0;
int pipefd[2];
FILE* output;
char line[256];
int status;

pipe(pipefd); //create a pipe
pid = fork(); //span a child process
if (pid == 0)
{
// Child. Let's redirect its standard output to our pipe and replace process with tail
 close(pipefd[0]);
 dup2(pipefd[1], STDOUT_FILENO);
 dup2(pipefd[1], STDERR_FILENO);
 execl("/usr/bin/tail", "/usr/bin/tail", "-f", "path/to/your/file", (char*) NULL);
}

//Only parent gets here. Listen to what the tail says
close(pipefd[1]);
output = fdopen(pipefd[0], "r");

while(fgets(line, sizeof(line), output)) //listen to what tail writes to its standard output
{
//if you need to kill the tail application, just kill it:
  if(something_goes_wrong)
    kill(pid, SIGKILL);
}

//or wait for the child process to terminate
waitpid(pid, &status, 0);
like image 173
Patryk Avatar answered Oct 19 '22 03:10

Patryk


  1. You can use pipe, a function of the exec* family and fdopen. This is non-standard, but so is popen.
  2. You don't need to wait. Just read the pipe up to EOF.
  3. execvp("tail -f file > tmpfile") won't work, redirection is a feature of the shell and you're not running the shell here. Even if it worked it would be an awful solution. Suppose you have read to the end of the file, but the child process has not ended yet. What do you do?
like image 31
n. 1.8e9-where's-my-share m. Avatar answered Oct 19 '22 03:10

n. 1.8e9-where's-my-share m.