The purpose of this lab is to give gain some experience working with system calls and learn about process management
(process creation, termination etc). In this lab, you will write C programs making use of several UNIX system calls.
You will also need to write a design document explaining how your program(s) are organized (details below).
Part 1: Process Creation
This component of the lab requires you to use the fork system call. Write a program using fork() where
a parent process creates a child process and a child process creates a grand-child. The grand-child process
sleeps for 10 seconds and exits after printing "Grandchild process with process id [PID] has completed."
The child process should wait for the grand-child to finish and once it does so, it should exit after printing
"Child process with process ID [PID] has completed." The parent process should wait for the child process
to finish and then exit after printing "Parent process with process ID [PID] has finished.
A process may use the waitpid() system call to wait on a process and the getpid() call to obtain its own process ID.
Be sure to read the manual ("man") pages for these system calls to understand how to use them in your program.
These are also available online here
Part 2: Process termination
Write a program that functions as a process termination utility (which we will refer to as the kill utility).
Your program should prompt the user to enter a process id and then use the kill system call to terminate it.
Be sure to handle error cases carefully, including errors where the user does not have privileges
to terminate the specified process, or the user specifies an non-existent process. The return values
from the kill system calls will indicate whether the call succeeded or what type of error occured.
Part 3: Batch Scheduler
The last component of the lab involves writing a batch scheduling shell. This shell program
takes as input a sequence of batch programs to run. The shell then runs these programs sequentially
one after another. This is done by forking a child process and having it use exec to run the specified
program. The parent shell process waits for the child to finish and then runs the next program in a same
fashion.
Your batch program should be able to take an arbitrary list of programs from the command line
and then run them one after another.
user@elnux2$ ./batch-shell
batch-shell> program1 program2 program3 program4
output of program1
output of program 2
output of program 3
output of program 4
batch-shell> quit
user@elnux2
Additionally, each new process your program creates should print its process ID before executing the command,
as well as any other output you would like that demonstrates how your program is operating.
Tips
-
Useful functions and system calls include fork,
exec
sleep,
waitpid, and
kill. You should use the SIGKILL signal value in kill to terminate a process.
-
If you're trying to use waitpid and get a warning like "warning: implicit declaration of function 'waitpid'",
you probably need to include an additional system header file. Add the line "#include <sys/wait.h>"
to the top of your file alongside the other #include statements.
-
Be careful when adding calls to fork -- if you write an infinite loop with a fork in it,
a fork bomb will result. If in doubt, you can add a sleep(1);
before each fork during debugging, which will slow the rate of process creation.
-
If you have difficulties with C syntax or errors, email or see the TA. While minimal features of C are required for
this assignment, we don't want you to spend too much time debugging issues with C itself.
Design Document and Observation
Once you have written code for all three parts, write a design
document that documents your design choices. In addition to the design document, write detailed comments
in the source code to explain your code.
How to Turn in Lab 1
All of the following files must be submitted on Moodle as a zip file in the form FirstnameLastname.zip to get
full credit for this assignment.
-
Include a copy of all source files.
-
Include a README file containing an outline of what you did.
It should also explain and document your design choices. Keep it short
and to the point. If your implementation does not work, you should document the
problems in the README, preferably with your explanation of why it does
not work and how you would solve it if you had more time. Of course,
you should also comment your code. We can't give you credit for something
we don't understand!
-
Finally, include a file showing sample output from running your program.
-
Note: Please make sure you turn in your
own work. We will strictly enforce policies on cheating.
Remember that we routinely run similarity checking programs on your
solutions to detect cheating.
You should be very careful about using code snippets you find on
the Internet. In general your code should be your own. It is OK to
read tutorials on the web and use these concepts in your
assignment. Blind use of code from web is strictly disallowed. Feel free to
check with us if you have questions on this policy. And be sure to CLEARLY document any
Internet sources/ tutorials you have used to complete the assignment
in your README file.
We will check and flag snippets of code borrowed
from various internet sites, so be sure to write your own code.
Lab 1 Grading Scheme
(max 100) Total Grade
- (20) part 1 on process creation
- (10) part 2 on process termination
- (70) part 3 on batch scheduler
- These grading will be based on four criteria: correctness, code comments, code structure and design document
Late Policy: Lab 1 is due at 5 PM on Tuesday, October 13.
Please refer to the course syllabus for late
policy on lab assignments. This late policy will be strictly
enforced.
Please start early so that you can submit the assignment on time.