Difference between revisions of "COMP 3000 Lab 3 2011"

From Soma-notes
Jump to navigation Jump to search
m (Protected "COMP 3000 Lab 3 2011" ([edit=sysop] (indefinite) [move=sysop] (indefinite)))
 
(21 intermediate revisions by 2 users not shown)
Line 1: Line 1:
A few guidelines:
A few guidelines:
* Submit your solutions via WebCT by October 21st at 8 PM.   
* Submit your solutions for both Part A and Part B via WebCT by '''Monday, October 24th at 11:30 PM''' (note the extension).   
* Please answer using text files. Do not use doc, docx, pdf etc.   
* Please answer using a single text file. Do not submit doc, docx, pdf, or other formatsAlso, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one.  (Please don't just cut and paste your work into a text box on webct.)
* 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.
* 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.
* All your codes should compile and run. Partial code fragments or explanations will not be sufficient.


==Part A==
==Part A==
Line 9: Line 10:
<li>Compile run-program.c with the command ''gcc -g run-program.c -o run-program'' and use it to answer the following questions.</li>
<li>Compile run-program.c with the command ''gcc -g run-program.c -o run-program'' and use it to answer the following questions.</li>
<ol style="list-style-type: lower-alpha;">
<ol style="list-style-type: lower-alpha;">
<li> Is the line "Is this line printed?" when you execute '''./run-program /bin/ls -a /tmp'''?  Why?</i>
<li> Is the line "Is this line printed?" printed when you execute '''./run-program /bin/ls -a /tmp'''?  Why? (1 mark) </li>
<li> Change the program to use execve instead of execvp . What is the new line?</li>
<li> Change the program to use execve instead of execvp. What is the difference between the two system calls? (1 mark) </li>
</ol>
</ol>
<li> Linux signals is a simple form of IPC that is used for a variety of purposes.</li>
<li> Linux signals is a simple form of IPC that is used for a variety of purposes.</li>
<ol style="list-style-type: lower-alpha;">
<ol style="list-style-type: lower-alpha;">
<li> What signal is generated when a program attempts to dereference an invalid pointer?  Give a simple program that generates such a signal.</li>
<li> What signal is generated when a program attempts to dereference an invalid pointer?  Give a simple program that generates such a signal.(1 mark) </li>
<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? (1 mark) </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? (1 mark)
<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''. (2 marks) </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).  Be sure to document how you tested your program. (2 marks)
</ol>
</ol>


Line 23: Line 26:


<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).(2 marks)</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.(2 marks) </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.(2 marks)</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> 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...) (1 mark)</li>
<li> 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...)</li>
</ol>
</ol>


