Operating Systems ECE344 Ashvin Goel ECE University of Toronto Threads and Processes.

Slides:



Advertisements
Similar presentations
Processes Management.
Advertisements

Introduction to Operating Systems CS-2301 B-term Introduction to Operating Systems CS-2301, System Programming for Non-majors (Slides include materials.
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.
Threads. Announcements Cooperating Processes Last time we discussed how processes can be independent or work cooperatively Cooperating processes can.
Processes CSCI 444/544 Operating Systems Fall 2008.
Chapter 4: Threads. 2 What’s in a process? A process consists of (at least): –an address space –the code for the running program –the data for the running.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
1 Chapter 4 Threads Threads: Resource ownership and execution.
Processes April 5, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
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.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
1 From Processes to Threads. 2 Processes, Threads and Processors Hardware can interpret N instruction streams at once  Uniprocessor, N==1  Dual-core,
CS 162 Discussion Section Week 1 (9/9 – 9/13) 1. Who am I? Kevin Klues Office Hours:
Threads, Thread management & Resource Management.
Implementing Processes and Process Management Brian Bershad.
Introduction to Threads CS240 Programming in C. Introduction to Threads A thread is a path execution By default, a C/C++ program has one thread called.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Overview of Hardware.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto OS-Related Hardware.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Thread Scheduling.
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.
Background: Operating Systems Brad Karp UCL Computer Science CS GZ03 / M th November, 2008.
Processes Introduction to Operating Systems: Module 3.
11/13/20151 Processes ICS 240: Operating Systems –William Albritton Information and Computer Sciences Department at Leeward Community College –Original.
CS333 Intro to Operating Systems Jonathan Walpole.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Process-Concept.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Virtual Memory Hardware.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Process-Concept.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Unix System Calls and Posix Threads.
Department of Computer Science and Software Engineering
Processes and Virtual Memory
Thread basics. A computer process Every time a program is executed a process is created It is managed via a data structure that keeps all things memory.
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. Readings r Silberschatz et al : Chapter 4.
Chapter 2 Process Management. 2 Objectives After finish this chapter, you will understand: the concept of a process. the process life cycle. process states.
Goals for Today How do we provide multiprogramming? What are Processes? How are they related to Threads and Address Spaces? Note: Some slides and/or pictures.
3/12/2013Computer Engg, IIT(BHU)1 PARALLEL COMPUTERS- 2.
What is a Process ? A program in execution.
Background Computer System Architectures Computer System Software.
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
Embedded Real-Time Systems
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
CS 162 Discussion Section Week 2. Who am I? Prashanth Mohan Office Hours: 11-12pm Tu W at.
Processes and threads.
Process concept.
Operating Systems CMPSC 473
Process Management Process Concept Why only the global variables?
Chapter 3: Process Concept
Chapter 1: A Tour of Computer Systems
CS 6560: Operating Systems Design
Lecture Topics: 11/1 Processes Process Management
CS399 New Beginnings Jonathan Walpole.
Process Realization In OS
Chapter 4: Threads.
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Understanding Program Address Space
Lecture Topics: 11/1 General Operating System Concepts Processes
Processes Hank Levy 1.
Processes and Process Management
CS510 Operating System Foundations
CSE 153 Design of Operating Systems Winter 2019
Processes Hank Levy 1.
Threads.
Presentation transcript:

Operating Systems ECE344 Ashvin Goel ECE University of Toronto Threads and Processes

Overview  Threads  Address space  Processes  OS-level process state 2

Program  A program is a static file containing instructions + data o Describes how to perform a computation  When a program is run, OS maintains state for the various abstractions its provides to the running program o Thread (CPU) state  Holds registers, PC, SP, etc. o Address space (memory) state  Holds program instructions, static and dynamic data values o Device and other state  Holds state such as open files, network connection state, etc. 3

Threads  OS abstraction for virtualizing the CPU o Each thread runs on CPU, thinks it has its own set of CPU registers, unaware of other threads  # of threads is arbitrary, # of CPUs is fixed o Implemented by multiplexing thread instruction streams 4 A B C Time, uniprocessor A B C Time, dual processor

Comparing Functions with Threads 5 Functions F1F2 F1 calls F2 F2 returns to F1 F2 runs to completion Threads (on single CPU) T1T2 switch suspend resume suspend resume stream of instructions

Benefits of Threads  Allow running multiple programs concurrently  Allow a program to run multiple tasks concurrently o Help hide I/O latency: if one thread has to wait on I/O, then another thread can run while the first thread blocks  E.g., a web server could use a thread to receive a request, read a file and send it  How would you implement this functionality without threads?  Hiding I/O latency: have we seen this before? o With multiple CPUs, tasks can be run truly in parallel  E.g., a parallel matrix multiple program 6

Address Space  An address space is the set of (virtual) memory regions accessible to a program o Text – the program code (usually read only) o Data, heap – static, dynamic variables o Stack – used for function and system calls  OS abstraction for virtualizing memory o Each running program has its own address space 7 Address space stack text data SP PC stack text data SP PC

Review of Program Data 8 int var; int p = &var; A program variable is a symbolic name for some data stored in memory Type of variable determines size and alignment of memory Address of variable is the index of memory "array" where data is stored, e.g., if data is stored at memory[i], then address is i, value of variable is the contents of memory[i] Pointer is a variable whose value is a memory location, e.g., value of pointer variable is i, dereferencing pointer gives memory[i]

Review of Program Execution 9 main(), f() a = 5; prev fp other regs b c r ret val ret addr int a = 5; int main(int r) { int b = 10, c; c = f(b+c); } int f(int x) { int y = x; return y; } text (code) data (globals, heap) pc activation frame of main() fp (frame ptr) sp (stack ptr) parameter of main() return value of main() address where main() will return

Function Call and Return 10 main(), f() a = 5; ret addr prev fp other regs b c x = b+c prev fp other regs y ret value r ret val ret addr int a = 5; int main(int r) { int b = 10, c; c = f(b+c); } int f(int x) { int y = x; return y; } text (code) data (globals, heap) pc fp (frame ptr) sp (stack ptr) activation frame of main() parameter of f() activation frame of f() return value of f() address in main() to return to

Process  A running program consists of one or more processes  Traditionally: o process = address space + thread  Address space provides memory protection  Thread enables concurrency  Communicate using system calls  Today: o process = address space, with one or more threads  Threads share address space –E.g., threads share data (e.g., arrayA), code, but don’t share stack  Communicate with reading/writing memory 11

How to Speedup Vector Operation  How would you speedup this program? o Using multiple processes o Using threads in a single process  Would there be any speedup on a single processor? o How is the speedup achieved?  Would there be any speedup on a multiprocessor? o How is the speedup achieved?  E.g., client-server program o One process for server + one process per client o Server may run using multiple threads  Threads communicate using shared address space o Useful when threads cooperate heavily, e.g., server cache  Processes communicate using buffers (e.g., files) o Provides isolation, e.g., browser tabs  Benefits of using threads vs. separate processes o Faster communication by reading/writing memory o Lower memory needs because address space is shared o Faster because thread switching doesn’t require switching address space 12 For (k = 0; k < n; k++) a[k] = b[k] * c[k] + d[k] * e[k];

How to Speedup Web Server Program  How would you speedup this program? o Using multiple processes o Using threads in a single process  Would there be any speedup on a single processor? o How is the speedup achieved?  Would there be any speedup on a multiprocessor? o How is the speedup achieved?  E.g., client-server program o One process for server + one process per client o Server may run using multiple threads  Threads communicate using shared address space o Useful when threads cooperate heavily, e.g., server cache  Processes communicate using buffers (e.g., files) o Provides isolation, e.g., browser tabs  Benefits of using threads vs. separate processes o Faster communication by reading/writing memory o Lower memory needs because address space is shared o Faster because thread switching doesn’t require switching address space 13 Run in loop: 1. get network message (URL) from client 2. get URL data from disk, cache in memory 3. compose response 4. send response

Threads versus Processes 14 ThreadsProcesses Memory neededShared, lessNot shared, more Communication & Synchronization Via shared variables, fasterVia system calls, slower SwitchingFasterSlower RobustnessMemory sharing can cause hard-to-detect bugs All communication is explicit, more robust program design

OS-level Process State  OS keeps state for each process o Called process state, process control block (PCB), etc.  The per-process state consists of o Thread state (for one or more threads)  Processor registers, allows resuming a suspended thread  Thread id, uniquely identifies thread  Various parameters, e.g., scheduling parameters o Address space state  Location of text, data, stack regions  MMU virtual memory state, i.e., virtual -> physical mapping o Device related state  Open files, network connections o What other state must the OS keep? 15

Summary  A running program is called a process  OS maintains per-process state for each abstraction it provides to a process o Thread state (virtualizes CPU)  Enables running multiple threads/streams of execution concurrently o Address space state (virtualizes memory)  Enables running multiple programs with “private” memory o Device related state  Includes file state, socket state, etc. 16

Think Time  What is the difference between a program and a process?  What is the difference between a thread and a process?  What is the difference between an address space and a process?  Is the OS code run in a separate process? a separate thread? Does it need a process structure?  What is the address space of an OS? 17