开发者

capturing commandline output directly in a buffer

开发者 https://www.devze.com 2022-12-16 16:42 出处:网络
I want to execute a command using system() command or execl and want to capture the output directly in a buffer in C. Is ther any possibility to capture the output in a buffer using dup() system call

I want to execute a command using system() command or execl and want to capture the output directly in a buffer in C. Is ther any possibility to capture the output in a buffer using dup() system call or using pipe(). I dont want to use any file in between using mkstemp or any other temporary file. please help me in this.Thanks in advance.

I tried it with fork() creating two process and piping the output and it is working.However I dont want to use fo开发者_开发知识库rk system call since i am going to run the module infinitely using seperate thread and it is invoking lot of fork() and system is running out of resources sometimes after.

To be clear about what i am doing is capturing an output of a shell script in a buffer processing the ouput and displaying it in a window which i have designed using ncurses.Thankyou.


Here is some code for capturing the output of program; it uses exec() instead of system(), but that is straightforward to accomodate by invoking the shell directly:

How can I implement 'tee' programmatically in C?

void tee(const char* fname) {
    int pipe_fd[2];
    check(pipe(pipe_fd));
    const pid_t pid = fork();
    check(pid);
    if(!pid) { // our log child
        close(pipe_fd[1]); // Close unused write end
        FILE* logFile = fname? fopen(fname,"a"): NULL;
        if(fname && !logFile)
                fprintf(stderr,"cannot open log file \"%s\": %d (%s)\n",fname,errno,strerror(errno));
        char ch;
        while(read(pipe_fd[0],&ch,1) > 0) {
                //### any timestamp logic or whatever here
                putchar(ch);
                if(logFile)
                        fputc(ch,logFile);
                if('\n'==ch) {
                        fflush(stdout);
                        if(logFile)
                                fflush(logFile);
                }
        }
        putchar('\n');
        close(pipe_fd[0]);
        if(logFile)
                fclose(logFile);
        exit(EXIT_SUCCESS);
    } else {
        close(pipe_fd[0]); // Close unused read end
        // redirect stdout and stderr
        dup2(pipe_fd[1],STDOUT_FILENO);  
        dup2(pipe_fd[1],STDERR_FILENO);  
        close(pipe_fd[1]);  
    }
}


A simple way is to use popen ( http://www.opengroup.org/onlinepubs/007908799/xsh/popen.html), which returns a FILE*.


You can try popen(), but your fundamental problem is running too many processes. You have to make sure your commands finish, otherwise you will end up with exactly the problems you're having. popen() internally calls fork() anyway (or the effect is as if it did).

So, in the end, you have to make sure that the program you want to run from your threads exits "soon enough".


You want to use a sequence like this:

Call pipe once per stream you want to create (eg. stdin, stdout, stderr)
Call fork
in the child
   close the parent end of the handles
   close any other handles you have open
   set up stdin, stdout, stderr to be the appropriate child side of the pipe
   exec your desired command
   If that fails, die.

in the parent
   close the child side of the handles
   Read and write to the pipes as appropriate
   When done, call waitpid() (or similar) to clean up the child process.

Beware of blocking and buffering. You don't want your parent process to block on a write while the child is blocked on a read; make sure you use non-blocking I/O or threads to deal with those issues.


If you are have implemented a C program and you want to execute a script, you want to use a fork(). Unless you are willing to consider embedding the script interpreter in your program, you have to use fork() (system() uses fork() internally).

If you are running out of resources, most likely, you are not reaping your children. Until the parent process get the exit code, the OS needs keeps the child around as a 'zombie' process. You need to issue a wait() call to get the OS to free up the final resources associated with the child.

0

精彩评论

暂无评论...
验证码 换一张
取 消