Thread: save the output of child process

  1. #1
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92

    save the output of child process

    In Gnome desktop manager, you have virtual desktops or workspace. Click here if u don't know what virtual desktop is:
    http://www.mozillaquest.com/Linux4Wi..._Story-01.html

    Now, assume you have program called "stupid" that can guess what workspace u r in now. Assume you are in workspace number one and in console you type:
    $ stupid
    The stupid person are in workspace 0
    You go to workspace number 2 and run it again:
    $ stupid
    The stupid person are in workspace 1

    Now I want to make wallpaper changer according to this program. This is my idea. I make the daemon and check every half second with this function:
    Code:
    string bullStrong;
    while(TRUE) {
            execlp("stupid", NULL);
            //I want to save the stdout of this child process to string
            //but I don't know how
            bullStrong = howDoISaveIt();
            //some code here
            ChangeTheWallpaper(bullStrong);
    }
    Would you help me write the howDoISaveIt() function please?
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use popen()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92
    I have read that before I post my question, Hammer but it is stupidly hard for newbie to understand. Well, if newbie want to know how to save the output of child process and accidentally stop in this thread, here how I do it:

    Code:
    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        FILE *read_fp;
        char buffer[BUFSIZ + 1];
        int chars_read;
        memset(buffer, '\0', sizeof(buffer));
        read_fp = popen("uname -a", "r");
        if (read_fp != NULL) {
            chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp);
            if (chars_read > 0) {
                printf("Output was:-\n%s\n", buffer);
            }
            pclose(read_fp);
            exit(EXIT_SUCCESS);
        }
        exit(EXIT_FAILURE);
    }
    That code is copyrighted. I downloaded code for "Beginning Linux Programming" free of charge from here:
    http://www.wrox.com/dynamic/books/download.aspx

    You got a lot of sample code that easy to understand. That's all, babe.
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. init adopts zombie process?
    By password636 in forum Linux Programming
    Replies: 4
    Last Post: 07-01-2009, 10:05 AM
  2. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 02:14 PM
  3. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  4. Redirecting STDOUT to STDIN of a child process
    By 0xception in forum Linux Programming
    Replies: 4
    Last Post: 09-13-2005, 11:58 PM
  5. Problems with child process creation
    By Niloc1 in forum C Programming
    Replies: 0
    Last Post: 02-09-2003, 02:52 PM