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

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. Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems.
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.
Chapter 4: Multithreaded Programming
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Implementing Processes, Threads, and Resources.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Based on Silberschatz, Galvin and Gagne  2009 Threads Definition and motivation Multithreading Models Threading Issues Examples.
Process Management. External View of the OS Hardware fork() CreateProcess() CreateThread() close() CloseHandle() sleep() semctl() signal() SetWaitableTimer()
Chapter 2 Processes and Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
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.
Threads. Processes and Threads  Two characteristics of “processes” as considered so far: Unit of resource allocation Unit of dispatch  Characteristics.
Chapter 6 Implementing Processes, Threads, and Resources.
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
A. Frank - P. Weisberg Operating Systems Threads Implementation.
Chapter 4: Threads READ 4.1 & 4.2 NOT RESPONSIBLE FOR 4.3 &
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.
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.
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.
Multithreading Allows application to split itself into multiple “threads” of execution (“threads of execution”). OS support for creating threads, terminating.
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
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-
System calls for Process management
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
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
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,
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.
2 Processor(s)Main MemoryDevices Process, Thread & Resource Manager Memory Manager Device Manager File Manager.
Cs431-cotter1 Processes and Threads Tanenbaum 2.1, 2.2 Crowley Chapters 3, 5 Stallings Chapter 3, 4 Silberschaz & Galvin 3, 4.
What is a Process ? A program in execution.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
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-
Chapter 4: Threads 羅習五. Chapter 4: Threads Motivation and Overview Multithreading Models Threading Issues Examples – Pthreads – Windows XP Threads – Linux.
CSCI/CMPE 4334 Operating Systems Review: Exam 1 1.
Contents 1.Overview 2.Multithreading Model 3.Thread Libraries 4.Threading Issues 5.Operating-system Example 2 OS Lab Sun Suk Kim.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Advanced Operating Systems CS6025 Spring 2016 Processes and Threads (Chapter 2)
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
OPERATING SYSTEM CONCEPT AND PRACTISE
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Chapter 4 Threads.
Operating Systems: A Modern Perspective, Chapter 6
Chapter 4: Threads.
Threads and Concurrency
Threads Chapter 4.
CHAPTER 4:THreads Bashair Al-harthi OPERATING SYSTEM
Implementing Processes, Threads, and Resources
CS510 Operating System Foundations
Concurrency, Processes and Threads
Implementing Processes, Threads, and Resources
Chapter 4: Threads.
Presentation transcript:

Threads, Thread management & Resource Management

Algorithms, Programs, and Processes Data Files Other Resources Algorithm Idea Source Program Source Program Binary Program Execution Engine Process Stack Status

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

OS Address Space OS Address Space Implementing the Process Abstraction Control Unit OS interface … Machine Executable Memory ALU CPU P i Address Space P i Address Space P i CPU P i Executable Memory P i Executable Memory P k Address Space P k Address Space … P k CPU P k Executable Memory P k Executable Memory P j Address Space P j Address Space P j CPU P j Executable Memory P j Executable Memory Ideal Abstraction: As if using an Actual machine

Operating Systems: A Modern Perspective, Chapter 6 Modern 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  To enable the overlap of computation with input and output  To enable concurrent processing on multiprocessors.

Enables parallelism (web server) with blocking system calls Threads are faster to create and destroy then processes Natural for multiple cores Easy programming model Reasons to use threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

Divide classic process: – 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 Process Stack Program Operating System Thread Stack …

A Process with Multiple Threads Data Files Other Resources Binary Program Process Stack Status Stack Status Stack Status Thread (Execution Engine)

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

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

15 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 A Classic process is allocated memory space to hold its image and its resources 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.

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

(a) A user-level threads package. (b) A threads package managed by the kernel. Implementing Threads in User Space Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

Hybrid approach Multiplex user-level threads onto kernel level threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

Kernel is aware of kernel threads only User level threads are scheduled, created destroyed independently of kernel thread Programmer determines how many user level and how many kernel level threads to use Hybrid Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved

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.

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

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.

User level thread implementation! Threads have a number of advantages over where you might use fork Separate processes can not see each others memory. They can only communicate with each other via other system calls. Threads however, share the same memory. So you have the advantage of multiple processes, with the expense of having to use system calls to communicate between them. 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 and they are many and varied.

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

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 Memory Abstract Resources Multiprogramming Thread Abstraction Thread Abstraction Process Abstraction Process Abstraction Generic Resource Manager 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.

Using the Model There is a resource manager, Mgr(R j ) for every R j Mgr(R j ) Process p i can only request n i  c j units of reusable R j p i can request unbounded # of units of consumable R j Process p i can request units of R j if it is currently running request Mgr(R j ) can allocate units of R j to p i allocate

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