POSIX Threads (Pthreads) Standard – Pthreads-Win32, POSIX Thread for Windows – Scheduler Activations (I.E
Total Page:16
File Type:pdf, Size:1020Kb
Distributed Systems LEEC (2005/06 – 2º Sem.) Processes João Paulo Carvalho Universidade Técnica de Lisboa / Instituto Superior Técnico Outline • Processes and Threads –Overview – Processes, threads and fibers – Light weight processes • Client and Server Processes • Code migration • Software Agents • Case Studies J.P. Carvalho Sistemas Distribuídos 2005/2006 2 Processes Review • Multiprogramming versus multiprocessing • Kernel data structure: process control block (PCB) • Each process has an address space – Contains code, global and local variables.. • Process state transitions • Uniprocessor scheduling algorithms – Round-robin, shortest job first, FIFO, lottery scheduling, EDF • Performance metrics: throughput, CPU utilization, turnaround time, response time, fairness J.P. Carvalho Sistemas Distribuídos 2005/2006 3 Processes (2) Review - Scheduling • Priority queues: multiples queues, each with a different priority – Use strict priority scheduling – Example: page swapper, kernel tasks, real-time tasks, user tasks • Multi-level feedback queue – Multiple queues with priority – Processes dynamically move from one queue to another • Depending on priority/CPU characteristics – Gives higher priority to I/O bound or interactive tasks – Lower priority to CPU bound tasks – Round robin at each level J.P. Carvalho Sistemas Distribuídos 2005/2006 4 Processes and Threads • Processes – Typically independent – Carry considerable state information – Have separate address spaces – Interact only through system-provided inter-process communication mechanisms • Threads – Share the state information of a single process – Share memory and other resources directly • Context switching between threads in the same process is typically faster than context switching between processes. – Systems like Windows XP and OS/2 are said to have "cheap" threads and "expensive" processes, while in other operating systems there is not so big a difference. J.P. Carvalho Sistemas Distribuídos 2005/2006 5 Processes and Threads(2) • Multi-threaded program advantage: – Operates faster on computer systems that have multiple CPUs, CPUs with multiple cores, or across a cluster of machines • Why? – Because the threads of the program naturally lend themselves for truly concurrent execution • The programmer needs to be careful to avoid race conditions, and other non-intuitive behaviors • In order for data to be correctly manipulated, threads will often need to rendezvous in time in order to process the data in the correct order • Threads may also require atomic operations (often implemented using semaphores) in order to prevent common data from being simultaneously modified, or read while in the process of being modified. Careless use of such primitives can lead to deadlocks J.P. Carvalho Sistemas Distribuídos 2005/2006 6 Processes and Threads (3) • Thread Scheduling: – Preemptive multithreading • Allows the operating system to determine when a context switch should occur – Disadvantage: the system may make a context switch at an inappropriate time – Cooperative multithreading • The threads themselves relinquish control once they are at a stopping point – Disadvantage: can create problems if a thread is waiting for a resource to become available). J.P. Carvalho Sistemas Distribuídos 2005/2006 7 Why use Threads? • Large multiprocessors need many computing entities (one per CPU) • Threads can execute in parallel on multiprocessors • Switching between processes incurs high overhead. By using threads, an application can avoid per-process overheads • Thread creation and deletion are also cheaper than processes • Threads have full access to a process address space (easy sharing) • Threads retain the idea of sequential processes with blocking system calls, and yet achieve parallelism • Software engineering perspective – Applications are easier to structure as a collection of thread where each thread performs several [mostly independent] tasks J.P. Carvalho Sistemas Distribuídos 2005/2006 8 Thread Management • Creation and deletion of threads – Static versus dynamic • Critical sections – Synchronization primitives: blocking, spin-lock (busy-wait) – Condition variables • Global thread variables • Kernel versus user-level threads J.P. Carvalho Sistemas Distribuídos 2005/2006 9 User-level Threads User-Level Threads, aka Fibers • Threads managed by a threads library – Kernel is unaware of presence of threads • Advantages: – No kernel modifications needed to support threads – Efficient: creation/deletion/switches don’t need system calls – Flexibility in scheduling: library can use different scheduling algorithms, can be application dependent • Disadvantages – Need to avoid blocking system calls (a thread blocks the entire process) – Threads compete for resources within a process – Do not take advantage of multiprocessors (no real parallelism) J.P. Carvalho Sistemas Distribuídos 2005/2006 10 User-level Threads (2) User-level Thread Example J.P. Carvalho Sistemas Distribuídos 2005/2006 11 Kernel-level Threads Kernel Level Threads • Kernel aware of the presence of threads – Blocked threads do not block other threads in the same process – Better but more expensive scheduling decisions – Better for multiprocessors, more overheads for uniprocessors J.P. Carvalho Sistemas Distribuídos 2005/2006 12 Light-Weight Processes LWP •Goal:Implement “fibers” that do not block the associated process – NOTE: Many systems (e.g. Java), do not make a distinction between Kernel Threads and LWP (Kernel Threads are implemented as LWP) • Several LWPs per process • Multithreaded applications – create multiple threads, assign threads to LWPs (one-one, many-one, many-many) • Each LWP, when scheduled, searches for a runnable thread on a shared thread table (two-level scheduling) – Shared thread table: no kernel support needed • If a LWP thread blocks on a system call, switch to kernel mode and, if possible, switch OS context to another LWP of the same process J.P. Carvalho Sistemas Distribuídos 2005/2006 13 Light-Weight Processes(2) LWP Example J.P. Carvalho Sistemas Distribuídos 2005/2006 14 P T F Example (extracted from Wikipedia) N N N A program running on DOS. The program can only do one thing at a time. Windows 3.1 running on top of DOS. All Windows programs are run within a single process, so the programs can corrupt each N N Y other's memory space. A poorly written program could corrupt data it didn't own, causing the infamous General Protection Fault, or neglect to yield the processor, resulting in a hung system. Amiga OS's original implementation. The operating system had full thread support, allowing multiple applications to run independently of each other which are scheduled by the kernel. The lack of process support resulted in a more efficient system N Y N (by avoiding the overhead of memory protection), with the price that application bugs could easily crash the entire computer. Many commercial RTOSes, such as vxWorks, also support this model. Mac OS 9 supported fibers through Apple's Thread Manager and threads through Apple's Multiprocessing Services, which N Y Y works with the nanokernel intoduced in Mac OS 8.6 to provide thread support, but still use the MultiFinder's way of managing applications. Most early implementations of Unix. The operating system could run more than one program at a time, and programs were protected from each other. If a program behaved badly, it could crash its process ending that instance of the program without Y N N disrupting the operating system or other programs. However, due to this protection, sharing information between programs was error-prone (using techniques like shared memory) and expensive (using techniques like message passing). Performing any tasks asynchronously required an expensive fork() system call. Sun OS before Solaris. Sun OS is Sun Microsystem's version of Unix. Sun OS implemented "green threads" in order to allow a single process to asynchronously perform multiple tasks such as playing a sound, repainting a window, and responding to user Y N Y events such as clicking the stop button. Although processes were pre-emptively scheduled, the "green threads" or fibers were co-operatively multitasked. Often this model was used before real threads were implemented. This model is still used in microcontrollers and embedded devices. This is the most common case for applications running on Windows NT 3.51 SP3+, Windows 2000, Windows XP, Mac OS X, Linux, and other modern operating systems. Although each of these operating systems allows the programmer to implement fibers or use a fiber library, most programmers do not use fibers in their applications. The programs are multithreaded and run Y Y N inside a multitasking operating system, but perform no user-level context switching. On the typical home computer, most running processes have two or more threads. A few processes will have a single thread. Usually these processes are services running without user interaction. Typically there are no processes using fibers. Almost all operating systems after 1995 fall into this category. The use of threads to perform concurrent operations is the most Y Y Y commonJ.P. choice, Carvalho although there are also multi-process Sistemas andDistribuídos multi-fiber applications. 2005/2006 Threads are used, for example, to enable 15 a program to render its graphical user interface while waiting for input from the user or performing a task like spell checking. Threads Implementations • There are many different and incompatible implementations of threading, including both kernel-level