CS510 Operating System Foundations

Slides:



Advertisements
Similar presentations
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 pThreads.
Advertisements

Threads. Readings r Silberschatz et al : Chapter 4.
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.
1 CS 333 Introduction to Operating Systems Class 3 – Threads & Concurrency Jonathan Walpole Computer Science Portland State University.
CS533 - Concepts of Operating Systems 1 Anyone NOT on this list see me after class! Bock, Tony Carroll, Diana Kulkarni, Ashwini LeVitre, Jon Mukherjee,
CS533 Concepts of Operating Systems Class 1
Jonathan Walpole Computer Science Portland State University
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 4: Threads.
CS533 Concepts of Operating Systems Jonathan Walpole.
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
CS333 Intro to Operating Systems Jonathan Walpole.
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.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
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.
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.
CS510 Concurrent Systems Jonathan Walpole. Introduction to 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:
1 Introduction to Threads Race Conditions. 2 Process Address Space Revisited Code Data OS Stack (a)Process with Single Thread (b) Process with Two 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
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Processes and threads.
Realizing Concurrency using the thread model
Threads in C Caryl Rahn.
Boost String API & Threads
CS399 New Beginnings Jonathan Walpole.
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Chapter 4: Threads.
PTHREADS These notes are from LLNL Pthreads Tutorial
Multithreading Tutorial
Threads and Cooperation
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.
PTHREADS AND SEMAPHORES
Multithreading Tutorial
Realizing Concurrency using the thread model
Jonathan Walpole Computer Science Portland State University
Pthread Prof. Ikjun Yeom TA – Mugyo
Operating System Concepts
Multithreading Tutorial
Programming with Shared Memory
Jonathan Walpole Computer Science Portland State University
Multithreading Tutorial
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Programming with Shared Memory
Realizing Concurrency using Posix Threads (pthreads)
CS510 Operating System Foundations
Tutorial 4.
Jonathan Walpole Computer Science Portland State University
Shared Memory Programming with Pthreads
Presentation transcript:

CS510 Operating System Foundations Jonathan Walpole

Threads & Concurrency

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

A word processor with three threads Typical Thread Usage A word processor with three threads

Processes vs Threads HTTPD GET / HTTP/1.0 disk

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

Processes vs Threads HTTPD HTTPD GET / HTTP/1.0 disk

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

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

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

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

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

Using Create, Join and Exit

An Example Pthreads Program #include <pthread.h> #include <stdio.h> #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); 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: http://www.llnl.gov/computing/tutorials/pthreads

Pros & Cons of Threads Pros: Cons: - 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 Context switching between thread is cheaper… ASK WHY? Because you don’t have O.S. to change.

Concurrency

Sequential Programming Sequential programming with processes - Private memory a program data stack heap - CPU context program counter stack pointer - registers

Sequential Programming Example int i = 0 i = i + 1 print i What output do you expect? Why?

Concurrent Programming Concurrent programming with threads - Shared memory a program data heap - Private stack for each thread - Private CPU context for each thread program counter stack pointer registers

Concurrent Threads Example int i = 0 Thread 1: i = i + 1 print i What output do you expect with 1 thread? Why?

Concurrent Threads Example int i = 0 Thread 1: i = i + 1 Thread 2: i = i + 1 print i print i What output do you expect with 2 threads? Why?

Race Conditions How is i = i + 1 implemented? load i to register increment register store register value to i Registers are part of each thread’s private CPU context

Race Conditions Thread 1 Thread 2 load i to regn load i to regn inc regn store regn to i Thread 2 load i to regn inc regn store regn to i

Critical Sections What is dangerous in the previous example? How should we reason about this kind of code? What property did we have in sequential programming, which we lost in concurrent programming? Why was that property important?

Memory Invariance Sequential programs have the property that memory values do not change unless the control flow changes them. Hence, we can reason about the effects of a control flow. How can we regain this memory invariance property in concurrent programs?

Mutual Exclusion

Mutual Exclusion But how can we implement it?