1 / 26

Threads

Threads. Irfan Khan Myo Thein. What Are Threads ?. a light, fine, string like length of material made up of two or more fibers or strands of spun cotton, flax, silk, etc. twisted together and used in sewing Webster’s New World Dictionary. Overview of Process.

swain
Download Presentation

Threads

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 Irfan Khan Myo Thein

  2. What Are Threads ? • a light, fine, string like length of material made up of two or more fibers or strands of spun cotton, flax, silk, etc. twisted together and used in sewing Webster’s New World Dictionary

  3. Overview of Process • An abstraction of a running program • A process includes the code, current value of the program counter, registers, and variables • CPU switches from process to process

  4. Process Image • User program • User data • Stack(s) • For function calls and parameter passing • Process control block(execution context, or state) • Pointers to all of the above • Attributes needed by the OS to control the process • Process identification information • Processor state information • Process control information

  5. Process Identification in PCB • A few numeric identifiers may be used • Unique process identifier • Indexes (directly or indirectly) into the primary process table • User identifier • The user who initiated the process • Effective user: the user whose permissions the process inherits • Identifier for the process that created this process • I.E. A pointer to the process’ parent

  6. Processor State Information in PCB • Contents of processor registers • User-visible registers • Control and status registers • Stack pointers • Program status word (PSW) • Contains status information • Example: the EFLAGS register on Pentium machines

  7. Process Control Information in PCB • Scheduling and state information • Process state (i.E.: Running, ready, blocked...) • Priority of the process • Event for which the process is waiting (if blocked) • Process privileges • Access to certain memory locations • OS resources • Memory management • Pointers to segment/page tables assigned to this process • Resource ownership and utilization • Resource in use: open files, I/O devices... • History of usage: accounting (of CPU time, I/O...)

  8. Process Creation • Assign a unique process identifier • Allocate space for the process image • Initialize process control block • Many default values (example: state is new, no I/O devices or files...) • Set up appropriate linkages • Example: add new process to linked list used for the scheduling queue

  9. Context Switching • A context switch may occur whenever the OS is invoked • System call • Explicit request by the program, such as open file • The process will likely be blocked • OS will dispatch a new process • Trap • An error resulted from the last instruction • May cause the process to be moved to the terminate state • Interrupt • The cause is external to the execution of the current instruction • Control is transferred to the interrupt handler • Hence the OS is event driven

  10. Steps in Context Switching • Save context of processor including program counter and other registers • Update the PCB of the running process with its new state and other associate info • Move PCB to appropriate queue - ready, blocked • Select another process for execution • Update PCB of the selected process • Restore CPU context from the PCB

  11. Introduction to Threads • A process can be considered as based on • Resource grouping • Execution • Its own address space • A thread represents the execution part of a process. • A thread has a program counter, register states, stack pointer. • All threads of a process share its address space. • All threads of a process share its resources.

  12. Threads in Process

  13. Why Use Threads Over Processes • Both thread and process models provide concurrent program execution • Creating new process can be expensive • It takes time: calling into the OS kernel is needed • Can trigger process rescheduling activity: context switch • It takes up memory resource: entire process is replicated • Communication and synchronization is expensive • Requiring calling into the OS kernel

  14. Why Use Threads Over Processes, contd • Threads can be created without replicating an entire process • Most of the work of creating a thread is done in user space rather than the OS kernel • Thread can synchronize by monitoring a variable, as opposed to processes that require calling into the OS kernel • The benefits of the thread model results from staying inside the user address space of the program

  15. Multithreading – multiple threads in the same process

  16. Multithreading OS • MS-DOS – single process, single thread • UNIX – multiple processes, single thread per process • JVM – single process with multiple threads • Windows 2000, Linux, OS/2, Solaris – multiple processes with multiple threads

  17. Thread states – running, blocked, or ready. • Each thread has its own stack.

  18. Thread Usage • Example: word processor • Displays contents; • Edits (e.G. Typing); • Reformats; • Auto saves; • Printing etc.

  19. Why Threads Become Popular Now? • SMPs(symmetric multiprocessors) • 2 to 64 processors sharing • Buss • I/O system • Same memory • One operating system for all processors • Examples: • SGI PowerChallenge (8 MIPS 1000 CPUs) -- CYC807 • Sun ultra enterprise 6000 (8 CPUs) -- CYC807 • ALR SMP server (4 Pentium pro) - CYC414

  20. Three Types of Thread System • Kernel-supported threads (mach, OS/2, NT) • User-level threads; Supported above the kernel, via a set of library calls at the user level (linux via clone) • Hybrid approach implements both user-level and kernel-supported threads (Solaris 2)

  21. Kernel-level Versus User-level Threads • User-level thread • User-level activities; No kernel involvement • Basic scheduling unit in OS is process • Threads of the same process can not run on different CPUs in SMP in parallel • Kernel-level thread • Each process consists of several threads • Basic scheduling unit is thread • Can run on different CPUs in SMP in parallel

  22. Advantages of Kernel Threads • Higher application throughput • If there were no kernel thread support • Need I/O; It means the process goes into waiting state and wait until the I/O is complete • With multiple kernel threads per task • Block the I/O requesting thread and continue to work on another thread • Increases the overall throughput of the application

  23. Advantages of User Level Threads • Threads are cheap • Can be implemented at user levels, no kernel resources • Threads are fast • No system calls, switching modes involved

  24. Sun Solaris 2 • Mixed approach • OS schedules light-weight process (LWP) • User-level library schedules user-level threads • User threads are cheap, can be thousands per task • Each LWP supports one or more user threads • LWPs are what we’ve been calling kernel threads • Solaris has entities called kernel threads; They are scheduling artifacts contained in the OS

  25. User/ Kernel Threads in Sun Solaris 2 User-level thread Task 1 Task 2 Task 3 Light weight process (LWP) Kernel thread KERNEL CPU CPU CPU CPU

  26. Thank You

More Related