C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-24-2010, 09:18 PM   #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");

      }

    }
right2001 is offline   Reply With Quote
Old 01-24-2010, 11:21 PM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,636
> 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.

Salem is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Understanding fork() NuNn C Programming 8 02-27-2009 12:09 PM
Fork(), pause(), and storing PID's in a linked list vital101 C Programming 10 09-28-2007 02:16 AM
Fork - unpredictable? fredkwok Linux Programming 4 03-26-2006 02:49 PM
fork(), exit() - few questions! s3t3c C Programming 10 11-30-2004 06:58 AM
Daemon programming: allocated memory vs. fork() twisgabak Linux Programming 2 09-25-2003 02:53 PM


All times are GMT -6. The time now is 09:58 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22