Listing Directories and Closing Pipes: Listings

Listing 1. A find command to list out only directories.


find . -type d ! -name "." -prune -print

Listing 2. Reading from a pipeline.

A. Ricardo's Code:

 1  main ()
 2  {
 3      int fd[2];
 4      char linea[132];
 5      char *nom = "/bin/ls /dev";
 6  
 7      pipe(fd);
 8      if (fork())        /* parent */
 9          while (read(fd[0], linea, sizeof(linea)) > 0)
10              write(1, linea, strlen(linea));
11      else {             /* child */
12          close(1);      /* close stdout */
13          dup(fd[1]);    /* make pipe stdout */
14          close(fd[1]);  /* close pipe */
15          close(fd[0]);
16          execl("/bin/sh","/bin/sh", "-c", nom, (char *)0);
17          perror(nom);
18          exit(1);
19      }
20  }
21  

B. My Corrections:

 3      int fd[2];
 3a     int count;
 4      char linea[132];
 5      char *nom = "/bin/ls /dev";
 6  
 7      pipe(fd);
 8*     if (fork()) {
 8a         close(fd[1]);
 9*         while ( (count = read(fd[0], linea, sizeof(linea))) > 0)
10*             write(1, linea, count);
10a     }
11      else {
        rest of program unchanged


Listing 3: Using wait3() on BSD-based UNIX systems to prevent zombie processes.

/* In some initialization function: */
signal(SIGCHLD, reaper);
...

reaper()
{
    while (wait3((union wait *) 0, WNOHANG, NULL) > 0)
        ;
}

Listing 4: Getting a random number from the Korn Shell


set x=`/bin/ksh -c 'echo $RANDOM'`
Print This Page


e-mail Send as e-mail

Research and Reports

Storage Virtualization Guide
May 2012

Network Computing: May 2012

TechWeb Careers