Thread: Double Fork Problem

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    23

    Double Fork Problem

    I am having problems with running a child in the background. I want the child to run and then keep the parent active.

    This is what keeps happening:
    PID TTY TIME CMD
    16844 pts/0 00:00:00 bash
    5262 pts/0 00:00:00 youare
    5263 pts/0 00:00:00 youare <defunct>
    5264 pts/0 00:00:00 ps

    youare is my main program and it keeps getting defunct.

    Here is the section of my code that is doing the forking and executing:

    Code:
    pid_t childPid;
    pid_t pid;
    int status;
    
    
    if ((childPid = fork()) == 0) //1st child
        {
          
          
        if ((pid = fork()) != 0) // 2nd child
          {
            exit(0);
          }
          
        
        if (execlp ("ps", "ps", (char *)0) == -1)
         {
           perror("exec");
           exit(0);
         }
    
        }
    }
    Thank you!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well no wonder it's defunct, you call fork() and then exit(), which just kills the process you just created.

    No wait, your comments are wrong
    Code:
    if ((childPid = fork()) == 0) //1st child
        {
          
          
        if ((pid = fork()) != 0)
          {
            exit(0);  // 1st child exits
          }
          
        // 2nd child exec's "ps"
        if (execlp ("ps", "ps", (char *)0) == -1)
         {
           perror("exec");
           exit(0);
         }
    
        }
    }
    The way you tidy up the first child process is by calling wait() with childPid as a parameter.

    Also, bear in mind that "ps" is a momentary snapshot which could be inaccurate given that processes are being created and killed at around the same time.
    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
    Registered User
    Join Date
    Apr 2003
    Posts
    23
    I don't think my question was answered correctly. I don't want to wait because I want the program to run in the background, in this case the ps command. While it's running in the background I want to be able to still control my main program.

    Here is an updated version of my excerpt of my program.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <dirent.h>
    #include <fcntl.h>
    #include <time.h>
    #include <sys/param.h>
    #include <signal.h>
    
    
    main()
    {
    pid_t childPid;
    pid_t pid;
    int status;
    char test[100];
    
    
    while(1)
    {
      printf("%> ");
      scanf("%s", &test);
      if (strcmp(test,"go") ==0)
      {
    
        if ((childPid = fork()) == 0) 
        {
          
          
          if ((pid = fork()) != 0)
          {
            exit(0);
          }
          
        
        if (execlp ("ps", "ps", (char *)0) == -1)
        {
          perror("exec");
          exit(0);
        }
            
       }
      }
      else if (strcmp(test,"exit") ==0)
       exit(0);
    
    }
    
    }
    Thank you!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I don't want to wait because I want the program to run in the background
    But you're waiting on the child (which has already exited, so wait() will return almost immediately), and the grand-child goes on its merry way doing what it needs to do

    You need to call wait() on a terminated process so that it does not remain a 'zombie' in the process table.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  2. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  3. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM
  4. Little Problem on Presentation in the Tower of Hanoi
    By momegao in forum C++ Programming
    Replies: 3
    Last Post: 04-20-2003, 06:22 PM
  5. getline problem
    By scottmanc in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2003, 09:27 PM