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,

Slides:



Advertisements
Similar presentations
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Advertisements

More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Chapter 3 Process Description and Control
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.
Chapter 5 Threads os5.
Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
Introduction to Processes
Chapter 5 Processes and Threads Copyright © 2008.
Process Description and Control Module 1.0. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor.
Process Description and Control Chapter 3. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
1 Process Description and Control Chapter 3. 2 Process Management—Fundamental task of an OS The OS is responsible for: Allocation of resources to processes.
CSCE 351: Operating System Kernels
3.5 Interprocess Communication
Processes and Resources
1 Chapter 4 Threads Threads: Resource ownership and execution.
Threads, SMP, and Microkernels
UNIX Process Creation Every process, except process 0, is created by the fork() system call fork() allocates entry in process table and assigns a unique.
Process Concept An operating system executes a variety of programs
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
Process Description and Control A process is sometimes called a task, it is a program in execution.
Using Two Queues. Using Multiple Queues Suspended Processes Processor is faster than I/O so all processes could be waiting for I/O Processor is faster.
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.
Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place  (address space + resources) –Thread is a program.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Multithreaded Programming.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
Processes Part I Processes & Threads* *Referred to slides by Dr. Sanjeev Setia at George Mason University Chapter 3.
Process Description and Control
Process Description and Control Chapter 3. Major Requirements of an OS Interleave the execution of several processes to maximize processor utilization.
Chapter 4 Threads, SMP, and Microkernels Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E.
Process Description and Control
Chapter 3 Process Description and Control Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Operating Systems Lecture 09: Threads (Chapter 4)
Chapter 4: Threads. From Processes to Threads 4.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Threads.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Chapter 3 Process Description and Control Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Chapter 3 Advanced Operating System
Chapter 3 Process Description and Control
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
Processes and Threads Processes have two characteristics: – Resource ownership - process includes a virtual address space to hold the process image – Scheduling/execution.
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 13 Threads Read Ch 5.1.
Threads G.Anuradha (Reference : William Stallings)
Processes and Process Control 1. Processes and Process Control 2. Definitions of a Process 3. Systems state vs. Process State 4. A 2 State Process Model.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
1 Threads, SMP, and Microkernels Chapter Multithreading Operating system supports multiple threads of execution within a single process MS-DOS.
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.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts What is Thread “Thread is a part of a program.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Operating Systems: Internals and Design Principles
Process Description and Control Chapter 3. Source Modified slides from Missouri U. of Science and Tech.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Module 2.0: Threads.
Processes & Threads Introduction to Operating Systems: Module 5.
1 OS Review Processes and Threads Chi Zhang
Threads-Process Interaction. CONTENTS  Threads  Process interaction.
1 Process Description and Control Chapter 3. 2 Process A program in execution An instance of a program running on a computer The entity that can be assigned.
Operating System Concepts
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 Chapter 4. Processes and Threads Operating systems use processes for two purposes - Resource allocation and resource ownership.
Lecturer 3: Processes multithreaded Operating System Concepts Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess.
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Chapter 4 Threads.
ICS 143 Principles of Operating Systems
Process Description and Control
Threads Chapter 4.
Presentation transcript:

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 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

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

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

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

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...)

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

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

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

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.

Threads in Process

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

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

Multithreading – multiple threads in the same process

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

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

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

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

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)

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

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

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

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

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

Thank You