Line 46: Line 48:
     printf( "Is this line printed?\n" );
     printf( "Is this line printed?\n" );
     return 0;
     return 0;
}
=Answers=
All questions graded by Saran. Email him for any queries.
==Part A==
<ol> <li>
<ol style="list-style-type: lower-alpha;">
<li>"is this line printed?" is not printed because execvp/execve replaces the program binary with a different one.</li>
<li> execve( argv[1], argv + 1 , envp); <br>
execve requires an environment argument which may be different from the existing one, where as execvp runs the program using the current environment. Another difference is that execvp searches the PATH environment variable for folders containing binary files, if it can't find the file in the current working directory. </li>
</ol> </li>
<li> <ol style="list-style-type: lower-alpha;">
<li>SIGSEGV or SIGBUS is raised for invalid memory references. SIGBUS is low level device specific signal. The following program generates a SIGSEGV in my computer : int *a; *a = 10; </li>
<li> SIGSTOP (which cannot be caught) or SIGTSTP (can be caught) for pausing, and SIGCONT for continuing.
You can use "kill -SIGWHATEVER pid" from the terminal, or from code. For ex :
pause-and-cont.c </li>
</ol>
</li>
<li> renice </li>
<li> run-program-reverse.c </li>
<li> sanitize-env.c. Run it with /usr/bin/env </li>
</ol>
(Part B)
<ol>
<li value=6> Compile and run receive-usr1.c and issue "kill -SIGUSR1 pid" </li>
<li> Use gdb to figure out what signal is thrown for invalid pointer accesses, then catch that. nocrash.c </li>
<li> run-program-dots.c </li>
<li>NI is the nice value of a process, which can be changed by the user. It ranges from -20 to 19 and is used as an input to the scheduler. Depending on all other processes and available resources, the scheduler decides the priority of a process and displays its calculated value in the PR column.
</li>
</ol>
==Answers : Program Listings==
/* pause-and-cont.c */
#include<stdio.h>
#include<signal.h>
int main(int argc, char * argv[], char *envp[])
{
  int child = fork();
  if(child == 0)
  { 
    execvp("/usr/bin/yes",argv);
  } 
  else
  { 
    kill(child,SIGSTOP);
    sleep(4);
    kill(child,SIGCONT);
    sleep(4);
    kill(child,SIGKILL);
  } 
  return 0;
}
/* run-program-reverse.c */
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
int main( int argc, char **argv, char *envp[] ) {
        char **newenv;
        int i = 0;
  if( argc < 2 ) {
      printf( "Insufficient arguments.\n" );
      return -1;
  }
        newenv = (char**)malloc(argc*sizeof(char**));
        for(i = 0; i < argc; i++)
        {
                newenv[i] = (char*)malloc(sizeof(argv[argc-i-1]));
                strcpy(newenv[i],argv[argc-i-1]);
        }
        newenv[argc-1] = NULL;
        if(execvp(newenv[0],newenv + 1) == -1)
                printf("error! = %s",strerror(errno));
 
        printf( "Is this line printed?\n" );
  return 0;
}
/* sanitize-env.c */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h> 
int main( int argc, char *argv[], char *envp[] ) {
        extern char **environ;
        int i = 0; 
        char *path = getenv("PATH");
        char *term = getenv("TERM");
        if(clearenv())
                exit(-1);
        setenv("PATH",path,1);
        setenv("TERM",term,1); 
        //Check now.
        while(environ[i] != NULL) {
                printf("environ[%d] = %s\n",i,environ[i++]);
        }
  if( argc < 2 ) {
      printf( "Insufficient arguments.\n" );
      return -1;
  }
  execvp(argv[1], argv + 1);
  printf( "Is this line printed?\n" );
  return 0;
}
/* receive-usr1.c */
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
int child;
void sig_handler(int signum)
{
          if(signum == SIGUSR1)
                printf("Ouch!.\n");
    fflush(stdout);
}
int main()
{
  struct sigaction act;
        sigset_t set;
 
  act.sa_handler = sig_handler;
  act.sa_flags = 0;
  sigemptyset(&act.sa_mask);
  sigaction(SIGUSR1, &act, NULL);
  sigfillset(&set);//Block on all signals
  sigdelset(&set,SIGUSR1);//except this one
  sigsuspend(&set);//now wait.
  return 0; 
}
/* nocrash.c */
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
int child;
void sig_handler(int signum)
{
        if(signum == SIGSEGV)
                printf("Oops, my mistake.\n");
        fflush(stdout);
        exit(1);
}
int main()
{
        int *a;
  struct sigaction act;
 
  act.sa_handler = sig_handler;
  act.sa_flags = 0;
  sigemptyset(&act.sa_mask);
  sigaction(SIGSEGV, &act, NULL);
  sleep(1);
  *a = 10; // Make a mistake.
  return 0; 
}
/* run-program-dots.c */
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
int child;
void sigchild_handler(int signum)
{
        exit(0);
}
int main()
{
  struct sigaction act;
 
  act.sa_handler = sigchild_handler;
  act.sa_flags = 0;
  sigemptyset(&act.sa_mask);
  child = fork();
 
  if(child == 0) {
        execve("program",NULL,NULL);
    return 0;
  }
  else {
    sigaction(SIGCHLD, &act, NULL);
        while(1)
        {
                printf(".");
                fflush(stdout);
                sleep(1);
        }
    return 0; 
  } 
}
where my program.c is
#include<stdlib.h>
int main()
{
        sleep(8);
        return 0;
  }
  }

Latest revision as of 21:05, 30 October 2011

A few guidelines:

  • Submit your solutions for both Part A and Part B via WebCT by Monday, October 24th at 11:30 PM (note the extension).
  • Please answer using a single text file. Do not submit doc, docx, pdf, or other formats. Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one. (Please don't just cut and paste your work into a text box on webct.)
  • 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.
  • All your codes should compile and run. Partial code fragments or explanations will not be sufficient.

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? (1 mark)
    2. Change the program to use execve instead of execvp. What is the difference between the two system calls? (1 mark)
  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.(1 mark)
    2. How would you send a signal to a process to have it pause execution? Resume? (1 mark)
  3. What command can lower the priority of an already running process? (1 mark)
  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. (2 marks)
  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). Be sure to document how you tested your program. (2 marks)

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

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

Answers

All questions graded by Saran. Email him for any queries.

