COMP 3000 Lab 5 2010: Difference between revisions

From Soma-notes
Created page with "In this lab you will work with the commands '''ltrace''' and '''strace'''. You may need to install these programs; if so, on Debian and Ubuntu systems type "sudo aptitude instal…"
 
Mbingham (talk | contribs)
m added \n
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
In this lab you will work with the commands '''ltrace''' and '''strace'''.  You may need to install these programs; if so, on Debian and Ubuntu systems type "sudo aptitude install strace ltrace".
In this lab you will work with the commands '''ltrace''' and '''strace'''.  You may need to install these programs; if so, on Debian and Ubuntu systems type ''sudo aptitude install strace ltrace''.


==Questions==
==Questions==
Line 24: Line 24:
   
   
         printf("Hello, world!\n");
         printf("Hello, world!\n");
        return 0;
}
/* hello-fork.c */
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
        int pid;
        printf("Hello, world!\n");
        pid = fork();
        if (pid) {
                /* parent */
                while(1) {
                        sleep(1);
                }
        } else {
                /* child */
                printf("I am the child!\n");
        }
         return 0;
         return 0;
  }
  }


==Hints==
==Hints==

Latest revision as of 15:33, 9 November 2010

In this lab you will work with the commands ltrace and strace. You may need to install these programs; if so, on Debian and Ubuntu systems type sudo aptitude install strace ltrace.

Questions

Answer all of the following questions.

  1. Compile hello.c with the command gcc -O hello.c -o hello-dyn. Run ltrace ./hello-dyn. What dynamic functions did hello call?
  2. Run strace ./hello-dyn. How many system calls did hello make?
  3. Re-compile hello.c using the command gcc -static -O hello.c -o hello-static. Run hello-static using ltrace and strace. How does the output compare with that from the previous two questions? (Explain at a high level.)
  4. How big are the binaries of hello-dyn and hello-static? Why is one so much bigger than the other one? Explain.
  5. Add a while(1) sleep(1); loop to hello.c so that it waits forever after saying hello. Recompile statically and dynamically. What is the resident and virtual memory used by both?
  6. Compile and run hello-fork.c. Note that hello-fork.c produces a zombie process. How do you fix hello-fork.c so that the zombie exits properly?
  7. How can you modify hello-fork.c so that the child process would run /bin/ls using the execve() function?
  8. strace and ltrace examine the behavior of a running program. The entire design of UNIX is such that each process lives in its own address space. How can these programs work? What mechanism must they use? Extra credit: what is that mechanism? Extra extra credit: make a program that tells you the PIDs of all forked children of a monitored process.

Code

/* hello.c */
#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[])
{

       printf("Hello, world!\n");
       return 0;
}


/* hello-fork.c */
#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[])
{

       int pid;

       printf("Hello, world!\n");

       pid = fork();

       if (pid) {
               /* parent */
               while(1) {
                       sleep(1);
               }
       } else {
               /* child */
               printf("I am the child!\n");
       }
       return 0;
}

Hints