Programming Assignment V

Due on Friday, November 20, 2009 @ 11pm

70 points

A Fake CPU Scheduler

In this assignment, you are to write a program, using signals (and "non-local" gotos if you like), to implement a fake scheduler. You do not and should not use ThreadMentor.

The major part of this programming assignment is to implement the following:

  1. Periodically call one of the several given functions
    Each function is assigned a time quantum. Once it is called, it can use the CPU for that amount of time (i.e., real time rather than CPU time). The call to these functions is in a round-robin order. Suppose the time quantum is set to 1 second and there are three functions, f1(), f2() and f3(). Then, f1() is called and runs for 1 second (i.e., real time); f2() is called and runs for 1 second; f3() is called and runs for 1 second; f1() is called and runs for 1 second; and so on. The default time quantum value is 1 second.
  2. A Ctrl-C handler
    This Ctrl-C handler should be activated when the system detects a Ctrl-C.
  3. A Shell
    The Ctrl-C handler should 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.

An Important Note

Your program may or may not encounter a minor problem when running on Fedora; however, it is easy to fix if you read some Unix manual pages. This is an exercise!

Bonus Points

Here are two options that can help increase the number of points you can get. Option 2 includes Option 1 as a subset. Consequently, you have to figure out how to do Option 1 before thinking about Option 2.

Option 1: Study the available signals on Fedora, and you should be able to find that some signals can be used to stop and continue a process. Next, write a main() that includes and compiles funct5.c into your program. Then, when your prog5 starts, it creates five processes, say P1 to P5 so that process P1 calls function f1(), process P2 calls function f2(), etc. Your shell should use signals to suspend and resume these processes rather than by calling the functions. Other than the functions being replaced by processes, the shell should work the same way.

Option 2: The core of this option is identical to that of Option 1. The difference is that each process should run in its own window, while the shell runs in a shell window. More precisely, when your prog5 starts, the shell runs in a window and displays the shell prompt. Each process will create its own window and use this window for output purpose. Therefore, there are six windows in total. In this way, the running activity of each process can clearly be seen as processes are run in separate windows. Since your shell runs in a separate window, the resume (r or R) command has no effect. Note that creating text windows is not very difficult as long as you know where to find the document.

Important Notes

  • For both options, all processes and their windows must be removed when they are killed and upon the exit of the shell.
  • A successful implementation of Option 1 receives extra 20 points, and a successful implementation of Option 2 receives extra 30 points.
  • You should print the following lines once your program is run so that our grader would know the option you choose, where the X is either 1 or 2, depending on your choice.
    ********************************
    ***  Option X for Program #5 ***
    ********************************
    
  • You should make sure all paths and library search paths are correct, and should not assume the machine for grading would have the same path and environment setup like your local machine.
  • In your README file, you should explain your implementation in detail. Therefore, you are allowed to add one extra page for this purpose.

Input and Output

There is no input file. The output has already been defined in the funct5.c. There are five C functions, f1(), f2(), f3(), f4() and f5(), each of which produces its own output. The only output and possible input of your program are the commands to your Shell as shown in the previous section.

Click here to download a copy of funct5.c. Note that you should not change anything in this file, because the original version will be used for grading.

Program Specification

Please note the following specifications. Without observing these rules, you may risk lower grades.
  1. You have to use functions in file funct5.c for scheduling purpose. You have to link these functions with your program files.
  2. You have to implement all commands. While your shell is processing command input and output, all other events (e.g., Ctrl-C and alarm) must be masked off 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.
  3. Use signal.h and signal() to catch and handle signals. Other methods are simply unacceptable .
  4. Feel free to use setjmp() and longjmp() to switch back and forth between user functions and your scheduler, although they really are not needed.
  5. Use alarm() system call to simulate a time quantum.

Submission Guidelines

  1. All programs must be written in ANSI C. Use gcc to compile your program.
  2. Use the submit command to submit your work. You can submit as many times as you want, but only the last one will be graded. A makefile is required. Name your executable prog5. Please do not submit file funct5.c, because we may use a different funct5.c to test your program.
  3. I will use the following approach to compile and test your programs:
    make              <-- make your program
    prog5             <-- 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 alarm correctly.
  4. Your implementation should fulfill the program specification as stated. Any deviation from the specification will cause you to receive zero point.
  5. No late submission will be graded.
  6. My rule of grading is subtraction based. I assume you always receive 100%. If you missed something, some points will be taken off based on the following rules:
  7. Since the rules are all clearly stated above, no leniency will be given. So, if you have anything in doubt, please ask for clarifications.
  8. Click here to see how your program will be graded.