Thread: Passing PIDs to another program

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    54

    Passing PIDs to another program

    I'm a little lost in this File System Calls functions. I'm trying to implement a simple program which copies a source file and writes into the output file.

    To the point, when I fork 2 childs from a parent, I should have a pid of each child in the parent program. The first and the second.

    When I use the execvp() function to execute another program within the second child, which function should I use to pass the pid of the first child into the program I've executed? Knowing that execvp() takes 2 parameters which is

    Code:
    execvp( const char *file, char *const argv[])
    The parameters I've passed is the program name and the file name.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Try:
    Code:
    {
      char *argv[3];
      char pidstr[10];  // Enough space for the pid in string form
    
      sprintf(pidstr, "%d", pid_of_child1);
    
      argv[0] = program_for_child2;
      argv[1] = pidstr;
      argv[2] = NULL;
    
      execvp(program_for_child2, argv);
    }
    Disclaimer: The above is untested code.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    54
    When I run "./a.out input output"

    Is this true?
    Code:
    argv[0] = input;
    argv[1] = output;
    argv[2] = NULL;
    If I were to do this
    Code:
    char *arg[3];
    
    arg[0] = argv[1];
    arg[1] = <pidofchild1>;
    arg[2] = NULL;
    
    execvp("program_4_child2", arg);
    When I check the program for the 2nd child, i printed the argc
    because it gave me an error earlier due to different number "argc".
    It was suppose to be 3 but its shown 0 instead. How is this so?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Check on man exec:
    The const char *arg and subsequent ellipses in the execl, execlp, and execle functions can be thought of as arg0, arg1, ..., argn. Together they describe a list of one or more pointers to null-terminated strings that represent the argument list available to the executed program. The first argument, by convention, should point to the file name associated with the file being executed. The list of arguments must be terminated by a NULL pointer.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 09:48 AM
  5. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM