Threads G.Anuradha (Reference : William Stallings)

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

Chapter 4 Threads Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William.
Threads, SMP, and Microkernels
Operating Systems: Internals and Design Principles
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.
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.
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,
Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
1 Threads, SMP, and Microkernels Chapter 4. 2 Process: Some Info. Motivation for threads! Two fundamental aspects of a “process”: Resource ownership Scheduling.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
3.5 Interprocess Communication
Threads CSCI 444/544 Operating Systems Fall 2008.
1 Chapter 4 Threads Threads: Resource ownership and execution.
Threads, SMP, and Microkernels
Inter-Process Communication  most OSs provide several abstractions for inter- process communication: message passing, shared memory, etc.  communication.
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.
Threads. Processes and Threads  Two characteristics of “processes” as considered so far: Unit of resource allocation Unit of dispatch  Characteristics.
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.
A. Frank - P. Weisberg Operating Systems Threads Implementation.
Process Description and Control A process is sometimes called a task, it is a program in execution.
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.
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.
Multithreading Allows application to split itself into multiple “threads” of execution (“threads of execution”). OS support for creating threads, terminating.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
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.
Chapter 3 Process Description and Control
Processes and Threads Processes have two characteristics: – Resource ownership - process includes a virtual address space to hold the process image – Scheduling/execution.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
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.
Threads, SMP, and Microkernels Chapter 4. Threads, SMP, and Microkernels 1. Processes and threads 2. Symmetric MultiProcessing (SMP) 3. Microkernel: structuring.
1 Lecture 4: Threads Advanced Operating System Fall 2010.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts What is Thread “Thread is a part of a program.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Operating System 4 THREADS, SMP AND MICROKERNELS.
Department of Computer Science and Software Engineering
Thread By Group III Kathryn Bean and Wafa’ Jaffal.
Chapter 4 Threads Seventh Edition By William Stallings Operating Systems: Internals and Design Principles.
Operating Systems: Internals and Design Principles
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Module 2.0: Threads.
Threads-Process Interaction. CONTENTS  Threads  Process interaction.
Threads. Thread A basic unit of CPU utilization. An Abstract data type representing an independent flow of control within a process A traditional (or.
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.
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,
Process Management Presented By Aditya Gupta Assistant Professor
Chapter 4 Threads.
Principles of Operating Systems Lecture 8
Threads, SMP, and Microkernels
Threads Chapter 4.
Lecture 4- Threads, SMP, and Microkernels
Operating System 4 THREADS, SMP AND MICROKERNELS
Threads Chapter 4.
Presentation transcript:

Threads G.Anuradha (Reference : William Stallings)

Characteristics of a Process Resource Ownership: Process image is in a virtual address space and time to time it has been allocated resources. OS provides protection to the process (Process or Task) Scheduling/execution: Process has an execution state and priority and OS does the scheduling and execution (Thread or Light Weight Process)

Multithreading Ability of and OS to support multiple threads of execution within a single process Single thread of execution per process is called as single-threaded approach Process in a multithreaded environment is the unit of resource allocation and protection – Virtual address space that holds the process image – Protected access to processor(s),resources

Threads and Processes MS-DOS UNIX Java Windows 2000 Solaris Linux

Thread Within a process there may be thread(s) – Has an execution state – Saved thread context when not running – An execution stack – Some per-thread static storage for local variables – Access to memory and resources of its process shared with all other threads in that process

Single threaded and multithreaded process models Share the state and resources of the process and reside in the same address space and access the same data

Benefits of threads 1.Takes Less time to create a thread than a process 2.Takes less time to terminate a thread than a process 3.Takes less time to switch between threads within the same process 4.Enhance efficiency in communication between different executing programs.

Examples of users of threads in single –user multiprocessing system Foreground and background work Asynchronous processing Speed execution Modular program structure Note:- 1. Suspension of a process suspends all the threads in that process as well 2. Termination of a process terminates all the threads in that process as well

Thread Functionality Thread States Thread Synchronization User level threads Kernel level threads

Thread states Running Ready Blocked Note:- There is no suspended state becos its only in the process level. If a process is suspended all threads associated with it are also suspended

Operations associated with threads Associated with change in thread state we have four basic thread operations Spawn: When a new process is spawned a thread for that process is also spawned. Block: When a thread need to wait for an event it will block. The processor may now turn to the execution of another ready thread. Unblock: After the event for a blocked thread occurs its moved into the ready queue Finish: After a thread completes, its register context and stacks are deallocated.

Performance benefits of threads that do not block and entire process

Multithreading example on a uniprocessor

Thread Synchronization All threads of a process share the same address space and other resources of a process Any alternation in the resources by one thread affects the environment of other threads in the same process Activities of various threads need to be properly synchronised

Types of threads User-level threads Kernel-level threads

User-level threads Application does the work of thread management Thread libraries are used for this purpose Kernel is unaware of the thread creations and schedules the process as a unit assigning a single execution state Thread library Data structure for the new thread Passes control to one of the threads within this process that is in the ready state

Relationship between ULT states and Process states

In figures b and c kernel switches control back to process B so execution resumes in thread 2

Advantages of ULT 1.Thread switching requires only user mode privileges. So its saves the overhead of two mode switches 2.Scheduling is application specific without disturbing the OS scheduler 3.ULTs run on any operating system. Thread library utilities are shared by all applications

Disadvantage of ULT Many system calls are blocking system calls. This not only blocks that thread but all threads in a process Multithreaded application cannot take advantage of multiprocessing. One single thread within a process can execute at a time.

How to overcome the disadvantages? Write an application as multiple processes rather than multiple threads. This eliminates the advantage of threads. Overhead increases Jacketing:- convert a blocking system call into nonblocking system call. In this case instead of calling a I/O routine a I/o jacket routine is called which checks whether the I/O is buzy or not. If I/O is buzy it enters the Ready state and passes control to another thread.

Kernel level threads No thread management code in the application area Just an API All threads are supported by a single process. Scheduling by kernel is done by thread basis

Advantages and disadvantages of KLT Advantages – Kernel can simultaneously schedule multiple threads from the same process on multiple processors – If one thread is blocked the kernel can schedule another thread Disadvantage – Transfer of control is mode switch

Combined Approaches Thread creation, scheduling, synchronization of threads is done in the user space. Multiple ULTs can be mapped onto some number of KLTs Multiple threads within the same application can run in parallel on multiple processors Blocking system call need not block the entire process

Relationship Between Threads and Processes

TRIX Follows a many-to-many relationship between threads and processes There is a concept of domain and thread – Domain is a static entity consisting of an address space and ports through which messages may be send and received – Thread is a single execution path with an execution stack, processor state and scheduling information Multiple threads may execute in a single domain or a single user activity can be performed in multiple domains as well

Implementation of TRIX 1.Single process. Requires sufficient main memory but I/O subprogram requires a small address space 2.Main program and I/O can be implemented as two separate processes. 3.Main program and I/O subprogram as a single activity implemented as a single thread. But one domain to be created for the main program and one for I/O subprogram. Thread can be moved between two address spaces

One-To-Many Relationship Used in distributed operating systems. Eg Cloud operating systems Activity CASE STUDIES ON trix CASE STUDIES ON CLOUD OS

UNIX SVR4 Process Management In UNIX most of the OS executes within the environment of a user process. So it has user and kernel mode of execution

Categories of process in unix System processes – Run in kernel mode – Executes OS code to perform administrative and housekeeping function User Processes – Run in user mode – Executes user programs and utilities in kernel mode to execute instructions which belong to the kernel – Enters the kernel mode by issuing a system call when an exception is generated or when an interrupt occurs

UNIX process state transition Diagram

Process states in Unix

Processes which are unique in UNIX Process 0 is a special process that is created when the system boots. (Swapper process) Process 0 spawns process 1 (init process) All processes in UNIX has process 1 as its ancestor

Process Description Process Image User –level context Register ContextSystem level context

User-level context

Register context

System level context

Process Control When a parent process is forked(kernel mode) – A slot is allocated in the process table for the new process – An unique process ID is assigned to child process – Copy of process image of the parent is made – Increments counters for any files owned by the parent – Assigns the child process to a ready to run state – Returns the ID number of the child to the parent process and 0 to child process

Dispatcher routine Stay in parent process. Control returns to user mode at the point of fork call of parent Transfer control to child process Transfer control to another process. Both parent and child are left in Ready to Run state.

Linux process and thread mgt Task- task struct Task-struct – State:Execution state of a process – Scheduling information – Identifiers- process has UID and group has GID – Interprocess communicaiton – Links – Times and timers – File system – Virtual memory – Processor-specific context

Linux process/Thread model