170 likes | 303 Views
제 27 강 : estabur(). struct user k-stack. PAR PDR. Kernel a.out. sh (parent). < KERNEL>. ls (child). struct user k-stack. sh a.out. u--stack. At this point, context switch: kernel points to new image. PAR PDR. struct user k-stack. ls a.out.
E N D
제27강 : estabur() struct user k-stack PAR PDR Kernel a.out sh (parent) < KERNEL> ls (child) struct user k-stack sh a.out u--stack At this point, context switch: kernel points to new image PAR PDR struct user k-stack ls a.out u--stack < USER >
struct user k-stack PAR PDR Kernel a.out sh (parent) < KERNEL> ls (child) retu() struct user k-stack sh a.out u--stack At this point, context switch: kernel points to new image PAR PDR struct user k-stack ls a.out u--stack < USER >
Chapter Seven • After copying parent’s image, … • expand() 2250 • change the size of allocated space for user program • expand or shrink memory space • internally, calls malloc() & mfree() • estabur 1650 (7-4) • now user space addresses are determined • keep note in struct user • text, data, stack addresses • invoked whenever expand/shrink/relocated • sureg (1739) (7-4) • loads these mapping addresses from struct user • into CPU (user) mapping registers text data stack
Chapter Seven • After copying parent’s image, … • expand() 2250 • change the size of allocated space for user program • expand or shrink memory space • internally, calls malloc() & mfree() • estabur 1650 (7-4) • now user space addresses are determined • keep note in struct user • text, data, stack addresses • invoked whenever expand/shrink/relocated • sureg (1739) (7-4) • loads these mapping addresses from struct user • into CPU (user) mapping registers text PAR PDR data < USER > stack
PAR PDR Chapter Seven retu() < KERNEL > user k-stack • After copying parent’s image, … • expand() 2250 • change the size of allocated space for user program • expand or shrink memory space • internally, calls malloc() & mfree() • estabur 1650 (7-4) • now user space addresses are determined • keep note in struct user • text, data, stack addresses • invoked whenever expand/shrink/relocated • sureg (1739) (7-4) • loads these mapping addresses from struct user • into CPU (user) mapping registers text PAR PDR data < USER > sureg() stack
struct user k-stack PAR PDR Kernel a.out 1. Parent is creating child 2. fork() allocate memory space for child expand()malloc() 1628 Address for child is determined text, data, stack addresses CPU needs this addr. whenever ls runs Save addresses in struct user u estabur() 1629 3. To run Pchild CPU’s kernel -PAR must point to Pchild retu() 2228 now, u belongs to arising Pchild CPU’s user - PAR must point to Pchild load addresses from (new) u sureg() 2229 < KERNEL> struct user k-stack retu() sh a.out u--stack PAR PDR struct user k-stack sureg() ls a.out u--stack < USER >
1. Creating a new process 2. Allocate memory space for new process expand()malloc()1628 Address for new process is determined CPU need this addr. whenever this proc runs So save this address in struct user estabur()1629
swtch(1) wakeup setrun Current process retires Process #0 runs
3. Time to run Pchild CPU’s kernel level PAR must point to Pchild retu() 2228 Now, u belongs to arising Pchild CPU’s user level PAR must point to Pchild load addresses from (new) struct user u sureg() 2229 swtch(2)
General Register Address Mapping Registers R0 R1 R2 R3 R4 EP EP(K) SP SP(K) PAR PDR PAR PDR User KERNEL Interrupt Handler retu() sureg() retu() CPU & Context Switch CPU
Ch 8 When does process switching occur? [Case 1] • When active process voluntarily gives up CPU • eg tape read cannot be completed immediately • sleep() swtch() savu() retu() … [Case 2] • CPU is interrupted by urgent job during user mode [Case 3] • CPU was interrupted by urgent job during kernel mode • CPU not preempted. Before kernel returns to user mode … • Kernel tests runrun to check if urgent process is waiting • If yes, call swtch() • remember critical section problem,… • kernel mode CPU is not preempted immediately • interrupt handler just sets the runrun variable instead
1627 1550 main() 1826 newproc() 1637 1940 sched() 1968 2066 sleep() 2084 2178 swtch()
calls sleep() sched Swapping Manager
PS (line # 0164) --- CPU PSW (see 2095) • rp is proc[] of current process • waiting event • update proc[] state • priority • no interrupt here please (mutual exclusion) • interrupt handler also changes process state • swtch() process running in kernel mode calls sleep() to give up CPU Waiting event -- eg &buffer: use address as a bit pattern Has been saved before swtch( ) at 2065 Will not return for a very long time
wakeup( ) (2113) (8-3) • Wakes up ALL processes waiting for chan: • i.e. (i from 0 to NPROC) • Why? • Some events are exclusive eg buffer for DMA • Other events are shared eg “Printer Power ON” • So, the caller of sleep() must determine … • calls setrun() • setrun ( ) (2134) • just change process state in proc[] – SRUN = ready • Is waking process higher priority than current process? • this process will be dispatched later • at that point (on return from sleep()), • waking process checks • whether the reason for sleeping has gone away • in other words – it could be a premature return …. • (eg -- other process took away the only buffer …) swtch(1) wakeup setrun