Lecture 2 Processes and Threads 1 In this lecture A closer look at processes and threads and the possible relationships between them 2 Visualising multiprogramming 3 Process related goals of the OS 1. 2. 3. 4. 5. Switch processes: maximise processor utilisation Reasonable response times Allocate resources to processes. Handle inter-process communication. Synchronise processes 4 Creation of processes Some reasons for process creation: 1. System initialisation (boot time) 2. A process creates another process 3. A user request to create a process 5 Process termination Some reasons for a process to end: 1. 2. 3. 4. Normal exit (voluntary) Error exit (voluntary) Fatal error (involuntary) Killed by another process (involuntary) 6 Process states Running Transition legend: 1. A running process blocks due to I/O or waits for an event 2. Scheduling algorithm preemption 3. Scheduling algorithm gives focus 4. I/O available or event has arrived 1 2 3 Blocked 4 7 Ready Process states: under the hood Dispatch New Admit Ready Release Running Time-out Event Wait Event Occurs Blocked 8 Exit Process states: under the hood Ready Queue Release Dispatch Admit Processor Time-out Event Wait Event Occurs Blocked Queue 9 Process states: under the hood Ready Queue Release Dispatch Admit Processor Time-out Event 1 Wait Event 1 Occurs Event 1 Queue Event 2 Wait Event 2 Occurs Event 2 Queue 10 Optimisation suggestion Swap processes blocked on I/O to the disk How to alter our design? 1. Blocked state: in memory blocked process 2. Blocked-Suspended: blocked process out of memory 3. Ready-Suspended: ready process out of memory 11 Optimisation suggestion New Admit Admit Suspend Dispatch Activate Ready, suspend Ready Running Suspend Time out Event Occurs Event Occurs Activate Blocked, suspend Blocked 12 Event Wait Exit Main process related tasks in OS 1. Process creation and termination 2. Process scheduling and dispatching 3. Process switching 4. Process synchronisation and support for inter-process communication 13 The process table 14 The process context switch When switching between processes: 1. Save processor context: program counter, registers, etc. 2. Update the process table entry: new state etc. 3. Move process table entry to appropriate queue - ready, blocked 4. Select another process for execution 5. Update the table entry of the process selected 6. Update memory-management data structures 7. Restore context of selected process (relatively) expensive 15 Interrupt vector For each I/O class: memory location with address + queue of interrupts Interrupt vector = of interrupt handler procedure Scenario: (very roughly speaking) 1. 2. 3. 4. 5. 6. A process P is running Disk interrupt occurs P’s information is pushed onto a queue (table entry updated) CPU jumps to address in interrupt vector (done by hardware) Interrupt handler executes Process scheduler gets to decide who can run first 16 Signals to processes Signal = Software interrupt Signals are generated from: 1. Shell: kill <PID> 2. System calls: kill(PID) (Usually) a process can send signals to its group members. 17 Signals to processes Signal = Software interrupt Upon receiving a signal a process can: 1. Ignore it 2. Let the system handle it 3. “Catch it” with a handler As you would with an exception 18 Terminology involving signals 1. A signal is generated for a process when the event that causes it occurs. 2. A signal is delivered to a process when the action for the signal is taken 3. Between generation and delivery, the signal is pending 4. A process has the option of blocking the signal (signals mask) 19 Process termination Process may terminate due to: 1. return at “main” 2. Calling “exit” with some code 3. Get a kill signal Orphan = processes whose parent has terminated What to do with orphans? 1. 2. 3. Make them parentless? Have some ancestor of their parent adopt them? Perhaps the “init” process? 20 Zombie processes Zombie process = a process in terminated state that still has an entry in the process table 1. Child processes that have “exited” yet their entry is kept to allow their parent to read their exit status 2. This exit code is read through the wait system call performed by parent. Zombie process = a processes whose parent did not wait for him 21 Threads Multiprogramming within the environment of a single process 0xFFFFFFFF Stack for thread 1 SP thread 1 Stack for thread 2 SP thread 2 address space heap (dynamic allocated mem) static data (data segment) 0x00000000 code (text segment) 22 PC thread 2 PC thread 1 The fork() Unix system call #include <unistd.h> pid_t fork(void); Creates a new process by duplicating the calling process. The new process - the child - is an exact duplicate of the calling process - the parent. Child does not inherit everything! 1. Child has unique PID 2. Child’s pending signal lists empty 3. Parent’s threads are not inherited 23 Thread vs Process Threads Processes shared data unique data shared code unique code shared open I/O unique open I/O shared signal table unique signal table unique stack unique stack unique PC unique PC unique registers unique registers unique state unique state light context switch heavy context switch Signal handlers must be shared among all threads of a multithreaded application; however, each thread must have its own mask of pending and blocked signals (POSIX 1003.1). 24 User-space threads 1. 2. 3. Thread management in user space Control threads via a user mode library Kernel not aware of any threads Pros: 1. No user-kernel mode switch overhead 2. Scheduling is app specific 3. Thread library may be cross platform There are solutions to “cons” Price: system complexity Cons: 1. One thread makes a blocking sys call, then the whole process is blocked and all threads with it 2. Multi-CPU advantage lost: Kernel may only assign one process per CPU instead of assigning threads to different CPUs 25 Kernel-space threads 1. 2. Thread management in kernel space User only gets an API Pros: 1. Threads can be assigned to multiple CPUs 2. One thread blocks need not block others 3. Kernel routines can be multithreaded Cons: 1. Switch between threads requires switch between user to kernel mode 26 Combined Usually: 1. Thread creation in user space 2. Thread scheduling and sync. is split between user and kernel mode Technique: User threads of a single process are mapped to a smaller (or equal) sized set of kernel threads. 27 User thread and Kernel thread relationships 1:1 M:1 M:N 1.Least complicated 2.Increased burden on kernel 1. Increased utilisation of kernel resources 2. Complexity: switch kernel thread between several user All of the above 28 Synchronous and Asynchronous thread creation Asynchronous creation: 1. Parent resumes execution immediately after creation 2. Child and parent independent and execute pseudo-parallel 3. Parent does not know when child is done (unless join) Synchronous creation: 1. Parent has to wait until all its children have terminated 2. Option to create several threads through one call 3. Parent must know when child has terminated 29 Posix vs Windows vs Java Posix (Many versions of Unix/Linux) Windows Java User and kernel threads Kernel threads User threads Global data shared amongst threads Global data shared amongst threads No notion of global data in Java. Shared thread data is explicitly set Asynchronous Asynchronous Asynchronous creation + join function creation + join function creation + join function 30 Thread-Process relationship 31 Interrupts as threads Problem: Interrupt handlers are synchronised due to interrupt disabling. But interrupt handlers usually do not share much data anyway So why disable all interrupts for a single interrupt? Required solution: A more refined synchronisation between interrupt handlers 32 Interrupts as threads: Solaris In Solaris: 1. A set of kernel threads handle interrupts 2. Use traditional thread sync. mechanisms to sync. interrupt handlers 3. Interrupt threads have higher priority than other threads 33 Thread states Similar to processes: ready blocked running Lets review some examples….. 34 Windows thread states 35 Linux Process/Thread states 36 Thread pools A fixed set of threads is created These handle all tasks of the process Thread 1 feed Thread 2 Thread 3 Tasks Pool manager 37 MAC OS X GCD GCD = Grand Central Dispatch Programmers do not control threads! 1. 2. 3. 4. Tasks queues API to append a task to a queue GCD handles the dispatch of a task to a kernel thread User app may go on while task is processed Pros: Cons??? (debate) 1. MAC OS knows better than programmer how many threads the ENTIRE system needs (GCD = thread pool for everything) 2. Programmers do not handle thread synchronisation: they only care about tasks 1. Rigid: Apple knows better than the programmer approach 2. Why should my tasks be in queue with other apps? 38