Threads, Thread management & Resource Management.

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
Slide 2-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Using the Operating System 2.
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
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.
Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Implementing Processes, Threads, and Resources.
Process Management. External View of the OS Hardware fork() CreateProcess() CreateThread() close() CloseHandle() sleep() semctl() signal() SetWaitableTimer()
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Chapter 4: Threads.
1 Chapter 4 Threads Threads: Resource ownership and execution.
Chapter 6 Implementing Processes, Threads, and Resources.
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6.
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.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Threads, Thread management & Resource Management.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
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)
System calls for Process management
Chapter 2 Processes and Threads Introduction 2.2 Processes A Process is the execution of a Program More specifically… – A process is a program.
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: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Department of Computer Science and Software Engineering
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Module 2.0: Threads.
CSC 322 Operating Systems Concepts Lecture - 7: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
2 Processor(s)Main MemoryDevices Process, Thread & Resource Manager Memory Manager Device Manager File Manager.
Threads. Readings r Silberschatz et al : Chapter 4.
What is a Process ? A program in execution.
System calls for Process management Process creation, termination, waiting.
Operating Systems Unit 2: – Process Context switch Interrupt Interprocess communication – Thread Thread models Operating Systems.
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-
CSCI/CMPE 4334 Operating Systems Review: Exam 1 1.
Advanced Operating Systems CS6025 Spring 2016 Processes and Threads (Chapter 2)
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
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 Modified by Dr. Neerja Mhaskar for CS 3SH3.
OPERATING SYSTEMS CS3502 Fall 2017
Day 12 Threads.
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Chapter 4 Threads.
Operating Systems: A Modern Perspective, Chapter 6
Threads & multithreading
Chapter 4: Threads.
Operating System Concepts
Chapter 4: Threads.
Chapter 4: Threads.
Threads, SMP, and Microkernels
Mid Term review CSC345.
Threads Chapter 4.
Threads and Concurrency
Threads Chapter 4.
CHAPTER 4:THreads Bashair Al-harthi OPERATING SYSTEM
Threads Chapter 5 2/17/2019 B.Ramamurthy.
Threads Chapter 5 2/23/2019 B.Ramamurthy.
Still Chapter 2 (Based on Silberchatz’s text and Nachos Roadmap.)
Implementing Processes, Threads, and Resources
Concurrency, Processes and Threads
Implementing Processes, Threads, and Resources
Chapter 4: Threads.
Presentation transcript:

Threads, Thread management & Resource Management

Process manager responsibilities Process creation and termination Thread creation and termination Process synchronization Resource allocation Protection & security Implementing address space Operating Systems: A Modern Perspective, Chapter 6

Algorithms, Programs, and Processes Data Files Other Resource s Algorithm Ide a Source Program Binary Program Execution Engine Process Stac k Stat us

Operating Systems: A Modern Perspective, Chapter 6 OS Address Space Implementing the Process Abstraction Control Unit OS interface … Machine Executable Memory ALU CPU P i Address Space P i CPU P i Executable Memory P k Address Space … P k CPU P k Executable Memory P j Address Space P j CPU P j Executable Memory Ideal Abstraction: As if using an Actual machine

Operating Systems: A Modern Perspective, Chapter 6 Processes and Threads OS interface … … … P i CPU Thrd j in P i Thrd k in P i …

6 Processes and Threads ▪Threads ➢ Threads are schedulable activities attached to processes. ➢ The aim of having multiple threads of execution is : ❖ To maximize degree of concurrent execution between operations ❖ Threads are faster to create and destroy than processes ❖ Natural for multiple cores ❖ Threads are very useful in modern programming whenever a process has multiple tasks to perform independently of the others.

Process and Thread – Process is an infrastructure in which execution takes place – address space + resources – Thread is a program in execution within a process context – each thread has its own stack Data Proce ss Stack Progr am Operating System Threa d Stack …

A Process with Multiple Threads Data Files Other Resource s Binary Program Process Stac k Stat us Stac k Stat us Stac k Stat us Thread (Execution Engine)

Multi thread application - example For example in a word processor, a background thread may check spelling and grammar while a foreground thread processes user input ( keystrokes ), while yet a third thread loads images from the hard drive, and a fourth does periodic automatic backups of the file being edited.

Single Thread Vs Multi Thread

Operating Systems: A Modern Perspective, Chapter 6 Thread Abstraction Process Manager has algorithms to control threads and thread descriptor (data structure) to keep track of threads. Management Tasks - Create/destroy thread - Allocate thread-specific resources - Manage thread context switching Thread Descriptor - state - execution stats - process (reference to associated process) - list of related threads - stack (reference to stack) - thread-specific resources

Threads are lightweight Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

Have same states Running Ready Blocked Have their own stacks –same as processes Stacks contain frames for (un-returned) procedure calls Local variables Return address to use when procedure comes back Threads are like processes Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

14 Processes and Threads ▪Processes vs. Threads ➢ Threads are “lightweight” processes, ➢ processes are expensive to create but threads are easier to create and destroy.

Process & Thread Modern operating system Unit of resource ownership is usually referred to as process The unit of dispatching is usually referred to as a thread. Thus a thread Has an execution state Saves thread context when not running Has access to memory address space & its resources

Advantage of using threads It takes less time to create a new thread than a process It takes less time to terminate a thread than a process Context switch between two threads within the same process involves lesser time. Communication overhead is minimized.

Thread implementation There are two broad categories of thread implementation User Level Threads – thread libraries Kernel Level Threads – system calls In ULT, kernel is not aware of the existence of threads In KLT, all thread management is done by kernel. There is no thread library

How Linux actually implemented user level threads! ❑ In the kernel, fork is actually implemented by a clone() system call. ❑ Clone allows you to explicitly specify which parts of the new process are copied into the new process, and which parts are shared between the two processes. ❑ This may seem a bit strange at first, but allows us to easily implement threads with one very simple interface.

➢ While fork copies all of the attributes we mentioned above, imagine if everything was copied for the new process except for the memory. This means the parent and child share the same memory, which includes program code and data. ➢ This hybrid child is called a thread.

Kernel Support for Process Text Stack Data File Descriptor Table Per Process Region Table Kernel Process Table Kernel Region Table A Process U Area

Clone ( ) System Call Thread clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0); Process with fork( ) clone(SIGCHLD, 0); Process with vfork( ) clone(CLONE_VFORK | CLONE_VM | SIGCHLD, 0);

Advantages of Thread over process ❑ Separate processes can not see each others memory. They can only communicate with each other via other system calls(IPC). ❑ Threads however, share the same memory. ❑ The problem that this raises is that threads can very easily step on each others toes. One thread might increment a variable, and another may decrease it without informing the first thread. ❑ These type of problems are called concurrency problems

Pthreads a POSIX standard (IEEE c) API for thread creation and synchronization. API specifies behavior of the thread library, implementation is up to development of the library. Common in UNIX operating systems.

Pthreads are IEEE Unix standard library calls POSIX Threads (Pthreads) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

. A Pthreads example-”Hello,world” Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

Summary : Process and Threads Process: encompasses – Unit of dispatching: process is an execution path through one or more programs set of threads (computational entities) execution may be interleaved with other processes – Unit of resource ownership: process is allocated a virtual address space to hold the process image Thread: Dynamic object representing an execution path and computational state. – threads have their own computational state: PC, stack, user registers and private data – Remaining resources are shared amongst threads in a process

Resources Resource: Anything that a process can request and then become blocked because that thing is not available. Resource Descriptors - Internal resource name - Total Units - Available Units - List of available units - List of Blocked processes

Process, Thread, and Resource Management … Processor Primary Memor y Abstract Resource s Multiprogramming Thread Abstraction Process Abstraction Generic Resource Manager Other

Resources R = {R j | 0 ≤ j < m} = resource types C = {c j ≥ 0 | ∀ R j ∈ R (0 ≤ j < m)} = units of R j available Reusable resource: After a unit of the resource has been allocated, it must ultimately be released back to the system. E.g., CPU, primary memory, disk space, … The maximum value for c j is the number of units of that resource Consumable resource: There is no need to release a resource after it has been acquired. E.g., a message, input data, … Notice that c j is unbounded.

A Generic Resource Manager Proc ess Resource Manager Proc ess Blocked Processes Resource Pool request() release() Policy