Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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 kernel running swtch swtch ready to run swap suspended ready zombie exit wait sleep, lock wakeup, unlock asleep swap suspended asleep (Source : UNIX Internals) Context • context : system context, address (memory) context, H/W context proc table file table memory segment tablepage table fd Registers (TSS) eip sp U area eax eflag …. s 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 stdout\n”; int main(void) { int var; pid_t pid; var = 88; write(STDOUT_FILENO, buf, sizeof(buf)-1); printf(“before fork\n”); if ((pid = fork()) == 0) { glob++; var++; } else sleep(2); } /* child */ /* parent */ printf(“pid = %d, glob = %d, var = %d\n”, 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 0xffffffff 0xbfffffff kernel stack … movl %eax, [glob] addl %eax, 1 movl [glob], %eax ... glob, buf data bss var, pid stack 0x0 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 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. memory segment T. pid = 11 after “glob++” operation proc T. text segment T. pid = 11 text stack proc T. segment T. stack proc T. pid = 12 data data segment T. pid = 12 data Context : address context • execve internal memory proc T. pid = 11 segment T. 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 80x86 architecture ALU IN Control Unit OUT Registers eip, eflags eax, ebx, ecx, edx, esi, edi, … cs, ds, ss, es, ... cr0, cr1, cr2, cr3, GDTR, TR, ... 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