Difference between revisions of "COMP 3000 Lab 3 2011"

From Soma-notes
Jump to navigation Jump to search
Line 17: Line 17:
<li> How would you send a signal to a process to have it pause execution?  Resume?</li>
<li> How would you send a signal to a process to have it pause execution?  Resume?</li>
</ol>
</ol>
<li>What command can lower the priority of an already running process?   
<li>What command can lower the priority of an already running process?  
<li>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>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).
</ol>
</ol>


Line 23: Line 25:


<ol>
<ol>
<li value=4> 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 value=6> 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).</li>
<li> 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).</li>
<li> Create a simple program ''nocrash.c'' that accesses an invalid pointer but then prints "Oops, my mistake" rather than crash.</li>
<li> Create a simple program ''nocrash.c'' that accesses an invalid pointer but then prints "Oops, my mistake" rather than crash.</li>
<li> 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.</li>
<li> 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.</li>

Revision as of 10:31, 17 October 2011

A few guidelines:

  • Submit your solutions for both Part A and Part B via WebCT by October 21st at 8 PM.
  • Please answer using text files. Do not use doc, docx, pdf etc.
  • Show all your work. Short answers are not sufficient; you should list any websites or individuals you consult and should document any experiments you conducted. For any question that you could just answer without any external reference or experiment, write "(no work required)" after your answer.

Part A

  1. Compile run-program.c with the command gcc -g run-program.c -o run-program and use it to answer the following questions.
    1. 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 new line?
  2. Linux signals is a simple form of IPC that is used for a variety of purposes.
    1. What signal is generated when a program attempts to dereference an invalid pointer? Give a simple program that generates such a signal.
    2. How would you send a signal to a process to have it pause execution? Resume?
  3. What command can lower the priority of an already running process?
  4. 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.
  5. 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).

Part B

  1. 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).
  2. Create a simple program nocrash.c that accesses an invalid pointer but then prints "Oops, my mistake" rather than crash.
  3. 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.
  4. 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

/* 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;
}