Thread: Fork Example

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    4

    Fork Example

    I'm having some problems to understand this code, especially how "(!pid[0])" works in if statements. Can somebody explain me how it's evaluated?

    Code:
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <stdio.h>
    #include <errno.h>
    #include <unistd.h>
        
        int main(int argc, char *argv[])
        {
          int status;
          int pid[2];
          int pipe_fd[2];
    
          char *prog1_argv[4];
          char *prog2_argv[2];
    
          /* 
           * Build argument list
           */
    
          prog1_argv[0] = "/usr/local/bin/ls";
          prog1_argv[1] = "-l";
          prog1_argv[2] = "/";
          prog1_argv[3] = NULL;
    
    
          prog2_argv[0] = "/usr/ucb/more";
          prog2_argv[1] = NULL;
    
          /*
           * Create the pipe
           */
          if (pipe(pipe_fd) < 0)
          {
            perror ("pipe failed");
            exit (errno);
          }
    
          /*
           * Create a process space for the ls  
           */
          if ((pid[0]=fork()) < 0)
          {
            perror ("Fork failed");
            exit(errno);
          }
    
          if (!pid[0])
          {
              /*
               * Set stdout to pipe     
               */
              close (pipe_fd[0]);
              dup2 (pipe_fd[1], 1);
              close (pipe_fd[1]);
    
            /* Execute the ls */ 
            execvp (prog1_argv[0], prog1_argv);
          }
    
          if (pid[0])
          {
            /* 
             * We're in the parent 
             */
    
            /*
             * Create a process space for the more
             */
            if ((pid[1]=fork()) < 0)
            {
              perror ("Fork failed");
              exit(errno);
            }
      
            if (!pid[1])
            {
              /*
               * We're in the child
               */
    
              /*
               * Set stdin to pipe     
               */
              close (pipe_fd[1]);
              dup2 (pipe_fd[0], 0);
              close (pipe_fd[0]);
    
              /* Execute the more */ 
              execvp (prog2_argv[0], prog2_argv);
            }
    
            /* This is the parent */
            close(pipe_fd[0]);
            close(pipe_fd[1]);
    
            waitpid (pid[1], &status, 0);
            printf ("Done waiting for more.\n");
    
          }
    
        }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if (!pid[0])
    Well writing
    if ( pid[0] == 0 )
    would have been more meaningful.
    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. Understanding fork()
    By NuNn in forum C Programming
    Replies: 8
    Last Post: 02-27-2009, 12:09 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. Fork - unpredictable?
    By fredkwok in forum Linux Programming
    Replies: 4
    Last Post: 03-26-2006, 02:49 PM
  4. fork(), exit() - few questions!
    By s3t3c in forum C Programming
    Replies: 10
    Last Post: 11-30-2004, 06:58 AM
  5. Daemon programming: allocated memory vs. fork()
    By twisgabak in forum Linux Programming
    Replies: 2
    Last Post: 09-25-2003, 02:53 PM