processes address spaces and context switches
play

Processes, Address Spaces, and Context Switches Chester Rebeiro - PowerPoint PPT Presentation

Processes, Address Spaces, and Context Switches Chester Rebeiro IIT Madras Executing Apps (Process) Process A program in execution Most important abstraction in an OS Comprises of $gcc hello.c Code from ELF In the


  1. Processes, Address Spaces, and Context Switches Chester Rebeiro IIT Madras

  2. Executing Apps (Process) • Process – A program in execution – Most important abstraction in an OS – Comprises of $gcc hello.c • Code from ELF In the • Data user space • Stack of process • Heap ELF Executable Process • State in the OS In the kernel (a.out) space • Kernel stack $./a.out – State contains : registers, list of open files, related processes, etc. 2

  3. Program ≠ Process Program Process code + static and global data Dynamic instantiation of code + data + heap + stack + process state One program can create several A process is unique isolated entity processes 3

  4. Process Address Space • Virtual Address Map MAX_SIZE – All memory a process can Stack address – Large contiguous array of Heap addresses from 0 to MAX_SIZE Data Text (instructions) 0 4

  5. Process Address Space • Each process has a different address space • This is achieved by the use of virtual memory • Ie. 0 to MAX_SIZE are virtual memory addresses MAX_SIZE MAX_SIZE Stack Stack Heap Heap Data Data Process A Process A Process B Process B Process A Process B Text Text Page Table Page Table (instructions) (instructions) 0 0 5

  6. Virtual Address Mapping Process A Process A Process B Process B Stack Stack Heap Heap Process B Process A Data Page Data Page Table Table Text Text (instructions) (instructions) Virtual Memory Virtual Memory Physical Memory 6

  7. Advantages of Virtual Address Map • Isolation (private address space) – One process cannot access another process’ memory • Relocatable – Data and code within the process is relocatable • Size – Processes can be much larger than physical memory 7

  8. Process Address Map in xv6 • Entire kernel mapped into 0xFE000000 every process address space Kernel – This allows easy switching from Text + Data, user code to kernel code (ie. DeviceMemory Kernel can access during system calls) User Process can access KERNBASE • No change of page tables (0x80000000) needed Heap – Easy access of user data from Stack kernel space Data Text (instructions) 0 8

  9. Process Stacks Kernel (Text + Data) • Each process has 2 stacks – User space stack Kernel Stack • Used when executing user code for process – Kernel space stack • Used when executing kernel Heap code (for eg. during system calls) User stack for process – Advantage : Kernel can execute even if user stack is corrupted Data (Attacks that target the stack, such as buffer overflow attack, will not affect the Text (instructions) kernel) Process Address Space 9

  10. Process Management in xv6 • Each process has a PCB (process control block) defined by struct proc in xv6 • Holds important process specific information • Why? – Allows process to resume execution after a while – Keep track of resources used – Track the process state ref : proc.h (struct proc) (2353) 10

  11. Summary of entries in PCB • More entries Size of process memory Page directory pointer for process Kernel stack pointer later  later  Files opened Current working directory Executable name 11

  12. Entries in PCB • PID – Process Identifier – Number incremented sequentially • When maximum is reached • Reset and continue to increment. • This time skip already allocated PID numbers 12

  13. Process States • Process State : specifies the state of the process EMBRYO RUNNABLE SLEEPING RUNNING EMBRYO  The new process is currently being created RUNNABLE  Ready to run RUNNING  Currently executing SLEEPING  Blocked for an I/O Other states ZOMBIE (later) ref : proc.h (struct proc) 2350 13

  14. Scheduling Runnable Processes Running Process i n t e r r u p t e v e r y 1 0 0 m s Queue of RUNNABLE Processes CPU Scheduler Scheduler triggered to run when timer interrupt occurs or when running process is blocked on I/O Scheduler picks another process from the ready queue Performs a context switch

  15. Page Directory Pointer Page Directory Pointer Stack Process A Process A Heap Process A Data Page Table Text (instructions) Virtual Memory Physical Memory 15

  16. Entries in PCB SS • Pointer to trapframe ESP EFLAGS CS EIP Error Code trapframe Trap Number ds es … eax ecx … esi edi esp (empty) 16

  17. Context Pointer Kernel (Text + Data) • Context pointer – Contains registers used for context switches. Kernel Stack for process – Registers in context : %edi, %esi, %ebx, %ebp, %eip – Stored in the kernel stack Heap space Stack context Data Text (instructions) 17

  18. Storing procs in xv6 • In a globally defined array present in ptable • NPROC is the maximum number of processes that can be present in the system (#define NPROC 64) • Also present in ptable is a lock that seralizes access to the array. ref : proc.c (struct ptable) 2409, params.h (NPROC) 0150 18

  19. Creating a Process by Cloning • Cloning – Child process is an exact replica of the parent – Fork system call Parent Child Process 1 Process 1 Process 2 system call fork => Kernel Kernel (execute fork) (execute fork) 19

  20. Creating a Process by Cloning (using fork system call) • In parent int pid; – fork returns child pid pid = fork(); if (pid > 0){ • In child process printf(“Parent : child PID = %d”, pid); pid = wait(); – fork returns 0 printf(“Parent : child %d exited\n”, pid); } else{ printf(“In child process”); • Other system exit(0); } calls – Wait, returns pid of an exiting child 20

  21. Virtual Addressing Advantage (easy to make copies of a process) • Making a copy of a process is called forking. Parent – Parent (is the original) Page Table – child (is the new process) • When fork is invoked, – child is an exact copy of parent Child • When fork is called all pages Page are shared between parent Table and child Physical Memory • Easily done by copying the parent’s page tables 21

  22. Modifying Data in Parent or Child int i=0, pid; pid = fork(); if (pid > 0){ sleep(1); Output printf("parent : %d\n", i); wait(); parent : 0 } else{ i = i + 1; child : 1 printf("child : %d\n", i); } 22

  23. Copy on Write (COW) • When data in any of the shared pages change, OS intercepts and makes a copy of the page. Parent • Thus, parent and child will have different Page copies of this page Table • Why? – A large portion of executables are not used. – Copying each page from parent and child would incur significant disk swapping.. i of child here huge performance penalties. Child Page – Postpone coping of pages as much as i of parent here Table possible thus optimizing performance This page now is no longer shared 23

  24. How COW works • When forking, – Kernel makes COW pages as read only – Any write to the pages would cause a page fault – The kernel detects that it is a COW page and duplicates the page 24

  25. Executing a Program (exec system call) • exec system call int pid; – Load into memory and then execute pid = fork(); if (pid > 0){ • COW big advantage for pid = wait(); } else{ exec execlp("ls", "", NULL); exit(0); – Time not wasted in copying pages. – Common code (for example shared libraries) would continue to be shared 25

  26. Virtual Addressing Advantages (Shared libraries) • Many common functions such as printf implemented in shared libraries • Pages from shared libraries, shared between processes Process A Process A Process B Process B Process B Process A Page Page Table Table printf(){ …} printf(){ …} printf(){ …} Virtual Memory Virtual Memory Physical Memory 26

  27. The first process • Unix : /sbin/init (xv6 initcode.S) – Unlike the others, this is created by the kernel during boot – Super parent. • Responsible for forking all other processes • Typically starts several scripts present in /etc/init.d in Linux 27

  28. Process tree Processes in the init.d system arranged in the form of a tree. NetworkManager lightdm dhclient dnsmasq init pstree in Linux gnome-session Who creates the first process? compiz 28

  29. Process Termination • Voluntary : exit(status) – OS passes exit status to parent via wait(&status) – OS frees process resources • Involuntary : kill(pid, signal) – Signal can be sent by another process or by OS – pid is for the process to be killed – signal a signal that the process needs to be killed • Examples : SIGTERM, SIGQUIT (ctrl+\), SIGINT (ctrl+c), SIGHUP 29

  30. Zombies • When a process terminates it becomes a zombie (or defunct process) – PCB in OS still exists even though program no longer executing – Why? So that the parent process can read the child’s exit status (through wait system call) • When parent reads status, – zombie entries removed from OS… process reaped! • Suppose parent does’nt read status – Zombie will continue to exist infinitely … a resource leak – These are typically found by a reaper process 30

  31. Orphans • When a parent process terminates before its child • Adopted by first process (/sbin/init) => 31

Recommend


More recommend