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:

  1. 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.
  2. 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
  3. 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.
  4. Write a Ctrl-C handler:
    This Ctrl-C handler should be activated when the system detects a Ctrl-C.
  5. 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.

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.
  1. You have to use functions in file process.c and prog1.c.
  2. 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.
  3. Use signal.h and signal() to catch and handle signals. Other methods are simply unacceptable.
  4. 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. Because different systems may have different default settings, you should assume our grader will use C89 standard for compiling your programs.
  2. 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.
  3. 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.
  4. Your implementation should fulfill the program specifications as stated. Any deviation from the specification will cause you to receive zero point.
  5. No late submission will be graded.
  6. Please note the following:
  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.