Threads, SMP and Microkernels

Slides:



Advertisements
Similar presentations
Threads, SMP, and Microkernels
Advertisements

Operating Systems: Internals and Design Principles
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Chapter 4 Threads, SMP, and Microkernels Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design.
Computer Systems/Operating Systems - Class 8
Day 10 Threads. Threads and Processes  Process is seen as two entities Unit of resource allocation (process or task) Unit of dispatch or scheduling (thread.
1 Threads, SMP, and Microkernels Chapter 4. 2 Process: Some Info. Motivation for threads! Two fundamental aspects of a “process”: Resource ownership Scheduling.
B.Ramamurthy1 POSIX Thread Programming 2/14/97 B.Ramamurthy.
Threads, SMP, and Microkernels
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
Chapter 4 Threads, SMP, and Microkernels Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E.
Operating Systems Lecture 09: Threads (Chapter 4)
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Threads, SMP, and Microkernels Chapter 4. 2 Outline n Threads n Symmetric Multiprocessing (SMP) n Microkernel n Linux Threads.
Operating System 4 THREADS, SMP AND MICROKERNELS
Chapter 4 Threads, SMP, and Microkernels Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design.
1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
Processes and Threads Processes have two characteristics: – Resource ownership - process includes a virtual address space to hold the process image – Scheduling/execution.
B. RAMAMURTHY 10/24/ Realizing Concurrency using the thread model.
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Threads G.Anuradha (Reference : William Stallings)
1 Threads, SMP, and Microkernels Chapter 4. 2 Process Resource ownership: process includes a virtual address space to hold the process image (fig 3.16)
1 Threads, SMP, and Microkernels Chapter Multithreading Operating system supports multiple threads of execution within a single process MS-DOS.
1 Lecture 4: Threads Advanced Operating System Fall 2010.
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
Operating Systems: Internals and Design Principles
Chapter 4 Threads, SMP, and Microkernels Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E.
1 Threads, SMP, and Microkernels Chapter 4. 2 Process Resource ownership - process includes a virtual address space to hold the process image Scheduling/execution-
Threads, SMP and Microkernels Process vs. thread: –Unit of resource ownership (process has virtual address space, memory, I/O channels, files) –Unit of.
Threads, SMP, and Microkernels Chapter 4. Processes and Threads Operating systems use processes for two purposes - Resource allocation and resource ownership.
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Chapter 4 – Thread Concepts
Realizing Concurrency using the thread model
Chapter 4: Threads Modified by Dr. Neerja Mhaskar for CS 3SH3.
Chapter 3: Process Concept
CS 6560: Operating Systems Design
Realizing Concurrency using the thread model
Day 12 Threads.
Chapter 4 – Thread Concepts
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Process Management Presented By Aditya Gupta Assistant Professor
Nachos Threads and Concurrency
Operating Systems Processes and Threads.
Realizing Concurrency using Posix Threads (pthreads)
Chapter 4: Threads.
Threads, SMP, and Microkernels
Realizing Concurrency using the thread model
Realizing Concurrency using the thread model
Threads Chapter 4.
Lecture 4- Threads, SMP, and Microkernels
Operating System 4 THREADS, SMP AND MICROKERNELS
Operating Systems : Overview
Threads and Concurrency
Threads Chapter 4.
Jonathan Walpole Computer Science Portland State University
Operating Systems : Overview
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems : Overview
Chapter 4 Threads, SMP, and Microkernels
Realizing Concurrency using the thread model
Threads and Concurrency
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems : Overview
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Presentation transcript:

Threads, SMP and Microkernels B.Ramamurthy Chapter 4 11/18/2018 B.Ramamurthy

Thread Unit of work A process has address space, registers, PC and stack (See man fork for the detailed list) A thread has registers, program counter and stack, but the address space is shared with process that started it. This means that a user level thread could be invoked without assistance from the OS. This low overhead is one of the main advantages of threads. If a thread of a process is blocked, the process could go on. Concurrency: Many threads could be operating concurrently, on a multi threaded kernel. User level scheduling is simplified and realistic (bound, unbound, set concurrency, priorities etc.) Communication among the threads is easy and can be carried out without OS intervention. 11/18/2018 B.Ramamurthy

Thread requirements An execution state Independent PC working within the same process. An execution stack. Per-thread static storage for local variables. Access to memory and resources of the creator-process shared with all other threads in the task. Key benefits: less time to create than creating a new process, less time to switch, less time to terminate, more intuitive for implementing concurrency if the application is a collection of execution units. 11/18/2018 B.Ramamurthy

Examples of thread usage Foreground and background work: Consider spreadsheet program: one thread could display menu and get response while another could be processing the request. Increases the perceived speed of the application. Asynchronous processing: Periodic backup (auto-saving) of RAM into disk. A thread could schedule itself to come-alive every 1 minute or so to do this saving concurrently with main processing. Speed execution: In hard-core data-processing simple concurrency can speed up process. Transaction processing: Many independent transactions can be modeled very nicely using threads. Such applications as neural networks, searches, graphics, agent/actor model suit well for thread-implementation. 11/18/2018 B.Ramamurthy

Multithreading Multithreading refers to the ability of the operating system to support multiple threads of execution within a single process. A process is defined as the unit of protection and the unit of resource allocation. It has : a virtual address space, protected access to processors, IPC, files, IO resources. 11/18/2018 B.Ramamurthy

Threads within Processes A process may have one or more threads with: 1. A thread execution state 2. A thread context when not running (independent PC) 3. An execution stack 4. Static storage for local variables 5. Shared access to the memory and resources of the process in which it resides. 11/18/2018 B.Ramamurthy

Thread Operations Basic Operations associated with a thread are: Spawn : newly created into the ready state Block : waiting for an event Unblock : moved into ready from blocked Finish : exit after normal or abnormal termination. 11/18/2018 B.Ramamurthy

Issues When a thread blocks should the process associated with it blocked? Do we need different types of threads for process context and kernel context? Should a thread be bound to a processor? If so, when? How about daemon threads ? Just like zombie processes? Thread scheduling : user level control? How many concurrent threads? How about thread synchronization? 11/18/2018 B.Ramamurthy

User-level Threads User Level Threads (ULTs) concept is supported by a thread library. All the thread-related operations are supported by the library: creating, destroying, passing messages, scheduling, saving and restoring contexts. An OS may support only ULT… Example: Encore multimax system (sybil) : NO Kernel level threads 11/18/2018 B.Ramamurthy

ULT : Advantages and Disadvantages No kernel interventions.. Overhead low Application level scheduling No changes needed in kernel to run ULT. Disadvantages: If a kernel process blocks, then entire process in which the thread is blocked. Multithreading cannot make use of kernel level multiprocessing. 11/18/2018 B.Ramamurthy

Kernel-level Threads (KLT) Lets consider a kernel that supports threads. Any thread in a process will be mapped on to a kernel level thread. Pure kernel level approach is used by OS/2 and Windows NT. It works quite nicely except for the fact that switching between two threads within a process requires kernel more switch! 11/18/2018 B.Ramamurthy

Combined Approach : ULT + KLT Approach used in Solaris. Thread creation, synchronization and scheduling done at user level. Several ULTs mapped to few KLTs. User may adjust number of KLTs (get and set concurrency). Reading (not in the book) : Posix thread standard. 11/18/2018 B.Ramamurthy

Solaris OS Supports : process, user level thread, LWP and kernel level thread Symmetric Multi Processing Process System call goes thru the same sequence as any other system 11/18/2018 B.Ramamurthy

System Call 1. Process traps to kernel. 2. Trap handler runs in kernel mode and saves all regs. 3. Handler sets stack pointer to process’s kernel stack. 4. Kernel runs system call. 5. Kernel places any requested data into user space’s structure. 6. Kernel changes any process structure values affected. 7. Process returns to user mode, replaces reg and stack and returning value from system call. 11/18/2018 B.Ramamurthy

Solaris Support for Multithreading Process user LWP Kernel threads kernel bound processors hardware 11/18/2018 B.Ramamurthy

Thread Support in Solaris Programmers write applications using thread library. User threads are scheduled into LWPs. LWPs are in turn implemented using kernel threads. More than one user thread may map onto an LWP. Kernel threads in turn are scheduled on available CPU. LWP syscalls are handled exactly as specified above. 11/18/2018 B.Ramamurthy

Unix and Solaris Process Structure PID, UID Signal dispatch table Memory Map File descriptors Process state PID, UID Signal dispatch table Memory Map File descriptors LWP2 LWP1 * * * 11/18/2018 B.Ramamurthy

Process State and LWP state Process state and LWP state in the above figure contain: LWP id (if applicable) Priority Signal mask registers Stack other process state details. 11/18/2018 B.Ramamurthy

Blocking Many LWPs can be scheduled independently. There is a kernel stack for each LWP. Each thread can issue a system call, but blocking of this thread will not block the process. For example, 10 threads of a process can be blocked on read, but 10 other sin the process can be computing. 11/18/2018 B.Ramamurthy

Emergence of thread standard 1991 .. No major commercial OS had contained robust user-level threads library. 1997.. Every major player in the computer industry has one (thread library). OS themselves are multithreaded! Posix standard emerged… as expected followed very closely Solaris (leading player) multithreading… Pthreads emerged. 11/18/2018 B.Ramamurthy

Thread control - posix style creation: pthread_create (&tid, &attr, start_fn, arg); exit : pthread_exit(&status); join : pthread_join(thr_name, &status); cancel : pthread_cancel(thr_name); We will look into synchronization mechanisms when studying process synchronization. 11/18/2018 B.Ramamurthy

Creating threads Always include pthread library: #include <pthread.h> int pthread_create (pthread_t *threadp, const pthread_attr_t * attr, void *(* start routine)(void *), void *arg); This creates a new thread of control that calls the function start_routine. It return a zero if the creation is successful, and thread id in threadp (first parameter). attr is to modify the attributes of the new thread. If it is NULL default attributes are used. The arg is passing arguments to the thread function. 11/18/2018 B.Ramamurthy

Using threads 1. Declare a variable of type pthread_t 2. Define a function to be executed by the thread. 3. Create the thread using pthread_create Make sure creation is successful by checking the return value. 4. Pass any arguments need through’ arg (packing and unpacking arg list necessary.) 5. #include <pthread.h> at the top of your header file. 6. Compile: cc xyz.c -lpthread -lthread -lposix4 11/18/2018 B.Ramamurthy

Thread’s local data Variables declared within a thread (function) are called local data. Local (static) data associated with a thread are allocated on the stack. So these may be deallocated when a thread returns. So don’t plan on using locally declared variables for returning arguments. Plan to pass the arguments thru argument list passed from the caller or initiator of the thread. 11/18/2018 B.Ramamurthy

Thread Join and Cancellation pthread_join(ThreadId, &Status) makes a thread wait for the thread specified in the ThreadId. Pthread_cancel(ThreadId); cancels the thread specified by ThreadId. Any thread can cancel any other thread. There is no need for any relationship for cancellation. 11/18/2018 B.Ramamurthy

Thread termination Implicit : Simply returning from the function executed by the thread terminates the thread. In this case thread’s completion status is set to the return value. Explicit : Use thread_exit. Prototype: void thread_exit(void *status); The pointer value in the variable “status” is available to the threads waiting for this thread. 11/18/2018 B.Ramamurthy

Example Lets look at multi_thr.c example enclosed. Best way to handle the operational complexity of this sample program is thru’ a timing diagram. 11/18/2018 B.Ramamurthy

Symmetric Multiprocessing (SMP) Greatest push towards multithreading came with the emergence of SMP. Multithreading provides exactly the right paradigm to make maximal use of these new concept (SMP). When we have multiple threads at user level, multiprocessor at the hardware level can exploit the user-level concurrency. 11/18/2018 B.Ramamurthy

SMP (contd.) Multiple processors. Tightly coupled… shared memory. Kernel can execute on any processor. Each processor self-schedules from a pool of processes and threads. Parts of kernel can execute concurrently on different processors! OS design is complex, but performance improvement is great. 11/18/2018 B.Ramamurthy

Key Design Issues Simultaneous concurrent processes or threads : kernel routines should be reentrant. Interconnection structures Memory management : multi-port memory, cache coherence Syncronization : Mutual exclusion. 11/18/2018 B.Ramamurthy

Microkernels A microkernel is a small operating system core that provides the foundation for modular extensions. Typically a microkernel is surrounded by a number of subsystems to support extended functionality of an operating system. 11/18/2018 B.Ramamurthy

Layered vs Microkernel mode user users Memory mgt. File System Client process Device driver File server Process server User mode IO and Device subsys. kernel Virtual memory Primitive process mgt. Kernel mode Microkernel Hardware Hardware 11/18/2018 B.Ramamurthy

Microkernel Philosophy Absolutely essential core operating system functions should be in kernel. Less essential services and applications are built on the microkernel and execute in user mode. OS components outside the microkernel are implemented as server processes. These components interact by message passing thru the kernel. 11/18/2018 B.Ramamurthy

Benefits of Microkernel Uniform Interface: processes need not distinguish between kernel-level and user-level services because all services are provided by means of message passing. Extensibility: Adding a new service to the OS does not involved rewriting the kernel. Just add a (vertical pillar) server on the microkernel. 11/18/2018 B.Ramamurthy

Benefits of Microkernel(contd.) Flexibility: Not only adding features easy but also removing features to provide an efficient, optimized OS. Portability : Only the microkernel is processor dependent. Changes to port to newer processor are fewer. Reliability: Small microkernel can be rigorously tested. 11/18/2018 B.Ramamurthy

Microkernel… Distributed system support: Lends itself to distributed system control. Components (server) need not be in a single central location. They can be distributed. Object-oriented system: Decomposition of the traditional kernel into microkernel and servers yields very nicely to OO design. 11/18/2018 B.Ramamurthy

Multi-threaded and SMP Windows NT supports threads within processes. SMP : Symmetric Multi - Processing allows for any process or thread can be assigned to any processor by the kernel. Design for exploiting SMP: OS routines can run on available processors. Multiple threads of the same process can execute on different processors. Server process may use multiple-threads to take request from multiple users at the same time. Provides ease of sharing data and resources, and flexible IPC. 11/18/2018 B.Ramamurthy

Processes and threads NT has two types of process-related objects: processes and threads. A process corresponds to a user job or application that owns resources, such as memory, and files. A thread is dispatchable unit of work that executes sequentially and is interruptible. NT kernel does not maintain any relationship among the processes that it creates, including parent-child relationship. NT process must contain at least one thread to execute. This thread may then create other threads. 11/18/2018 B.Ramamurthy

process class An object is an instantiation of a class. A simple class definition contains: attributes (data structures) and methods (operations, services /functions). These attributes could be private, public (and/or protected). Description of NT process in Fig.4.11. See an excellent description of classes for process and thread in Fig.4.12. 11/18/2018 B.Ramamurthy

Support for NT Subsystems It is the responsibility of the OS subsystem to exploit the NT process and thread features to emulate facilities of its OS. (Obviously each OS has its own subsystem.) Application requests process creation ==> protected subsystem ==> process request to NT executive ==> NT ex. instantiates an object process and returns handle of the object to the subsystem. 11/18/2018 B.Ramamurthy

Support for NT Subsystems (contd.) But win32 and OS/2 processes are always created with a thread... so the subsystem issues one more request to the NT executive to instantiate and return a handle for a thread. After this the process and thread handle are returned to the application program. NT allows subsytem to specify the parent of the new process for inheriting its attributes. NT has no predetermined relationship among procs. Both OS/2, win32 and unix have parent -child relationship. This is solved by using “handles”. 11/18/2018 B.Ramamurthy

Summary Multithreading is an important concept at all levels: application, library and operating system level. Microkernel is a worthwhile concept to keep in mind when designing operating systems especially the highly specialized ones. SMP has been successfully used in many current OS : Solaris and Windows NT. 11/18/2018 B.Ramamurthy