Lab 2 - fork(), exec(), waitpid()

Prerequisite Skills for This Lab


  • Able to run the Docker dev environment for this course.
  • C Skills
    • Able to create a C program.
    • Able to use man page.

1. Setup


  1. Labs are a co-operative time! Introduce yourself to someone!
    • You can work individually or in pairs (pair-programming).
    • Help others!
  2. Use either your own laptop or the lab PC.
    • See Lab Directions for more details about using lab PCs.
    • See Resources page for docker commands.
    • Remember to use multiple tabs to work efficiently and put your code in your private git repo.


2. Design (10 mins)


Your task is:

Write a program that does the following:

  • It receives user input from the keyboard that is the full path of a program to run.
    e.g., "/usr/bin/ls", "/usr/bin/nvim", or "/usr/bin/ip", etc.
    • You do not need to handle commandline arguments (such as /usr/bin/ls -l -a).
  • It executes the command the user typed in.
  • It repeats the above two steps forever.
  • Use fork(), exec(), and waitpid().

Example output:

Enter programs to run.
> /usr/bin/ls
a.out  lab1_bri.c  lab1.c  lab2.c
Enter programs to run.
> /usr/bin/ip
Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }
       ip [ -force ] -batch filename
Enter programs to run.
> oops/bad/file
Exec failure
Enter programs to run.
> ^C

(Ended with Ctrl+c)

  1. Design your solution.
  2. Hints
    • Use pseudo code to plan your code.
    • Hint: LinefeedWatch out for \n. getline() includes the \n but it will mess up exec().
    • Hint: Use execl() execl() needs to be passed the command to run, and the command line arguments. The first command line argument must be the name of the program you are running. So just pass the input from getline().
    • Don't Google it; you need these skills. Labs are for figuring it out, not for completing work.

3. Implement it!


  • Inside your cmpt201 Docker container, create a file named lab2.c in your private Git repo.
  • Write a couple lines of code then compile, run, debug. Repeat!
  • Do simplest thing first, then add functionality bit-by-bit.
  • Hint - Suggested order
    1. Start with reading user input from the keyboard and then just printing it to the screen. See Lab 1!
    2. Then work on tokenizing it.
    3. Then get execl() working. It will exec once, which kills your program.
    4. Finally make your program fork() a new process for execl(). Have it wait using waitpid().
  • Help others around you!


4. Reviewing


  • During the last 15 minutes, TAs will show a sample solution.
  • TAs will talk through how the solution works and discuss its implementation.
  • Suggested discussion points:
    1. Briefly show how to get user input with strtok_r().
    2. Show how execl(), and focus discussion on passing the name of the application to run as the first command line argument for the application.
    3. Show how fork() is used, and how it works with waitpid().


5. Optional Challenges


You can try these if you like; however, we are not marking them! They are just for your own learning.

  1. Optional: Make your program exit when the user types in nothing and just presses ENTER.
  2. Optional: Make your program exit when the user types in "exit".
  3. Optional: Test your program with extra spaces. Does it handle it correctly?
  4. Optional: Make your program tell you a joke if the user types in "'joke'"! Make sure your joke is funny. :)


Submission


Submit your lab2.c C code to CourSys; the file name must be an exact match to what CourSys is expecting, otherwise it won't accept it.

Submissions will be marked for completion. It must be valid C code that runs (however we are unlikely to actually compile and run the code). You do not need to complete any optional steps.

If you were working on a CSIL machine, delete your docker container: docker rm cmpt201
(Don't do this if you were on your own laptop!)