Alternative for popen pipe to execute program and read output

Please suggest a good alternative for popen() to execute shell commands and then read the result.

EDIT: The alternative should be without calling fork() . Because my server is already taking up too much memory. Then ffmpeg also needs to increase the size of the memory and the process! and every time I get problems with fork() the memory server.

+2
source share
2 answers

If you are worried about copying the memory of the parent process during forcing, you need to use vfork() , a special version of "fork" that does not copy the memory of the parent process, but requires the forked process to immediately issue execve() .

+4
source

Here's how I was taught at school:

 int main(int argc, char *argv[]) { int pipefd[2]; pid_t cpid; char buf; if (pipe(pipefd) == -1) { perror("pipe"); exit(EXIT_FAILURE); } cpid = fork(); if (cpid == -1) { perror("fork"); exit(EXIT_FAILURE); } if (cpid == 0) { /* Child reads from pipe */ close(pipefd[1]); //make the standard input to be the read end pipefd[0] = dup2(pipefd[0], STDIN_FILENO); system("more"); write(STDOUT_FILENO, "\n", 1); close(pipefd[0]); } else { /* Parent writes argv[1] to pipe */ close(pipefd[0]); /* Close unused read end */ pipefd[1] = dup2(pipefd[1], STDOUT_FILENO); system("ps aux"); /* Wait for child */ wait(NULL); exit(EXIT_SUCCESS); } return 0; } 

this spawns two processes, one of which runs "ps aux" and feeds the output to the other, which runs "more."

0
source

All Articles