Introduction to Threads

Similar documents
CSC 1600: Chapter 6. Synchronizing Threads. Semaphores " Review: Multi-Threaded Processes"

Synchronization: Basics

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control

Carnegie Mellon Concurrency and Synchronization

Synchronization: Basics

Process Synchroniza/on

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

The course that gives CMU its Zip! Concurrency I: Threads April 10, 2001

Programming with Threads Dec 7, 2009"

Today. Threads review Sharing Mutual exclusion Semaphores

CS 3305 Intro to Threads. Lecture 6

Threads. studykorner.org

Carnegie Mellon. Synchroniza+on : Introduc+on to Computer Systems Recita+on 14: November 25, Pra+k Shah (pcshah) Sec+on C

Outline. CS4254 Computer Network Architecture and Programming. Introduction 2/4. Introduction 1/4. Dr. Ayman A. Abdel-Hamid.

CS 261 Fall Mike Lam, Professor. Threads

Carnegie Mellon Performance of Mul.threaded Code

Thread and Synchronization

Shared Variables in Threaded C Programs. Systemprogrammering Föreläsning 11 Programming with Threads

CS510 Operating System Foundations. Jonathan Walpole

for (i = 0; i < 2; i++)

! What is the memory model for threads? ! How are variables mapped to memory instances? ! How many threads reference each of these instances?

Synchronization: Advanced

pthreads CS449 Fall 2017

Lecture 4. Threads vs. Processes. fork() Threads. Pthreads. Threads in C. Thread Programming January 21, 2005

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Threads. Threads (continued)

Pthread (9A) Pthread

Thread. Disclaimer: some slides are adopted from the book authors slides with permission 1

Threads. CS 475, Spring 2018 Concurrent & Distributed Systems

CS333 Intro to Operating Systems. Jonathan Walpole

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

POSIX threads CS 241. February 17, Copyright University of Illinois CS 241 Staff

Synchronization April 29, 2008

EPL372 Lab Exercise 2: Threads and pthreads. Εργαστήριο 2. Πέτρος Παναγή

CS370 Operating Systems

CS510 Operating System Foundations. Jonathan Walpole

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

CSE Opera,ng System Principles

CS370 Operating Systems

CS370 Operating Systems

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective?

Lecture Contents. 1. Overview. 2. Multithreading Models 3. Examples of Thread Libraries 4. Summary

Concurrent Programming

Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits

Intro to POSIX Threads with FLTK

Concurrent Programming

Computer Systems Laboratory Sungkyunkwan University

pthreads Announcement Reminder: SMP1 due today Reminder: Please keep up with the reading assignments (see class webpage)

CSE 306/506 Operating Systems Threads. YoungMin Kwon

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas

Chapter 10. Threads. OS Processes: Control and Resources. A process as managed by the operating system groups together:

Reminder from last <me

Overview. Administrative. * HW 2 Grades. * HW 3 Due. Topics: * What are Threads? * Motivating Example : Async. Read() * POSIX Threads

Concurrency, Thread. Dongkun Shin, SKKU

CSE Opera,ng System Principles

Synchroniza+on II COMS W4118

User Space Multithreading. Computer Science, University of Warwick

CPSC 341 OS & Networks. Threads. Dr. Yingwu Zhu

Last class: Today: Thread Background. Thread Systems

ANSI/IEEE POSIX Standard Thread management

Pthreads. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CSE 333 SECTION 9. Threads

Threading Language and Support. CS528 Multithreading: Programming with Threads. Programming with Threads

CS370 Opera;ng Systems Midterm Review. Yashwant K Malaiya Spring 2018

CMPSC 311- Introduction to Systems Programming Module: Concurrency

High Performance Computing Lecture 21. Matthew Jacob Indian Institute of Science

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CS345 Opera,ng Systems. Φροντιστήριο Άσκησης 2

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

A Thread is an independent stream of instructions that can be schedule to run as such by the OS. Think of a thread as a procedure that runs

Concurrent Programming

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Preview. What are Pthreads? The Thread ID. The Thread ID. The thread Creation. The thread Creation 10/25/2017

Threads. Jo, Heeseung

Introduction to pthreads

Concurrent Server Design Multiple- vs. Single-Thread

Introduction to PThreads and Basic Synchronization

Threads. Today. Next time. Why threads? Thread model & implementation. CPU Scheduling

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Threads. To do. Why threads? Thread model & implementation. q q q. q Next time: Synchronization

Concurrent programming

Review: Easy Piece 1

Threads. Jo, Heeseung

THREADS. Jo, Heeseung

Total Score is updated. The score of PA4 will be changed! Please check it. Will be changed

Lecture 4 Threads. (chapter 4)

CS533 Concepts of Operating Systems. Jonathan Walpole

