1 / 42

Threads, SMP and Microkernels

Threads, SMP and Microkernels. B.Ramamurthy Chapter 4. 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.

jens
Télécharger la présentation

Threads, SMP and Microkernels

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Threads, SMP and Microkernels B.Ramamurthy Chapter 4 B.Ramamurthy

  2. 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. B.Ramamurthy

  3. 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. B.Ramamurthy

  4. 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. B.Ramamurthy

  5. 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. B.Ramamurthy

  6. 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. B.Ramamurthy

  7. 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. B.Ramamurthy

  8. 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? B.Ramamurthy

  9. 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 B.Ramamurthy

  10. ULT : Advantages and Disadvantages • Advantages: • 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. B.Ramamurthy

  11. 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! B.Ramamurthy

  12. 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. B.Ramamurthy

  13. 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 B.Ramamurthy

  14. 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. B.Ramamurthy

  15. Solaris Support for Multithreading Process user LWP Kernel threads kernel bound processors hardware B.Ramamurthy

  16. 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. B.Ramamurthy

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

  18. 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. B.Ramamurthy

  19. 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. B.Ramamurthy

  20. 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. B.Ramamurthy

  21. 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. B.Ramamurthy

  22. 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. B.Ramamurthy

  23. 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 B.Ramamurthy

  24. 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. B.Ramamurthy

  25. 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. B.Ramamurthy

  26. 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. B.Ramamurthy

  27. 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. B.Ramamurthy

  28. 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. B.Ramamurthy

  29. 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. B.Ramamurthy

  30. 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. B.Ramamurthy

  31. 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. B.Ramamurthy

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

  33. 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. B.Ramamurthy

  34. 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. B.Ramamurthy

  35. 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. B.Ramamurthy

  36. 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. B.Ramamurthy

  37. 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. B.Ramamurthy

  38. 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. B.Ramamurthy

  39. 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. B.Ramamurthy

  40. 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. B.Ramamurthy

  41. 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”. B.Ramamurthy

  42. 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. B.Ramamurthy

More Related