COMP 3000 Lab 5 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.
- Compile hello.c with the command gcc -O hello.c -o hello-dyn. Run ltrace ./hello-dyn. What dynamic functions did hello call?
- Run strace ./hello-dyn. How many system calls did hello make?
- 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.)
- How big are the binaries of hello-dyn and hello-static? Why is one so much bigger than the other one? Explain.
- 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?
- 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?
- How can you modify hello-fork.c so that the child process would run /bin/ls using the execve() function?
- 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; }