Lab 2 - fork() - exec() - waitpid()
1. Setup (2 mins)
- Labs are a co-operative time! Introduce yourself to someone!
- You can work individually or in pairs (pair-programming).
- Help others!
- Use either your own laptop or the lab PC.
- Only if using CSIL lab PC:
- [Only CSIL PC] If in Windows, reboot to Linux.
- While booting, select Ubuntu from boot menu.
- [Only CSIL PC] Don't setup GitHub tokens in container because it may be shared with other users.
- [Only CSIL PC] Delete any possible previous docker container:
docker rm cmpt201
- [Only CSIL PC] If in Windows, reboot to Linux.
- Launch the docker container.
- See Resources page for commands.
- You do not need to use
.record
2. Design (10 mins)
Your task is:
Write a program that does the following:
- It receives a user input that is a full path command
e.g.,/usr/bin/ls
,/usr/bin/nvim
,/usr/bin/ip
, etc.- You do not need to handle commandline arguments
- It executes the command.
- It repeats the above two 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)
- Design your solution.
- Use man pages.
- Discuss with your partner, or someone else, how each of these functions works.
- Hints
- Use pseudo code to plan your code.
- Watch out for
\n
.getline()
includes the\n
but it will mess upexec()
. - Use execl(). It 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! (20 mins)
- Write a couple lines of code then compile, run, debug. Repeat!
- Do simplest thing first (read input and output it?). Then add functionality bit-by-bit.
- Help others around you!
4. Reviewing (10 mins)
- During the last 10 minutes, TAs will show a sample solution.
- TAs will talk through how the solution works and discuss its implementation.
Nothing is submitted for this lab. Thank you for learning!
If you were working on a CSIL machine, delete your docker container:
docker rm cmpt201