Thread: Keeping pids

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

    Keeping pids

    As I fork() 2 childs from the same parent, I would like to keep my first child's pid and pass it to the 2nd child. Question is how do I do it? I've tried global variables or maybe I did it incorrectly.

    From the start, I was told by my lecturer that global variables are bad so I didn't read much about it. Here's a piece of the code...

    Code:
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <signal.h>
    #include <errno.h>
    
    int firstID;
    
    int main(){
    
      int id;
    
      if(fork() == 0){
        id = (int)getpid();
        printf("I am child : %d\n", id);
        firstID = id;
      }
      else{
        if(fork() == 0){
          printf("My First child's ID : %d\n", firstID);
        }
      }
    
      return 0;
    }
    When I print the pid on the 2nd fork, it shows me '0'.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Global variables are initialized to zero by default.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    54
    But if you see the code there, in the first fork, I've set the variable with the pid but it didn't appear on the second fork.

    Is there any solution on how I can keep the value of the first child's pid so it can be printed in the second child?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    int main(){
      int id;
    
      id = fork();
      if(id == 0){
        id = (int)getpid();
        printf("I am child : %d\n", id);
      }
      else{
        if(fork() == 0){
          printf("My First child's ID : %d\n", id);
        }
      }
    
      return 0;
    }
    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.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Quote Originally Posted by quzah
    Global variables are initialized to zero by default.


    Quzah.
    I thought that was only if the 'static' keyword was present.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Nope.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    54
    Thanks Salem, I got it to work this time

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [C] keeping a TreeView always expanded or collapsed
    By pc2-brazil in forum Windows Programming
    Replies: 2
    Last Post: 08-28-2008, 04:46 PM
  2. Fork(), pause(), and storing PID's in a linked list
    By vital101 in forum C Programming
    Replies: 10
    Last Post: 09-28-2007, 02:16 AM
  3. Converting types while keeping same size.
    By sbho in forum C Programming
    Replies: 3
    Last Post: 06-08-2007, 04:41 AM
  4. Problem keeping Dos Window Open
    By Unrege 123 in forum C++ Programming
    Replies: 3
    Last Post: 07-31-2003, 11:32 AM
  5. Paste code into word keeping color
    By GaGi in forum C++ Programming
    Replies: 1
    Last Post: 02-07-2002, 08:26 AM