Classwork 3

It's time to do a bunch of investigative work with processes! A veritable process potpourri -- a proctpourri!

You may answer all of the following questions on either a written sheet of paper, or single text document turned in via Brightspace (make sure you number the questions and clearly indicate which answers pertain to which problems).


Problem 1

In a modified version of the fork_wait.c sample from the lecture, we have added an infinite loop to the child process as follows:

  #include <stdio.h>
  #include <unistd.h>
  #include <sys/types.h>
  #include <sys/wait.h>
  
  int main() {
      pid_t pid;
  
      pid = fork();
      
      if (pid == 0) {
          printf("I'm just a kid so I see pid = 0: %d\n", pid);
          // [!] Arrested development
          while(1) {}
      
      } else {
          printf("I'm Papa Process; my kid has pid: %d\n", pid);
          int status;
          wait(&status);
          printf("All done! Result was: %i\n", status);
      }
      
      return 0;
  }

Compile this program and name the executable something easily identifiable, like arrested_dev, then Run the executable in the background via ./arrested_dev & and answer the following questions.

  1. List the bash command you used to find the parent and child process that are running, and explain how you can identify which is the parent.

  2. Identify the PID of the bash terminal in which you executed your program. Show the process tree that results from using pstree <bash_PID>, where <bash_PID> is, of course, the PID of your bash terminal.

  3. What gets printed when you kill the child? (yikes, hope I'm not on a list for writing that)


Problem 2

  #include <stdio.h>
  #include <unistd.h>
  #include <sys/types.h>
  
  int main() {
      fork();
    
      fork();
    
      fork();
    
      return 0;
  }
  1. In the program above, how many processes, including the original parent, are created during its execution?

  2. Look up the getpid() system call, then show how (by modifying the program) you could use it to verify your answer to the above.


Problem 3

  #include <stdio.h>
  #include <unistd.h>
  #include <sys/types.h>
  #include <sys/wait.h>
  
  int main() {
      pid_t pid;
  
      /* fork a child process */
      pid = fork();
    
      if (pid == 0) {
          printf("Child %d\n", pid);
          execlp("/bin/ls","ls",NULL);
          printf("WILL I PRINT?!");
      }
      else {
          printf("Parent %d\n",pid);
          wait(NULL);
          printf("Child Complete\n");
      }
        
      return 0;
  }

In the modified version of the exec example from class, we've added a print statement following the exec system call.

Will it be printed? Why or why not?


Problem 4

  #include <stdio.h>
  #include <unistd.h>
  #include <stdlib.h>
  #include <sys/types.h>
  #include <sys/wait.h>
  
  int svar = 0;
  
  int main() {
      int lvar = 0;
      int* dvar = malloc(sizeof(int));
      *dvar = 0;
      pid_t pid;
  
      pid = fork();
      
      if (pid == 0) {
          svar  += 1;
          lvar  += 2;
          *dvar += 3;
          return 0;
      } else if (pid > 0) {
          wait(NULL);
          printf ("PARENT: svar=%i, lvar=%i, dvar=%i\n", svar, lvar, *dvar);
          return 0;
      }
      free(dvar);
  }
  1. Before running the above, consider what will be printed.

  2. After running the above, explain the output in terms of each process' image in memory.


Problem 5

It's time to resurrect everyone's favorite go-to algorithm... but now with more forking! That's right, we'll be making good ole Fibonacci by forking... Forkonacci.

Recall: a Fibonacci sequence is simply the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, ...

Which is defined as: $$\begin{eqnarray} fib_0 &=& 0 \\ fib_1 &=& 1 \\ fib_n &=& fib_{n-1} + fib_{n-2} \end{eqnarray}$$

Write a C program that takes \(n\) as an argument via the command line, and then prints out the first \(n\) items of the Fibonacci sequence.

The catch: your child process may only have a printf and return statement, and your parent process may *not* contain a printf statement.


Problem 6

Warning: do not run the following unless incrediby curious, it may crash your system.

  #include <unistd.h>

  int main() {
      while(1) {
          fork();
          int* i = malloc(1000);
      }
  }

The above is commonly known as a fork bomb, for obvious reasons. Explain why it is a naughty thing to do, but also, why it may be hilarious as a prank on a fellow OS student if they leave their laptop unattended.



Submission

What

Either hand in your classwork on paper or submit it in a single text document or PDF via Brightspace.

Ensure that your name, and the names of all group members (up to 4), appear at the top of your source files.



  PDF / Print