Thread: Child processes

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    13

    Child processes

    New question for you guys out there. If I was to use execvp to launch a child process could I monitor and change its variable using the parent program? Better yet, can I capture stdout from that specific process?

    Moreover, if I use other exec functions or system() to launch this program, would the launched program still be a child process?

    Any help in understanding this would be greatly appreciated.
    Last edited by jsx-27; 08-20-2012 at 08:09 PM.

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by jsx-27 View Post
    New question for you guys out there. If I was to use execvp to launch a child process could I monitor and change its variable using the parent program?
    I am going to assume you know to use fork before execvp to launch a child process. If you don't know about fork, just ask and we can help with that part too.

    Now, what do you mean by change a child process's variable? Do you mean changing the value of a specific variable in the child from the parent? If so, then yes; debuggers do this kind of stuff all the time. The child won't have to know that something is changing its variables, but the parent would have to have intimate knowledge of the child's memory layout. There are less complicated ways to do it, though, but the parent and child will have to cooperate to make it work (eg, they have to explicitly share memory).

    Better yet, can I capture stdout from that specific process?
    That is relatively straightforward. Look up the pipe() system call. The basic idea is to connect the child's stdout to a pipe that the parent can read from. An even simpler method is to use popen to treat the program's output like a file stream (the same thing you get with fopen).

    Moreover, if I use other exec functions or system() to launch this program, would the launched program still be a child process?
    Yes, the system() function is basically a wrapper around fork and exec, or a similar set of calls that starts a program as a child process.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    you can share memory between processes using shmget and shmat. then you can overlay the shared memory region with a data structure both processes know the layout of. here is a reasonable tutorial Using Shared Memory in Linux Programming « Kah – The Developer

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    13
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-01-2011, 11:06 AM
  2. How do I stop child processes from becoming defunct?
    By Zarniwoop in forum C Programming
    Replies: 7
    Last Post: 12-19-2009, 06:36 PM
  3. forks and child processes
    By kpax in forum C Programming
    Replies: 1
    Last Post: 05-28-2008, 04:47 AM
  4. sockets and child processes
    By Elkvis in forum Linux Programming
    Replies: 2
    Last Post: 03-06-2008, 04:03 PM
  5. Child processes in Linux
    By Music_Man in forum Linux Programming
    Replies: 0
    Last Post: 03-20-2003, 09:04 AM