about fork command in linux

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • satyanagendra
    New Member
    • Mar 2007
    • 21

    about fork command in linux

    Hi
    My aim is to create 4 individual childs for a parent and the parent will wait until the 3rd child expires. For that I write the code like this but I am not getting the output correctly. If any one know the problem in code please reply.

    [code=c]
    main()
    {
    int i = 4;
    for( i =0 ;i < 4 ;i++)
    {
    if(fork())
    {
    fflush(stdout);
    printf("\nparen t : %d",getpid()) ;
    }
    else
    {
    fflush(stdout);
    printf("\nchild : %d frm parent:%d",getp id(),getppid()) ;
    sleep(5);
    exit(0);
    }
    }
    printf("parent waiting for third child");
    wait(3);
    printf("parent terminating");
    }

    [/code]
    regards

    satya
    Last edited by weaknessforcats; Oct 13 '07, 02:43 PM. Reason: Fixed code tags
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I corrected your code tags. Use square brackets, [ ], rather than angle brackets, < >. See the reply guidelines message box.

    Comment

    • smalpani
      New Member
      • Aug 2007
      • 29

      #3
      Originally posted by satyanagendra
      Hi
      My aim is to create 4 individual childs for a parent and the parent will wait until the 3rd child expires. For that I write the code like this but I am not getting the output correctly. If any one know the problem in code please reply.

      [code=c]
      int main()
      {
      pid_t pids[4] = {0,0,0,0}; //be careful with these
      pid_t parentid,childi d,myid;
      int i; //why do you want to initialize these with 4
      int status;
      parentid = getpid(); //this is the original pid of the parent
      for( i =0 ;i < 4 ;i++)
      {
      pids[i] =fork();
      if (pid[i]<0)
      {
      printf("Fork for %d child failed",pids[i]+1);
      if (i==2)
      exit(0); //this program is just concerned abt third chld
      }
      if(pid[i]!=0)
      {
      fflush(stdout);
      printf("\nparen t : %d",getpid()) ;
      }
      else
      {
      fflush(stdout);
      printf("\nchild : %d frm parent:%d",getp id(),getppid()) ;
      sleep(5);
      exit(0);
      }
      }
      myid = getpid();
      while(myid==par entid) //enter loop only if parent
      {
      printf("parent waiting for third child\n"); // this line will be printed a lot remove it if you dont want this junk
      childid = wait(&status);
      if (childid =pids[2] ) //if the child to expire is third child
      {
      printf("I am parent, my favorite child died, I dont want to live anymore");
      break;
      }
      //else some other child died we dont worry abt him
      }
      }

      [/code]
      regards

      satya
      Ok first lets try to identify whats wrong with your code.

      1. A call to wait requires a pointer to an integer which will contain the exit status of the child. Your call will try to write the status to a memory location 3, which is most liable to crash the program.

      2. You see to assume that a call like wait(3) will exit when the third process exits, I guess you are absolutely wrong, there is nothing like that.

      3. I have modified your code according to me it should work but right now I am not a linux machine so I am not sure if there is still an error.

      Comment

      • smalpani
        New Member
        • Aug 2007
        • 29

        #4
        Code:
        int  main()
        {
             pid_t pids[4] = {0,0,0,0}; //be careful with these
             pid_t parentid,childid,myid;
              int i;  //why do you want to initialize these with 4
             int status;
             parentid = getpid();   //this is the original pid of the parent
              for( i =0 ;i < 4 ;i++)
              {
                  myid = getpid();
                  if (myid == parentid)   //only the parent forks
                  {
                      pids[i] =fork();
                  }
            
                  if (pids[i]<0) 
                  {
                       printf("Fork for %d child failed",pids[i]+1);
                       if (i==2)
                             exit(0); //this program is just concerned abt third chld
                  }
                  if(pids[i]!=0)
                  {
                       fflush(stdout);
                       printf("\nparent : %d",getpid());
                  }
                 else  
                 {
                     fflush(stdout);
                     printf("\nchild : %d frm parent:%d",getpid(),getppid());
                     sleep(5);
                     exit(0);
                  }
              }
             myid = getpid();
             while(myid==parentid)   //enter loop only if parent
             {
                 printf("parent  waiting for third child\n"); // this line will be printed a lot remove it if you dont want this junk
                 childid = wait(&status);
                 if (childid =pids[2] )  //if the child to expire is third child
                 {
                         printf("I am parent, my favorite child died, I dont want to live anymore");
                         break;
                 }
                 //else some other child died we dont worry abt him
             }
        }

        Modified the code so that only the parent forks
        Last edited by smalpani; Oct 13 '07, 04:10 PM. Reason: corrected few errors

        Comment

        • smalpani
          New Member
          • Aug 2007
          • 29

          #5
          Originally posted by smalpani
          [CODE=c]
          int main()
          {
          pid_t pids[4] = {0,0,0,0}; //be careful with these
          pid_t parentid,childi d,myid;
          int i;
          int status;
          parentid = getpid(); //this is the original pid of the parent
          for( i =0 ;i < 4 ;i++)
          {
          myid = getpid();
          if (myid == parentid) //only the parent forks
          {
          pids[i] =fork();
          }

          if (pids[i]<0)
          {
          printf("Fork for %d child failed",pids[i]+1);
          if (i==2)
          exit(0); //this program is just concerned abt third chld
          }
          if(pids[i]==0)
          {
          fflush(stdout);
          printf("\nchild : %d frm parent:%d",getp id(),getppid()) ;
          sleep(5);
          exit(0);
          }
          }
          myid = getpid();
          while(myid==par entid) //enter loop only if parent
          {
          childid = wait(&status);
          if (childid =pids[2] ) //if the child to expire is third child
          {
          printf("I am parent, my favorite child died, I dont want to live anymore");
          break;
          }
          //else some other child died we dont worry abt him
          }
          }
          [/CODE]


          Corrected the code syntax and few other things, this should work without much clutter

          Comment

          • satyanagendra
            New Member
            • Mar 2007
            • 21

            #6
            Originally posted by smalpani
            Corrected the code syntax and few other things, this should work without much clutter
            Thank Q i will try this.

            regards
            Satya

            Comment

            Working...