LAB2pre Work: Processes in an OS Kernel
                     CheckList
	     
Download samples/LAB2pre/mtx. Run it under Linux.
MTX is a multitasking system. It simulates process operations in a Unix/Linux
kernel, which include

        fork, exit, wait, sleep, wakeup, process switching

	     
Like Unix/Linxu kernel, the mtx system has these data structs: 
==================================================================
typedef struct proc{
    struct proc *next;      // next proc pointer
    int  *saved_sp;         // saved sp when process not running
 
    int   pid;              // pid = 0 to NPROC-1
    int   ppid;             // parent pid 
    int   status;           // PROC status: FREE|READY|etc 
    int   priority;         // scheduling priority 
    int   event;            // event to sleep on 
    int   exitCode;         // exit code value

    struct proc *child;     // first child PROC pointer
    struct proc *sibling;   // sibling PROC pointer
    struct proc *parent;    // parent PROC pointer

    int   kstack[SSIZE];    // processs stack                 
}PROC;

PROC proc[NPROC];  // NPROC(9) proc structures, each for a process
PROC *freeList;    // list of all FREE PROCs
PROC *readyQueue;  // priority queue of PROCs that are READY to run
PROC *sleepList;   // list of SLEEP procs, if any.
PROC *running;     // pointer to the current running PROC
========================================================================

Run mtx. It first initialize the system, creates an initial process P0.
P0 has the lowest priotiry 0, all other processes have priority 1

Ater initialization, P0 forks a child prcoess P1, switch process to run P1.

                The display looks like the following
-----------------------------------------------------------------------------
Welcome to KCW's Multitasking System
1. init system
freeList = [0 0]->[1 0]->[2 0]->[3 0]->[4 0]->[5 0]->[6 0]->[7 0]->[8 0]->NULL

2. create initial process P0
init complete: P0 running

3. P0 fork P1 : enter P1 into readyQueue = [1,1]->NULL
	     
4. P0: switch task
       readyQueue = [1 1]->[0 0]->NULL
       next running = 1
       P1 resume to body()

P1 running: Parent=0 childList=NULL
freeList  = [2 0]->[3 0]->[4 0]->[5 0]->[6 0]->[7 0]->[8 0]->NULL
readQueue = [0 0]->NULL
sleepList = NULL
input a command: [ps|fork|switch|exit|sleep|wakeup|wait] : 
----------------------------------------------------------------------------
5.                     COMMANDS:
ps     : display procs with pid, ppid, status; same as ps in Unix/Linux
fork   : fork a child process
switch : switch process
exit   : give children to P1, become a ZOMBIE, wakeup parent, tswitch()
         
sleep  : SLEEP on an event value
wakeup : wakeup SLEEP procs with event value
	     
wait   : wait for a ZOMBIE child
---------------------------------------------------------------------------

------------------------ TEST REQUIREMENTS ---------------------------------
6. Step 1: test fork
While P1 running, enter fork: What happens?__________________________________

Enter fork many times; 
      How many times can P1 fork? ________________ WHY?__________________

Enter Control-c to end the program run.


7. Step 2: Test sleep/wakeup
Run mtx again.
While P1 running, fork a child P2;
Switch to run P2. Where did P1 go?___________________WHY?______________________

P2 running : Enter sleep, with a value, e.g.123 to let P2 SLEEP.
What happens?_______________________WHY?______________________________________

Now, P1 should be running. Enter wakeup with a value, e.g. 234
Did any proc wake up?_____________________ WHY? ____________________________

P1: Enter wakeup with 123
What happens?________________________________WHY? __________________________


8. Step 3: test child exit/parent wait
	     
When a proc dies (exit) with a value, it becomes a ZOMBIE, wakeup its parent.
Parent may issue wait to wait for a ZOMBIE child, and frees the ZOMBIE

Run mtx;
P1: enter wait; What happens?________________ WHY?_________________

CASE 1: child exit first, parent wait later

P1: fork a child P2, switch to P2.
P2: enter exit, with a value, e.g. 123 ==> P2 will die with exitCode=123.
Which process runs now?_______________________ WHY?____________________
enter ps to see the proc status: P2 status = ? ____________________________

(P1 still running) enter wait; What happens?_______________________________
                   enter ps;   What happened to P2?__________________________

CASE 2: parent wait first, child exit later

P1: enter fork to fork a child P3
P1: enter wait;  What happens to P1?______________________ WHY?______________
P3: Enter exit with a value; What happens?___________________________________
P1: enter ps;  What's the status of P3?_________________ WHY? _________________
	     
9. Step 4: test Orphans
	     
When a process with children dies first, all its children become orphans.
In Unix/Linux, every process (except P0) MUST have a unique parent.
All orphans become P1's children. Hence P1 never dies.

Run mtx again.
P1: fork child P2, Switch to P2.
P2: fork several children of its own, e.g. P3, P4, P5 (all in its childList).
P2: exit with a value. 
P1 should be running WHY?___________________________________________________
P1: enter ps to see proc status: which proc is ZOMBIE?______________________
What happened to P2's children? ____________________________________________
P1: enter wait; What happens? ________________________________________________
P1: enter wait again; What happens?__________________ WHY?__________________

How to let P1 READY to run again?_________________________________________