Process and Memory Management The lifetime of a process is between a fork and an exit (process state-transition diagram, states created 8 to zombie 9), and is largely spent sleeping, running or waiting to be scheduled. The kernel's process table holds a list of all processes. ps shows most of the information about a P. Some info about a P is accessible over its lifetime and some accessible to the kernel only when the P runs Process table entry fields: state -running, sleeping,.. size and location of u area & pregion entry for swapping & switching UIDs user identifiers permitting sending signals between processes PIDs unique process identifiers event descriptor - address of event to wakeup a sleeping process scheduling parameters determine access to CPU - system priority : user priority (PRI:NI) array of signals not yet handled while swapped out execution and kernel utilisation times Context of a Unix process 1. user-level 2. register 3. system-level 1. User-level context 1. process text (instructions) 2. data 3. user stack 4. any shared memory u area (user area) fields: pointer to process table entry UIDs to permit access rights time spent executing in user and kernel modes an array containing signal handlers login terminal associated with the process error from a system call return value from a system call I/O parameters: amount of data to transfer, the address of the source (or target) data array in user space, file offsets for I/O, etc. current and root directory user file descriptor table records the files the process has open size limits of a process and of a file it can write permission of files the process creates Dynamic Portion of Context : Static Portion of Context User Level Context Text Data Stack Shared Data logical pointer to current Kernel Stack for Layer 3 context Layer 3 Saved Register Context layer for Layer 2 Kernel Stack for Layer 2 Layer 2 Saved Register Context for Layer 1 Kernel Stack for Layer 1 Layer 1 Saved Register Context for Layer 0 Kernel Context (User stack) Layer 0 2. Register context Static Part of 1. program counter - next instruction to run System Level Context 2. processor status register 3. pointer to user or kernel stacks Process Table Entry 4. general registers U Area PerProcessRegion Table 3. System-level context: static part - for the P's lifetime 1. P table entry Components of the Context of a Process 2. u area 3. Per Process region (Pregion) entries, region tables, page tables dynamic part - changes over P's lifetime 1. kernel stack of current layer 2. restorable registers of previous system context layers 106761818 1 2 1 3 2 1 push push push 2 1 1 pop pop pop 1 A P mainly runs in user mode, so mainly operates in the user stack. When there is a system call or the P is involved in an interrupt, then the P runs in kernel mode and these instructions are protected by operating in the kernel stack. The figure shows the dynamic portion of the context: user and kernel stacks Kernel pushes a context layer, on a 1. interrupt 2. system call 3. context switch kernel pops a context layer when 1. kernel returns from handling interrupt 2. P returns to user mode after system call 3. context switch Interrupt Sequence Disk Interrupt.............. Context stack of a P can have 7 layers - a push for: each of the 5 prioritised interrupts, + a system call, + the user-level: Kernel Context Layer 3 Execute Disk Interrupt Handler Save Register Context of Software Interrupt Handler Kernel Context Layer 2 Execute Software Interrupt Handler Save Register Context of Sys Call Software Interrupt......... clock interrupt disk interrupt tty interrupt device interrupt software interrupt system call user-level 1. Interrupts are from hardware, software and users 1. push current P context on stack 2. handle interrupt 3. pop (restore previous) context of same P Kernel Context Layer 1 Execute Sys Call Save Register Context for user layer Make System Call.......... User Context Layer 0 Executing User Mode Example of Interrupts 2. System calls are library functions, which issue an interrupt to change execution from user mode to kernel mode 1. push current P context on stack 2. handle system call 3. pop (restore previous) context of same P 3. Context switching Kernel allows a P to switch context, on: 1. exit 2. sleep 3. return to user mode after a system call, but another P should run (priority, fairness) 4. return to user mode after an interrupt, ditto (User Level) Kernel Context Layer 2 Execute Code for Context Switch Save Register Context of Sys Call Invoke Sleep Algorithm..... Kernel Context Layer 1 Execute Sys Call Save Register Context User Level Make System Call ......... User Context Layer 0 Kernel checks integrity of all data structures and locks before a switch (User Level) Executing User Mode 1. push current P context on stack 2. determine which P to schedule 3. pop (restore previous) context of the scheduled P Typical Context Layers of a Sleeping Process 106761818 2 User process Interrupt/trap I1 I2 I3 I4 I5 I6 I7 I8 I9 I0 I1 I1 I2 I3 I4 Wake & resume I5 I6 I7 I8 I9 end Process2 I1 I2 I3 I4 suspend resume I5 I6 suspend & sleep interrupt I1 I2 I3 Wake & resume I7 I8 I9 I10 end Interrupt/trap I1 I2 I3 I4 I1 I2 I3 User process interrupted. Interrupt routines interrupted User process1 I1 I2 I3 I4 I5 I6 I7 I8 I9 Processes/interrupts running. Context switch. Interrupt. 106761818 I1 I2 I3 I4 I5 I6 I7 I8 I9 I0 I1 I1 I2 I3 I4 I5 User process interrupted by 2 routines in sequence Process1 I1 I2 I3 I4 suspend & sleep User process User process2 I1 I2 I3 I4 I5 I6 I7 I8 I9 I10 Interrupt/trap I1 I2 I3 2 concurrent user processes timeslicing process2 is interrupted Process1 Key input Process2 Disk input Process3 Wait child death Process4 Processes wait for events 2 processes can be woken by the same event 3 u areas Process table A B Pregion table Region table Process memory text 20K data 32K stack 44K text 20K data 28K stack 54K Process data structures The figure shows many items mentioned above. 2 Ps share text, ie they run the same code, eg after a fork The global kernel process table has entries for each P; each P has its u area; each P has a Pregion (a P's text, data & stack regions); Pregions are mapped to the global region table; which is mapped to virtual and on to physical memory. The entries of the region table have the following fields: size location of physical memory type: text, private data, shared memory or stack status: locked, used, loading into memory and/or valid pointer to the inode (the file) which originally uses the region count of processes which use the region The malloc and free C functions and the new and delete C++ operators use the brk() system call to change the data region size. Shared memory system calls (shmget, shmat, shmdt, shmctl) enable communication by attaching to the same region of memory in the global region table. Attached memory is accessed via pointers, so communication speed is very high, and overheads low. fork() creates an identical duplicate process sproc() creates a lightweight process, which is nearly identical to its parent. The new child process shares the virtual address space of the parent process, rather than simply being a duplicate. The parent and the child each have their own program counter value and stack pointer, but all the text and data regions are visible to both processes. Such lightweight process have similar overheads to full processes. exec() overlays a new process image on an old process. The new process image is constructed from an ordinary, executable file, so is often used to replace the child's text in a fork(). There can be no return from a successful exec() because the calling process image is overlaid by the new process image. pthreads are POSIX threads API that enable portable parallel processing, see man pthread_create 3 differences between threads and processes: 1. A P has its own set of state information, eg its own effective user ID and set of open file descriptors Threads exist within a process and do not have distinct copies of these state values. Threads share the single state belonging to their process 2. Normally, each P has a unique address space of memory segments which are accessible only to that process. (Ps created with sproc() share address space). Threads within a process always share the single address space belonging to their process 106761818 4 3. Ps are scheduled by the kernel. A change of process requires 2 context switches Threads are scheduled by code that operates largely in the user address space, without kernel assistance. Thread scheduling is faster than process scheduling. Attribute POSIX Threads Source portability Standard interface, portable between fork() is a UNIX standard vendors ISO/IEC 9945-1:1996 Creation overhead Relatively small Block/Unblock Overhead (Dispatch) Few microseconds Address space Shared Memory-mapped arenas files and Shared UNIX Processes Quite large Many microseconds Separate Explicit sharing only Mutual exclusion objects Mutexes and condition variables; semaphores and locks; POSIX POSIX semaphores; message queues semaphores; message queues Files, pipes, and I/O streams Shared single-process file table Signal masks and handlers Separate file table signal Each thread has a mask but handlers Each process has a mask and its own are shared handlers Resource limits Single-process limits Limits apply separately Process ID One PID applies to all threads PID per process One process One thread process control block user stack user addrs space kernel stack each process One process Many threads Threads A unix process can be said to have 1 thread of execution POSIX threads pthreads are run within a process to process control block thread contrl block user stack thread contrl block user stack thread contrl block user stack user addrs space kernel stack kernel stack kernel stack Multi-threads share the context, but have their own stack ------------------------------------------------------------------------------------------------------------------------------Tutorial Exercises 1. Explain what happens to the stacks of a process when the following happen: the process issues a system call a time slice while a process issues a system call, it is interrupted by a software, a device, a tty, a disk and a clock interrupt 2. Explain the major differences between a Unix process and a POSIX thread 106761818 5