0% found this document useful (0 votes)
166 views4 pages

1 Scheduling Context Switching

Context switching allows a multi-programming operating system to switch the CPU between different processes to create the illusion that multiple processes are running concurrently by rapidly switching between them. It involves saving the state of the currently running process, such as its program counter, CPU registers, and scheduling information, and restoring the saved state of another process from the process table so the CPU can resume its execution. The key steps are to save the current process context, select a new process to run from the ready queue, restore the new process context, and start its execution. A null process with very low priority is always kept ready to ensure the CPU always has a process to run.

Uploaded by

own-mine
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
166 views4 pages

1 Scheduling Context Switching

Context switching allows a multi-programming operating system to switch the CPU between different processes to create the illusion that multiple processes are running concurrently by rapidly switching between them. It involves saving the state of the currently running process, such as its program counter, CPU registers, and scheduling information, and restoring the saved state of another process from the process table so the CPU can resume its execution. The key steps are to save the current process context, select a new process to run from the ready queue, restore the new process context, and start its execution. A null process with very low priority is always kept ready to ensure the CPU always has a process to run.

Uploaded by

own-mine
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Scheduling & Context Switching

illusion of concurrent processing


rapidly switching processor

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

one process running


its saved information is out of date

all other processes are temporarily stopped to switch context


OS saves information about currently running process in the process table restores information from the process table for process about to execute
Scheduling & Context Switching 2

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

information used to control processes and account for resources


October 06 Scheduling & Context Switching 4

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

other fields are for book keeping purposes


stack name, length, arguments passed when process created some used to free memory, others for debugging
October 06 Scheduling & Context Switching 6

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

system designer evolves set of process states


should be well defined before implementation systems uses these states when manipulating processes
October 06 Scheduling & Context Switching 7

also uses the value PRFREE


indicates that no process is using that process table entry
October 06 Scheduling & Context Switching 8

Context Switching
current and ready ready
eligible for CPU service but not currently executing

Context Switching
context switching software
Scheduler

XINU - procedure resched


at any time, the highest priority process eligible for CPU service is executing. Among process with equal priority, scheduling is round robin

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

Scheduling & Context Switching

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

global variables rdyhead and rdytail current process


does not appear on ready list, but its process id is always given by the global integer variable currpid

to make selection faster


processes in ordered list, highest priority at the head
October 06 Scheduling & Context Switching 11

October 06

Scheduling & Context Switching

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

may be moved to other states

but which state


no explicit parameter for resched
checks the pstate field system routines set this field depending on next state

sometimes necessary to suspend resched for critical system activities


allows exclusive access to CPU for one routine even with interrupts enabled

October 06

Scheduling & Context Switching

13

October 06

Scheduling & Context Switching

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

Scheduling & Context Switching

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

also saves the BP, SI and DI registers


C procedures assume no change in these across calls
October 06 Scheduling & Context Switching 19

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

Scheduling & Context Switching

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

Scheduling & Context Switching

23

You might also like