CS 153 Lab4 and 5. Kishore Kumar Pusukuri. Kishore Kumar Pusukuri CS 153 Lab4 and 5

Concurrent Programming

Parallel Programming Languages COMP360

Threads. COMP 401 Fall 2017 Lecture 22

Multithreading Programming II

Midterm Review. CSE 120: Principles of Opera=ng Systems. UC San Diego: Summer Session I, 2009 Frank Uyeda

Approaches to Concurrency

Multithreaded Programming

CS 450 Operating System Week 4 Lecture Notes

CS370 Operating Systems

What is concurrency? Concurrency. What is parallelism? concurrency vs parallelism. Concurrency: (the illusion of) happening at the same time.

Proxy: Concurrency & Caches

Concurrency. Johan Montelius KTH

Transcription:

Computer Systems Introduction to Threads Race Conditions Single- vs. Multi-Threaded Processes Process Process Thread Thread Thread Thread Memory Memory Heap Stack Heap Stack Stack Stack Data Data Code Code single- threaded mul)- threaded 1

What are Threads? q A thread is an independent stream of instruc3ons - Basic unit of CPU u3liza3on q A thread has its own - thread ID - execu3on stack q A thread shares with its sibling threads - The code, data and heap sec3on - Other OS resources, such as open files and signals Multi-Threaded Processes q Each thread has a private stack q But threads share the process address space! q There is no memory protec3on! q Threads could poten3ally write into each other s stack 2

Why use Threads? q A specific example, a Web server: in an infinite loop { get web page request from client check if page exists and client has permissions create a thread to transmit web page back to client } Concurrent execu3on on a single- core system Parallel execu3on on a dual- core system 3

pthreads q Refers to the POSIX standard (IEEE 1003.1c) q API for thread crea3on and synchroniza3on q Common in UNIX opera3ng systems Java Threads q Java threads may be created by: - Extending Thread class - Implemen3ng the Runnable interface q JVM manages Java threads - Crea3on - Execu3on - Etc. 4

BIG Issue: Shared Data, which leads to Race Conditions An Example: badcnt.c #define NITERS 1000000 unsigned int cnt = 0; /* shared */ int main() { pthread_t tid1, tid2; pthread_create(&tid1, NULL, Count, NULL); pthread_create(&tid2, NULL, Count, NULL); } pthread_join(tid1, NULL); pthread_join(tid2, NULL); if (cnt!= (unsigned)niters*2) printf("boom! cnt=%d\n", cnt); else printf("ok cnt=%d\n", cnt); /* thread routine */ void * Count(void *arg) { int i; for (i=0; i<niters; i++) cnt++; return NULL; } linux>./badcnt BOOM! cnt=1988411 linux>./badcnt BOOM! cnt=1982618 linux>./badcnt BOOM! cnt=1982696 cnt should be equal to 2,000,000. What went wrong?! 5

q Cri3cal sec3on = block of code that accesses shared data q The statement cnt++ is non- atomic: at machine level, it translates into three machine instruc3ons mov R, cnt add R, 1 mov cnt, R Critical Sections /* thread routine */ void * count(void *arg) { int i; for (i=0; i<niters; i++) cnt++; /* global, shared */ return NULL; } ; atomic (no interrupts) ; atomic (no interrupts) ; atomic (no interrupts) Race Condition q First thread executes cnt++ implemented as register1 = cnt register1 = register1 + 1 cnt = register1 q Second thread executes cnt++ implemented as register2 = cnt register2 = register2 + 1 cnt = register2 q Consider this execu3on interleaving with cnt = 5 ini3ally: first thread executes register1 = cnt {register1 = 5} first thread executes register1 = register1 + 1 {register1 = 6} Timer Interrupt second thread executes register2 = cnt {register2 = 5} second thread executes register2 = register2 + 1 {register2 = 6} Timer Interrupt first thread executes cnt = register1 {cnt = 6 } second thread executes cnt = register2 {cnt = 6} 6

Race Conditions q Race Condi3ons - occur when several threads access shared data concurrently - the result may vary from one execu)on to the next q We d like to execute this instruc3on atomically: cnt++; machine level R = cnt R = R + 1 cnt = R (An atomic opera3on completes without interrup3on) q Need some kind of mutual exclusion primi3ves - only a thread should ever enter a cri3cal sec3on (next lecture) Practice Exercise q Consider two threads sharing a global variable count, with ini3al value equal to 10: Thread A count++; Thread B count--; q What are the possible values for count ader both threads finish execu3ng: 7

Hands-on Session q Complete the hands- on POSIX programming exercises posted on the class website - Crea3ng threads - Passing data to threads - pthread_create - pthread_exit - pthread_self - pthread_join - pthread_attach - pthread_detach 8