COMP 3000 Lab 3 2012
Instructions
This lab is still being revised
In this lab you will learn about execve, monitoring system calls, dynamic libraries. This lab has 30 points in total.
You may use any machine running Ubuntu 12.04 or equivalent. You may need to install software if a given utility is not already installed; other than that, you do not need administrative access to the machine.
You should expect to complete Part A in tutorial. You should submit the answers to both Part A and Part B, however.
Indicate where you got the information to answer each question. This can be as simple as "the TA told me" or instead "I looked at chapter 7 in book X" or, just, "man page for ls". If you did some sort of experiment(s) to determine the answer, outline them briefly. If you do not include such information, you'll automatically have 10 points deducted from your grade.
You should turn in Lab 3 by 10 PM on Friday, October 12 via cuLearn. Your answers should be in plain text (the true UNIX file format) or PDF. No other formats are acceptable (and will result in a zero grade until re-submitted in the correct format). All your code should compile and run. Partial code fragments or explanations will not be sufficient.
Part A
- [1] Compile the program hello.c (below) with gcc -O hello.c -o hello-dyn and then run it using the command ltrace ./hello-dyn . What dynamic functions does the program call?
- [1] Compile the same program with gcc -O -static hello.c -o hello-static . Run this second binary with ltrace as before. What dynamic functions does the program now call?
- [2] Run strace on the static and dynamically compiled versions of hello. How many system calls do they each produce?
- [1] How can you make the output of hello.c go to the file "hello-output" by changing how it is invoked at the command line?
- [1] How can you make the output of hello.c go to the file "hello-output" by changing its code?
- Compile run-program.c with the command gcc -g run-program.c -o run-program and use it to answer the following questions.
- [2] Is the line "Is this line printed?" printed when you execute ./run-program /bin/ls -a /tmp? Why?
- [2] Change the program to use execve instead of execvp. What is the difference between the two functions?
- Linux signals is a simple form of IPC that is used for a variety of purposes.
- [2] What signal is generated when a program attempts to divide by zero? Give a simple program that generates such a signal.
- [1] How would you send a signal to a process to have it pause execution? Resume?
- [1] What command can lower the priority of an already running process?
Part B
- [2] Why do we "open" files? Specifically, to what extent is it possible to do file I/O without open and close operations - assuming you could change the UNIX API?
- [2] What is the relationship between function calls, system calls, and library calls?
- [2] Create run-program-skips.c that is the same as run-program.c except that it ignores every other argument to the program to be run (starting with the first non-program-name argument). Hence, ./run-program-reverse /tmp ignore -a ignore /bin/ls will do the same as ./run-program /bin/ls -a /tmp.
- [2] Create sanitize-env.c that does the same as run-program.c except it exec's the program with all environment variables stripped except the PATH and TERM variables (assuming they are defined). Be sure to document how you tested your program.
- [2] Create a simple program receive-term.c that uses sigaction in order to print "Ouch!" when you send the program the TERM signal and then exits. Note that your program should not consume much CPU time while waiting (i.e., do not poll for the signal). As part of your answer, give the command for sending the signal.
- [2] Create a simple program nocrash.c that accesses an invalid pointer but then prints "Oops, my mistake" rather than crash.
- [2] Create a program run-program-dots.c that works the same as run-program.c, except that it prints one dot every second while a given program runs. Note that it should stop printing dots once the exec'd program terminates. Your solution should use the fork(), sleep(), execve(), and sigaction() calls.
- [1] What is the difference between the PR and NI columns in the top command? Explain in the context of running a CPU-intensive program. (Hint: you should try running a CPU intensive program or two and observe these columns...)
Program Listings
/* hello.c */
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
pid_t p;
p = getpid();
printf("Hello, world! I am process %d.\n", (int) p);
return 0;
}
/* run-program.c */
#include <unistd.h>
#include <stdio.h>
int main( int argc, char *argv[], char *envp[] ) {
pid_t p;
p = getpid();
printf("Hello again. I am process %d.\n", (int) p);
if( argc < 2 ) {
printf( "Insufficient arguments.\n" );
return -1;
}
execvp( argv[1], argv + 1 );
printf( "Is this line printed?\n" );
return 0;
}