Part A

    1. "is this line printed?" is not printed because execvp/execve replaces the program binary with a different one.
    2. execve( argv[1], argv + 1 , envp);
      execve requires an environment argument which may be different from the existing one, where as execvp runs the program using the current environment. Another difference is that execvp searches the PATH environment variable for folders containing binary files, if it can't find the file in the current working directory.
    1. SIGSEGV or SIGBUS is raised for invalid memory references. SIGBUS is low level device specific signal. The following program generates a SIGSEGV in my computer : int *a; *a = 10;
    2. SIGSTOP (which cannot be caught) or SIGTSTP (can be caught) for pausing, and SIGCONT for continuing. You can use "kill -SIGWHATEVER pid" from the terminal, or from code. For ex : pause-and-cont.c
  1. renice
  2. run-program-reverse.c
  3. sanitize-env.c. Run it with /usr/bin/env

(Part B)

  1. Compile and run receive-usr1.c and issue "kill -SIGUSR1 pid"
  2. Use gdb to figure out what signal is thrown for invalid pointer accesses, then catch that. nocrash.c
  3. run-program-dots.c
  4. NI is the nice value of a process, which can be changed by the user. It ranges from -20 to 19 and is used as an input to the scheduler. Depending on all other processes and available resources, the scheduler decides the priority of a process and displays its calculated value in the PR column.

Answers : Program Listings

/* pause-and-cont.c */

#include<stdio.h>
#include<signal.h>

int main(int argc, char * argv[], char *envp[])
{
  int child = fork();
  if(child == 0)
  {   
    execvp("/usr/bin/yes",argv);
  }   
  else
  {   
    kill(child,SIGSTOP);
    sleep(4);
    kill(child,SIGCONT);
    sleep(4);
    kill(child,SIGKILL);
  }   
  return 0;
}
/* run-program-reverse.c */

#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>  

int main( int argc, char **argv, char *envp[] ) {
        char **newenv;
        int i = 0;
  if( argc < 2 ) {
      printf( "Insufficient arguments.\n" );
      return -1;
  }
        newenv = (char**)malloc(argc*sizeof(char**));
        for(i = 0; i < argc; i++)
        {
                newenv[i] = (char*)malloc(sizeof(argv[argc-i-1]));
                strcpy(newenv[i],argv[argc-i-1]);
        }
        newenv[argc-1] = NULL;
        if(execvp(newenv[0],newenv + 1) == -1)
                printf("error! = %s",strerror(errno));
  
        printf( "Is this line printed?\n" );
  return 0;
}
/* sanitize-env.c */

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

int main( int argc, char *argv[], char *envp[] ) {
        extern char **environ;
        int i = 0;   

        char *path = getenv("PATH");
        char *term = getenv("TERM");
        if(clearenv())
                exit(-1);
        setenv("PATH",path,1);
        setenv("TERM",term,1);  

        //Check now.
        while(environ[i] != NULL) {
                printf("environ[%d] = %s\n",i,environ[i++]); 
        } 

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

#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h> 

int child;
void sig_handler(int signum)
{
         if(signum == SIGUSR1)
                printf("Ouch!.\n");
   fflush(stdout);
}

int main()
{
  struct sigaction act;
        sigset_t set;
  
  act.sa_handler = sig_handler;
  act.sa_flags = 0;
  sigemptyset(&act.sa_mask); 

  sigaction(SIGUSR1, &act, NULL);
  sigfillset(&set);//Block on all signals
  sigdelset(&set,SIGUSR1);//except this one
  sigsuspend(&set);//now wait.
  return 0;  
}
/* nocrash.c */

#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>

int child;
void sig_handler(int signum)
{
        if(signum == SIGSEGV)
               printf("Oops, my mistake.\n");
        fflush(stdout);
        exit(1);
}

int main()
{
       int *a;
  struct sigaction act;
 
 act.sa_handler = sig_handler;
 act.sa_flags = 0;
 sigemptyset(&act.sa_mask);

 sigaction(SIGSEGV, &act, NULL);
 sleep(1);
 *a = 10; // Make a mistake. 
 return 0;  
}
/* run-program-dots.c */

#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>

int child;
void sigchild_handler(int signum)
{
        exit(0); 
}

int main()
{
  struct sigaction act;
 
 act.sa_handler = sigchild_handler;
 act.sa_flags = 0;
 sigemptyset(&act.sa_mask);

 child = fork();
 
 if(child == 0) {
        execve("program",NULL,NULL);
   return 0;
 }
 else {
   sigaction(SIGCHLD, &act, NULL); 
        while(1)
        {
               printf(".");
               fflush(stdout);
               sleep(1);
        } 
   return 0;  
 }   
}

where my program.c is 
#include<stdlib.h>
int main()
{
        sleep(8);
        return 0;
}