LINUX System Lecture 7 Process BongSoo Sohn Lecture
LINUX System : Lecture 7 Process Bong-Soo Sohn Lecture notes acknowledgement : The design of UNIX Operating System
Process Management 2
What is Process? • Definition – – – an instance of a running program (runnable program) an execution environment of a program scheduling entity a control flow and address space PCB (Process Control Block) : proc. table and U area • Manipulation of Process – create, destroy – context – state transition • dispatch (context switch) • sleep, wakeup • swap
Process State Transition user running syscall, interrupt fork initial (idle) return from syscall or interrupt exit kernel running swtch ready to run swap suspended ready zombie wait sleep, lock wakeup, unlock asleep swap suspended asleep (Source : UNIX Internals)
Context • context : system context, address (memory) context, H/W context memory proc table file table segment tablepage table fd Registers (TSS) U area eip sp eflag s eax …. cs …. swap disk
Context : system context • System context – proc. Table • • identification: pid, process group id, … family relation state sleep channel: sleep queue scheduling information : p_cpu, p_pri, p_nice, . . signal handling information address (memory) information – U area (swappable information) • • • stores hardware context when the process is not running currently UID, GID arguments, return values, and error status for system call signal catch function file descriptor usage statistics
Context : address context • fork example int char glob = 6; buf[] = “a write to stdoutn”; int main(void) { int var; pid_t pid; var = 88; write(STDOUT_FILENO, buf, sizeof(buf)-1); printf(“before forkn”); if ((pid = fork()) == 0) { glob++; var++; } else sleep(2); } /* child */ /* parent */ printf(“pid = %d, glob = %d, var = %dn”, getpid(), glob, var); exit (0); (Source : Adv. programming in the UNIX Env. , pgm 8. 1) * guess what can we get from this program?
Context : address context • fork internal : compile results gcc test. c header text 0 xffff 0 xbfffffff kernel stack … movl %eax, [glob] addl %eax, 1 movl [glob], %eax. . . glob, buf data bss var, pid stack 0 x 0 data text a. out user’s perspective (virtual address) Executable and Linking Format
Context : address context • fork internal : before fork (after run a. out) memory proc T. segment T. text var, pid = 11 stack glob, buf data cf) we assume that there is no paging mechanism in this figure.
Context : address context • fork internal : after fork proc T. segment T. pid = 11 memory glob, buf data text var, pid stack proc T. segment T. glob, buf pid = 12 data stack – address space : basic protection barrier var, pid
Context : address context • fork internal : with COW (Copy on Write) mechanism after fork with COW proc T. memor y segment T. pid = 11 after “glob++” operation proc T. text segment T. pid = 11 text stack proc T. pid = 12 segment T. stack proc T. data pid = 12 segment T. data
Context : address context • execve internal proc T. pid = 11 segment T. memory data text a. out stack text data stack 12 header text data bss stack
Context : hardware context • time sharing (multitasking) Where am I ? ? time quantum process 1 process 2 process 3 13 …
Context : hardware context • brief reminds the 80 x 86 architecture ALU IN Control Unit OUT Registers eip, eflags eax, ebx, ecx, edx, esi, edi, … cs, ds, ss, es, . . . cr 0, cr 1, cr 2, cr 3, GDTR, . . . 14
Context : hardware context • context swtch save context Proc T. TSS eip sp eflags eax CPU Proc T. cs U area 15 restore context TSS eip sp eflags eax cs U area
Context : hardware context • context swtch : pseudo-code in UNIX … /* need context swtch */ if (save_context()) { /* pick another process to run from ready queue */ …. restore_context(new process) /* The control does not arrive here, NEVER !!! */ } /* resuming process executes from here !!! */ …. . . (Source : The Design of the UNIX OS) 16
- Slides: 16