Difference between revisions of "COMP 3000 Lab 3 2012"

From Soma-notes
Jump to navigation Jump to search
Line 37: Line 37:


<ol>
<ol>
<li value=6> [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?
<li value=9> [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?
<li> [2] What is the relationship between dynamic library calls and system calls?
<li> [2] What is the relationship between dynamic library calls and system calls?
<li> [2] Create ''run-program-reverse.c'' that is the same as run-program.c except that it interprets its arguments in reverse order.  Hence, ''./run-program-reverse /tmp -a /bin/ls'' will do the same as ''./run-program /bin/ls -a /tmp''. </li>
<li> [2] Create ''run-program-reverse.c'' that is the same as run-program.c except that it interprets its arguments in reverse order.  Hence, ''./run-program-reverse /tmp -a /bin/ls'' will do the same as ''./run-program /bin/ls -a /tmp''. </li>

Revision as of 04:26, 1 October 2012

Instructions

This lab is still being revised

In this lab you will examine how processes interact with the dynamic linker and the kernel. This lab has ?? 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 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 codes should compile and run. Partial code fragments or explanations will not be sufficient.

Part A

  1. [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?
  2. [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?
  3. [2] Run strace on the static and dynamically compiled versions of hello. How many system calls do they each produce?
  4. [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?
  5. [1] How can you make the output of hello.c go to the file "hello-output" by changing its code?
  6. Compile run-program.c with the command gcc -g run-program.c -o run-program and use it to answer the following questions.
    1. [2] Is the line "Is this line printed?" printed when you execute ./run-program /bin/ls -a /tmp? Why?
    2. [2] Change the program to use execve instead of execvp. What is the difference between the two system calls?
  7. Linux signals is a simple form of IPC that is used for a variety of purposes.
    1. [2] What signal is generated when a program attempts to dereference an invalid pointer? Give a simple program that generates such a signal.
    2. [1] How would you send a signal to a process to have it pause execution? Resume?
  8. [1] What command can lower the priority of an already running process?

Part B

  1. [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. [2] What is the relationship between dynamic library calls and system calls?
  3. [2] Create run-program-reverse.c that is the same as run-program.c except that it interprets its arguments in reverse order. Hence, ./run-program-reverse /tmp -a /bin/ls will do the same as ./run-program /bin/ls -a /tmp.
  4. [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.
  5. [2] Create a simple program receive-usr1.c that uses sigaction in order to print "Ouch!" when you send the program the USR1 signal. Note that your program should not consume much CPU time while waiting (i.e., do not poll for the signal).
  6. [2] Create a simple program nocrash.c that accesses an invalid pointer but then prints "Oops, my mistake" rather than crash.
  7. [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.
  8. [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[])
{
       printf("Hello, world!\n");
       return 0;
}


/* run-program.c */

#include <unistd.h>
#include <stdio.h>

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

   if( argc < 2 ) {
       printf( "Insufficient arguments.\n" );
       return -1;
   }
   execvp( argv[1], argv + 1 );
   printf( "Is this line printed?\n" );
   return 0;
}