1 Scheduling Context Switching
1 Scheduling Context Switching
Context Switching
Process Table
stores all information about all processes
one entry per process
Context switching
at the heart of the juggling process stopping current computation save information to restart restart another process CPU cannot be stopped
must continue to execute the code that switches to a new process
October 06 Scheduling & Context Switching 1 October 06
Context Switching
Information about processes
Process state Program counter CPU registers CPU scheduling information Memory-management information Accounting information I/O status information
Scheduling & Context Switching 3
Context Switching
what information must be saved
any values that will be destroyed For example each process has its own memory stack
A copy of the stack need not be saved
new process may change register values, but these are saved on the stack
October 06
Context Switching
struct pentry /* entry in proctab */ { int pprio; /* process priority */ int psem; /* semaphore if process waiting */ int pmsg; /* message sent to process */ int phasmsg; /* nonzero iff msg valid */ char *pregs; /* saved regs */ char *pbase; /* base of run time stack */ word plen; /* stack length */ char pname[PNMLEN+1]; /* process name */ int pargs; /* initial number of arguments */ int (*paddr)(); /* initial code address */ };
October 06 Scheduling & Context Switching 5
Context Switching
processes are referenced by their process id, which is the index of the saved state information in proctab pstack field
stores information needed to restart the process
Context Switching
system uses pstate field
what is process doing validity and semantics of operations performed on it
Context Switching
XINU uses six states
current, ready, receiving, sleeping, suspended, waiting
defined as
PRCURR, PRREADY, PRRECV, PRSLEEP, PRSUSP, PRWAIT
Context Switching
current and ready ready
eligible for CPU service but not currently executing
Context Switching
context switching software
Scheduler
current
single process receiving CPU service
switching context
select process from those that are ready give control of CPU to selected process
round robin
all members of a set have an opportunity to execute before any member has a second chance
9 October 06 Scheduling & Context Switching 10
October 06
Context Switching
Priority
kept in the pprio field some integer stored to give control over selection process in some systems priority is adjusted from time to time based on observed behaviour
Context Switching
rescheduling process - resched
examines and updates list process priority is the key by which lists are sorted highest priority process is found at the tail
October 06
12
Context Switching
what happens to current process during context switch
if it is to remain ready, it is passed to the ready list
state will be PRREADY
Context Switching
if current process will not remain eligible for CPU
sys routines assign next state before calling resched resched checks state and makes ready only if state is current
October 06
13
October 06
14
Context Switching
resched
moves current process to ready list also completes every detail of scheduling and context switching except saving and restoring machine registers and switching stacks selects process,changes table entries, removes it from ready table, marks it current, finally calls another procedure (ctxsw) to switch stacks and restore registers
October 06 Scheduling & Context Switching 15
/* resched.c - resched */ #include files here /*-----------------------------------------------------------------------* resched -- reschedule processor to highest priority ready process * * Notes: Upon entry, currpid gives current process id. * Proctab[currpid].pstate gives correct NEXT state for * current process if other than PRCURR. *----------------------------------------------------------------------- */ void resched(void) { struct pentry * optr; /* pointer to old process entry */ struct pentry * nptr; /* pointer to new process entry */ proctab[currpid].time = proctab[currpid].time + tod - proctab[currpid].oldtime; optr = & proctab[currpid];
October 06
16
if (optr->pstate == PRCURR) { /* no switch needed if current prio. higher than next */ if (lastkey (rdytail) < optr->pprio) {optr->oldtime = tod; preempt = 20; return;} /* or if rescheduling is disabled ( pcxflag == 0 ) */ if (sys_pcxget() == 0) {optr->oldtime = tod; preempt = 1; return;} /* force context switch */ optr->pstate = PRREADY; insert(currpid, rdyhead, optr->pprio); } else if (sys_pcxget() == 0) /* scheduling is permitted */ { kprintf("pid=%d state=%d name=%s", currpid, optr->pstate , optr->pname); panic("Reschedule impossible in this state"); }
October 06 Scheduling & Context Switching 17
/* remove highest priority process at end of ready list */ nptr = & proctab[(currpid=getlast(rdytail))]; nptr->pstate = PRCURR; /* mark it currently running */ preempt = 20; _pglob = nptr->pglob; /* retrieve global environment */ proctab[currpid].oldtime = tod; ctxsw(&optr->pregs, & nptr->pregs); /* The OLD process returns here when resumed. */ if (currpid != 0 && FP_OFF( optr->pregs) > optr->plen) panic("stack overflow"); if (optr->phastrap) { optr->phastrap = FALSE; /* mark trap as serviced */ if (optr->ptfn != NULL) (*optr->ptfn)(optr->ptarg); } }
October 06 Scheduling & Context Switching 18
Context Switching
resched uses ctxsw to save the process state and change stacks
registers and stack pointers cannot be manipulated in high level language ctxsw is then machine dependent
Context Switching
ctxsw
saves the registers to the stack
in a sense freezes stack by capturing value of stack pointer precisely
ctxsw
saves the FLAGS register
contains interrupt state of the process
stack for the new process is uploaded and the CPU works with this when it returns, the instruction pointer is set to code for new process parameter pregs is used to obtain the stack environment of new process
October 06 Scheduling & Context Switching 20
Context Switching
all processes call resched to perform context switching
resched calls ctxsw therefore all processes resume at the same place - just after the call to ctxsw but they go in different directions (different stack of procedure calls)
Context Switching
resched switches among current and ready processs
does not create new assumes that at least one process is available does not verify if list is empty
resched can only switch context from one process to another, so at least one process must always remain ready to run
this process is the NULL PROCESS
October 06 Scheduling & Context Switching 22
October 06
21
Context Switching
Null Process
process with priority zero, infinite loop always ready to run user process priority must be > 0 scheduler switches to the null process only when no user process remains ready to run
October 06
23