Programming Assignment I
Due on Monday, February 11, 2019 @ 11pm
100 points
A Fake CPU Scheduler
In this assignment, you are to write a program, using signals
to implement a fake scheduler.
First, study the following signals:
SIGKILL,
SIGTERM,
SIGTSTP,
SIGSTOP
and
SIGCONT.
You may use some of them and others in your program.
The major part of this programming assignment is to implement the following:
- Write a short program as follows:
Wtite a program process.c
that takes a positive integer from its command line argument,
and starts an infinite loop.
In each iteration, it prints a line of the following format:
∅∅...∅Process XXXXX (Y) prints ZZZ
where XXXXX is the PID of the running process,
Y is the value taken from the command line argument,
and ZZZ is the iteration counter (i.e., 1, 2, 3, 4, 5, ...).
The string "∅∅...∅" represents a sequence of leading spaces.
The number of spaces is equal to the value taken from the command line argument.
For example, if the command line argument is 3, then there should be
exactly 3 leading spaces.
- Write your main():
Your main()
creates five child processes,
loads the program you wrote in Step (1)
and exetues them with command line argument from 1 to 5.
More precisely, the first copy receives 1,
the second copy receives 2, and so on.
Right after loading and executing each program,
main()
suspends that program immediately.
It is certainly possible that the just loaded and executed program
could print a few lines before its suspension.
Name your main() as
prog1.c
- Executes these five child processes in a round-robin order:
Each process is assigned a time quantum.
Once it is allowed to run, it can use the CPU for that amount of time quantum.
Because after all five processes are loaded, executed and finally suspended,
you are able to resume the execution of each process in whatever order.
Suppose the processes are named as
P1,
P2,
P3,
P4
and
P5
based on the value passed to it from its command line argument.
This value is referred to as the process number.
Then, you are able to resume them in the order of
P1,
P2,
P3,
P4
and
P5.
More precisely, you should allow P1
to run for a time quantum and suspend it.
Then, resume P2 to run
for a time quantum and suspend it, and so on.
The default time quantum is 1 second.
Note that in this way only one of the five child processes runs at any time.
- Write a Ctrl-C handler:
This Ctrl-C handler should be
activated when the system detects a
Ctrl-C.
- A Shell:
The Ctrl-C handler should
(1) suspend the currently running process,
and
(2) transfer the control to this shell for further processing.
Once the shell is entered, it should display the following prompt:
Shell ->
This shell should start accepting commands until it encounters
e,
E,
r or
R. Each
command starts with a single character and possibly followed by
one or more integers.
Note that error checking is required.
- e
or E (exit): Exit the system.
Thus, if a user enters e
or E,
your program should terminate immediately.
Note also that all of the remaining running processes should also be terminated.
- k
or K (kill): Kill a process.
This command requires a non-negative integer argument, giving the process number to be killed.
For example,
Shell -> k 2
Process 2 has been killed.
A killed process should not run again.
If the integer argument indicates a process that does not exist (e.g., killed previously),
you should display a message.
For example, if prosses 3 was killed sometime ago,
then
Shell -> k 3
Process 3 does not exist.
- r
or R (resume): Exit the shell and resume the process
that was suspended when the Ctrl-C
handler took over the control.
However, if the most recent process was killed or suspended,
the shell should resume the next "runnable" process in the new scheduling order.
This process should receive a full, new time quantum.
If no process is runnable, the shell should print a message
and wait for the next command from the user.
- s
or S (suspend):
This command requires a non-negative integer, giving
the process number to be suspended. For example,
Shell -> s 2
Process 2 has been suspended.
After a process is suspended, it should not run until it is resumed/activated
again (by command a
or A).
If the process number gives a killed, a
suspended, or even a non-exist process, you should display a message:
Shell -> s 2
Process 2 does not exist
Shell -> s 2
Process 2 was suspended
- a or A
(resume/activate a suspended function):
This command also requires a non-negative integer argument,
indicating the process number that should be resumed.
The process to be resumed/activated must be a suspended one;
otherwise, you should display a message:
Shell -> a 2
Process 2 has been activated
Shell -> a 2
Process 2 is not a suspended one.
- o
or O (change execution order):
This command changes the execution order of processes.
It requires a permutation of integers 1, 2, 3, 4 and 5.
For example,
Shell -> o 3 2 4 1 5
The execution order is now changed to 3 2 4 1 5
If all five processes are currently active, then the execution order becomes
P3,
P2,
P4,
P1 and
P5.
However, if one or more of these processes were suspended or killed,
the new order assigned to them is simply ignored.
More precisely, if process P3 was
suspended and process P1 was killed,
then the new execution order is
P2,
P4 and
P5.
In this case, your Shell should generate the following message to inform the user:
Shell -> o 3 2 4 1 5
Process 3 was suspended. Assigned order ignored.
Process 1 was killed. Assigned order ignored.
The execution order is changed to 2 4 5
However, after a suspended process is resumed after this change,
it should be executed in the given order.
For example, if P3 is
resumed sometime later before another reordering, then the order is
P3,
P2,
P4 and
P5.
- t or T (set time quantum):
This command requires a positive integer argument, giving the new time quantum.
For example,
Shell -> t 2
Time quantum has been set to 2 seconds
The above sets the time quantum to 2 seconds.
Note that the shell should display a message about this change.
This new time quantum also affects the process
that is suspended due to a Ctrl-C key press.
When this process resumes right after exiting the shall,
it is run with this new time quantum.
- l or L (list process status):
This command lists the status of the five processes.
Note that the output format can be a little different;
but, the needed information must be shown in the following way.
Shell -> L
PID Number Status
xxxxx 1 Running
xxxxx 2 Killed
xxxxx 3 Running
xxxxx 4 Suspended
xxxxx 5 Running
Input and Output
There is no input file.
The output has already been defined in the
process.c
and in the decription of the shell.
Program Specifications
Please note the following specifications. Without observing these rules,
you may risk lower grades.
- You have to use functions in file
process.c
and prog1.c.
- You have to implement all commands.
While your shell is processing command input and output, all created processes must be
suspended so that the shell will not be interfered.
Since the system may not be able to keep
up with your Ctrl-C key press, you might want to quickly hit
Ctrl-C a few times.
- Use signal.h and
signal() to catch and handle signals.
Other methods are simply unacceptable.
- Use alarm() system call
to simulate a time quantum.
Submission Guidelines
- All programs must be written in ANSI C.
Use gcc to compile your program.
Because different systems may have different default settings,
you should assume our grader will use C89 standard for compiling your programs.
- Use the submit command to submit your work.
You can submit as many times as you want, but only the last on time one will be graded.
A makefile is required.
Name your executable prog1.
- I will use the following approach to compile and test your
programs:
make <-- make your program
prog1 <-- test your program
I will supply enough number of commands to test your shell and
run it long enough to see if your scheduler handles
Ctrl-C and other signals correctly.
- Your implementation should fulfill the program specifications
as stated. Any deviation from the specification will cause you to
receive zero point.
- No late submission will be graded.
- Please note the following:
- Your programs must contain enough meaningful comments.
- For each submitted file, the first piece should be a program
header to identify yourself like this:
/* ----------------------------------------------------------- */
/* NAME : John Smith User ID: xxxxxxxx */
/* DUE DATE : mm/dd/yyyy */
/* PROGRAM ASSIGNMENT # */
/* FILE NAME : xxxx.yyyy.zzzz (your unix file name) */
/* PROGRAM PURPOSE : */
/* A couple of lines describing your program briefly */
/* ----------------------------------------------------------- */
For each function in your program, include a simple description
like this:
/* ----------------------------------------------------------- */
/* FUNCTION xxyyzz : (function name) */
/* the purpose of this function */
/* PARAMETER USAGE : */
/* a list of all parameters and their meaning */
/* FUNCTION CALLED : */
/* a list of functions that are called by this one */
/* ----------------------------------------------------------- */
-
Not-compile programs receive 0 point.
By not-compile, I mean any reason that could cause an
unsuccessful compilation, including missing files, syntax
errors, incorrect makefile and so on. Double check your files
before you submit, since I will
not
change your program to make it work.
-
Compile-but-not-run programs receive 0 point.
Compile-but-not-run means you have attempted to solve the problem
to certain extent but you failed to make it working properly.
A meaningless or vague program receives no point even though it compiles successfully.
Another common problem is that your makefile could successfully process your
source files (i.e., compiled successfully);
but, your makefile does not generate an executable file with the desired
filename (i.e.,
prog1 in this assignment).
If this happens, I will consider your submission as compile-but-not-run.
- Submit a one-page description of your solution, discussing
the following issues:
- The logic of your program
- Why does your program work?
- You have to describe the use of each signal handler
and the corresponding signal.
This part is extremely important.
If I don't understand it, I will assume your solution is at best partially correct.
- If your program encountered the minor problem mentioned earlier,
how did you overcome this issue? Provide a detailed account
of your solution.
Name this file README
rather than readme or
Readme.
Note also that there is no
filename extension.
Hence, filename such as
README.TXT
is not acceptable.
README must
be a plain text file. We do not accept files produced
by a word processor. Moreover, watch for very long lines.
More precisely, limit the length of each line to no more than
80 characters with the Return key.
- Your submission should include the following files:
- File prog1.c
contains your main program and all
functions, signal handlers included.
- File process.c
contains the code to be executed as a child process,
- File Makefile
is a makefile that compiles the above file and
generates the executable files
prog1.
- File README.
-
Programs submitted to wrong class and/or wrong sections
will not be graded.
-
Always start early, because I will not grant any extension if
your home machine, your phone line or the department machine
crashes in the last minute.
- Since the rules are all clearly stated
above, no leniency will be given. So, if you have anything in doubt,
please ask for clarifications.
-
Click here to see how your
program will be graded.