Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

27.9. Exercises

  1. The final command in the following shell session uses the program in Example 27-3 to exec the program xyz. What happens?

    $ echo $PATH
    /usr/local/bin:/usr/bin:/bin:./dir1:./dir2
    $ ls -l dir1
    total 8
    -rw-r—r—   1 mtk      users        7860 Jun 13 11:55 xyz
    $ ls -l dir2
    total 28
    -rwxr-xr-x   1 mtk      users       27452 Jun 13 11:55 xyz
    $ ./t_execlp xyz

  2. Use execve() to implement execlp(). You will need to use the stdarg(3) API to handle the variable-length argument list supplied to execlp(). You will also need to use functions in the malloc package to allocate space for the argument and environment vectors. Finally, note that an easy way of checking whether a file exists in a particular directory and is executable is simply to try execing the file.

  3. What output would we see if we make the following script executable and exec() it?

    #!/bin/cat -n
    Hello world

  4. What is the effect of the following code? In what circumstances might it be useful?

    childPid = fork();
    if (childPid == -1)
        errExit("fork1");
    if (childPid == 0) {    /* Child */
        switch (fork()) {
        case -1: errExit("fork2");
    
        case 0:             /* Grandchild */
            /* ——- Do real work here ——- */
            exit(EXIT_SUCCESS);             /* After doing real work */
    
        default:
            exit(EXIT_SUCCESS);             /* Make grandchild an orphan */
        }
    }
    
    /* Parent falls through to here */
    
    if (waitpid(childPid, &status, 0) == -1)
        errExit("waitpid");
    
    /* Parent carries on to do other things */

  5. When we run the following program, we find it produces no output. Why is this?

    #include "tlpi_hdr.h"
    
    int
    main(int argc, char *argv[])
    {
        printf("Hello world");
        execlp("sleep", "sleep", "0", (char *) NULL);
    }

  6. Suppose that a parent process has established a handler for SIGCHLD and also blocked this signal. Subsequently, one of its children exits, and the parent then does a wait() to collect the child’s status. What happens when the parent unblocks SIGCHLD? Write a program to verify your answer. What is the relevance of the result for a program calling the system() function?


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint