CS333 Intro to Operating Systems Jonathan Walpole.

Slides:



Advertisements
Similar presentations
Threads. Readings r Silberschatz et al : Chapter 4.
Advertisements

Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
Lecture 4: Concurrency and Threads CS 170 T Yang, 2015 Chapter 4 of AD textbook.
1 OMSE 510: Computing Foundations 6: Multithreading Chris Gilmore Portland State University/OMSE Material Borrowed from Jon Walpole’s lectures.
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 TY, Sept 2011.
1 CS 333 Introduction to Operating Systems Class 3 – Threads & Concurrency Jonathan Walpole Computer Science Portland State University.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
CS533 Concepts of Operating Systems Class 1
Threads CSCI 444/544 Operating Systems Fall 2008.
Jonathan Walpole Computer Science Portland State University
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
Inter-Process Communication  most OSs provide several abstractions for inter- process communication: message passing, shared memory, etc.  communication.
Process Concept An operating system executes a variety of programs
CS 3013 & CS 502 Summer 2006 Threads1 CS-3013 & CS-502 Summer 2006.
Processes, Threads, SMP, and Microkernels
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 4: Threads.
Thread. A basic unit of CPU utilization. It comprises a thread ID, a program counter, a register set, and a stack. It is a single sequential flow of control.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
Multithreading Allows application to split itself into multiple “threads” of execution (“threads of execution”). OS support for creating threads, terminating.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 T Yang, Sept 2012.
Threads, Thread management & Resource Management.
CS533 Concepts of Operating Systems Jonathan Walpole.
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
B. RAMAMURTHY 10/24/ Realizing Concurrency using the thread model.
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
Chapter 4: Threads. 2 Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
CS533 - Concepts of Operating Systems 1 Anyone NOT on this list see me after class! Arryadi, Rizal Carlson, Kristen Ellet, Burke Florey, David Greenwald,
Lecture 7: POSIX Threads - Pthreads. Parallel Programming Models Parallel Programming Models: Data parallelism / Task parallelism Explicit parallelism.
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.
Department of Computer Science and Software Engineering
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Processes & Threads Introduction to Operating Systems: Module 5.
CS307 Operating Systems Threads Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2011.
CSC 360, Instructor: Kui Wu Thread & PThread. CSC 360, Instructor: Kui Wu Agenda 1.What is thread? 2.User vs kernel threads 3.Thread models 4.Thread issues.
Threads. Readings r Silberschatz et al : Chapter 4.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Threads A thread is an alternative model of program execution
CS533 Concepts of Operating Systems Jonathan Walpole.
POSIX Threads Loren Stroup EEL 6897 Software Development for R-T Engineering Systems.
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
CS533 Concepts of Operating Systems Class 2 Overview of Threads and Concurrency.
2.2 Threads  Process: address space + code execution  There is no law that states that a process cannot have more than one “line” of execution.  Threads:
Thread Programming 김 도 형김 도 형. 2 Table of Contents  What is a Threads?  Designing Threaded Programs  Synchronizing Threads  Managing Threads.
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Tutorial 4. In this tutorial session we’ll see Threads.
A thread is a basic unit of CPU utilization within a process Each thread has its own – thread ID – program counter – register set – stack It shares the.
Realizing Concurrency using the thread model
CS399 New Beginnings Jonathan Walpole.
Thread Programming.
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems Lecture 13.
Jonathan Walpole Computer Science Portland State University
Realizing Concurrency using the thread model
Operating Systems Lecture 14.
CS510 Operating System Foundations
Jonathan Walpole Computer Science Portland State University
Operating System Concepts
Jonathan Walpole Computer Science Portland State University
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
CS510 Operating System Foundations
Tutorial 4.
Threads CSE 2431: Introduction to Operating Systems
Presentation transcript:

CS333 Intro to Operating Systems Jonathan Walpole

2 Threads & Concurrency

3 Threads Processes have the following components: - an address space - a collection of operating system state -a CPU context … or thread of control To use multiple CPUs on a multiprocessor system, a process would need several CPU contexts - Thread fork creates new thread not memory space - Multiple threads of control could run in the same memory space on a single CPU system too!

4 Threads Threads share a process address space with zero or more other threads Threads have their own CPU context -PC, SP, register state, -Stack A traditional process could be viewed as a memory address space with a single thread

5 Single Thread in Address Space

6 Multiple Threads in Address Space

7 What Is a Thread? A thread executes a stream of instructions -it is an abstraction for control-flow Practically, it is a processor context and stack - Allocated a CPU by a scheduler -Executes in a memory address space

8 Private Per-Thread State Things that define the state of a particular flow of control in an executing program -Stack (local variables) -Stack pointer -Registers -Scheduling properties (i.e., priority)

9 Shared State Among Threads Things that relate to an instance of an executing program -User ID, group ID, process ID -Address space: Text, Data (off-stack global variables), Heap (dynamic data) -Open files, sockets, locks Important: Changes made to shared state by one thread will be visible to the others! Reading and writing memory locations requires synchronization! … a major topic for later …

Concurrent Access to Shared State Important: Changes made to shared state by one thread will be visible to the others! Reading and writing memory locations requires synchronization! This is a major topic for later …

11 Programming With Threads Split program into routines to execute in parallel -True or pseudo (interleaved) parallelism Alternative strategies for executing multiple rountines

12 Why Use Threads? Utilize multiple CPU’s concurrently Low cost communication via shared memory Overlap computation and blocking on a single CPU -Blocking due to I/O -Computation and communication Handle asynchronous events

13 Typical Thread Usage A word processor with three threads

14 Processes vs Threads GET / HTTP/1.0 HTTPD disk

15 Processes vs Threads GET / HTTP/1.0 HTTPD disk Why is this not a good web server design?

16 HTTPD Processes vs Threads GET / HTTP/1.0 HTTPD disk

17 Processes vs Threads GET / HTTP/1.0 HTTPD disk GET / HTTP/1.0

18 Processes vs Threads GET / HTTP/1.0 HTTPD disk GET / HTTP/1.0

19 System Structuring Options Three ways to construct a server

20 Common Thread Strategies Manager/worker -Manager thread handles I/O -Magaer assigns work to worker threads -Worker threads created dynamically -... or allocated from a thread-pool Pipeline -Each thread handles a different stage of an assembly line -Threads hand work off to each other in a producer- consumer relationship

21 Pthreads: A Typical Thread API Pthreads: POSIX standard threads First thread exists in main(), creates the others pthread_create (thread,attr,start_routine,arg) -Returns new thread ID in “thread” -Executes routine specified by “start_routine” with argument specified by “arg” -Exits on return from routine or when told explicitly

22 Pthreads (continued) pthread_exit (status) -Terminates the thread and returns “status” to any joining thread pthread_join (threadid,status) -Blocks the calling thread until thread specified by “threadid” terminates -Return status from pthread_exit is passed in “status” -One way of synchronizing between threads pthread_yield () -Thread gives up the CPU and enters the run queue

23 Using Create, Join and Exit

24 An Example Pthreads Program #include #define NUM_THREADS 5 void *PrintHello(void *threadid) { printf("\n%d: Hello World!\n", threadid); pthread_exit(NULL); } int main (int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int rc, t; for(t=0; t<NUM_THREADS; t++) { printf("Creating thread %d\n", t); rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); if (rc) { printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } pthread_exit(NULL); } Program Output Creating thread 0 Creating thread 1 0: Hello World! 1: Hello World! Creating thread 2 Creating thread 3 2: Hello World! 3: Hello World! Creating thread 4 4: Hello World! For more examples see:

25 Pros & Cons of Threads Pros: -Overlap I/O with computation! -Cheaper context switches -Better mapping to multiprocessors Cons: -Potential thread interactions -Complexity of debugging -Complexity of multi-threaded programming -Backwards compatibility with existing code

26 User-level threads The idea of managing multiple abstract program counters above a single real one can be implemented using privileged or non-privileged code. -Threads can be implemented in the OS or at user level User level thread implementations -Thread scheduler runs as user code (thread library) -Manages thread contexts in user space -The underlying OS sees only a traditional process above

27 Kernel-Level Threads Thread-switching code is in the kernel

28 User-Level Threads Package The thread-switching code is in user space

29 User-level threads Advantages -Cheap context switch costs among threads in the same process! -Calls are procedure calls not system calls! -User-programmable scheduling policy Disadvantages -How to deal with blocking system calls! -How to overlap I/O and computation!

30 Quiz 1. What is the difference between a program and a process? 2. Is the Operating System a program? 3. Is the Operating System a process? -Does it have a process control block? -How is its state managed when it is not running? 4. What is the difference between processes and threads? 5. What tasks are involved in switching the CPU from one process to another? -Why is it called a context switch? 6. What tasks are involved in switching the CPU from one thread to another? -Why are threads “lightweight”?