TCSS 422: OPERATING SYSTEMS

Similar documents
TCSS 422: OPERATING SYSTEMS

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

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

Interlude: Thread API

POSIX Threads. HUJI Spring 2011

Concurrency, Thread. Dongkun Shin, SKKU

CS510 Operating System Foundations. Jonathan Walpole

Operating System Labs. Yuanbin Wu

Introduction to PThreads and Basic Synchronization

Threads. Threads (continued)

CS533 Concepts of Operating Systems. Jonathan Walpole

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

CS 3305 Intro to Threads. Lecture 6

CS333 Intro to Operating Systems. Jonathan Walpole

POSIX Threads. Paolo Burgio

CSCI4430 Data Communication and Computer Networks. Pthread Programming. ZHANG, Mi Jan. 26, 2017

TCSS 422: OPERATING SYSTEMS

POSIX PTHREADS PROGRAMMING

pthreads CS449 Fall 2017

Pre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012

real time operating systems course

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

CSE 333 SECTION 9. Threads

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

[537] Locks and Condition Variables. Tyler Harter

Introduction to pthreads

Interlude: Thread API

Chapter 4 Concurrent Programming

CS-345 Operating Systems. Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization

Process Synchronization

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

Agenda. Process vs Thread. ! POSIX Threads Programming. Picture source:

C Grundlagen - Threads

CS510 Operating System Foundations. Jonathan Walpole

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

PThreads in a Nutshell

Locks and semaphores. Johan Montelius KTH

Threads need to synchronize their activities to effectively interact. This includes:

COSC 6374 Parallel Computation. Shared memory programming with POSIX Threads. Edgar Gabriel. Fall References

Threads. Jo, Heeseung

COMP 3430 Robert Guderian

Data Races and Deadlocks! (or The Dangers of Threading) CS449 Fall 2017

Condition Variables. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Locks and semaphores. Johan Montelius KTH

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

THREADS. Jo, Heeseung

Concurrency and Synchronization. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

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

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

Pthreads. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

recap, what s the problem Locks and semaphores Total Store Order Peterson s algorithm Johan Montelius 0 0 a = 1 b = 1 read b read a

Condition Variables & Semaphores

Multithreading Programming II

LSN 13 Linux Concurrency Mechanisms

Introduction to parallel computing

Solving the Producer Consumer Problem with PThreads

Synchronising Threads

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

ANSI/IEEE POSIX Standard Thread management

Synchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1

Tutorial 1 C Tutorial: Pointers, Strings, Exec

CS 261 Fall Mike Lam, Professor. Threads

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

Threaded Programming. Lecture 9: Alternatives to OpenMP

Multicore and Multiprocessor Systems: Part I

Introduction to Threads

CS 105, Spring 2015 Ring Buffer

Posix Threads (Pthreads)

Thread and Synchronization

Concurrent Server Design Multiple- vs. Single-Thread

CS 345 Operating Systems. Tutorial 2: Treasure Room Simulation Threads, Shared Memory, Synchronization

ENCM 501 Winter 2019 Assignment 9

Threads. Jo, Heeseung

High Performance Computing Course Notes Shared Memory Parallel Programming

Concurrency. Johan Montelius KTH

Unix. Threads & Concurrency. Erick Fredj Computer Science The Jerusalem College of Technology

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

CSE 333 Section 9 - pthreads

Condition Variables. Dongkun Shin, SKKU

Synchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1

Synchronization Primitives

Condition Variables. Dongkun Shin, SKKU

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

Real Time Operating Systems and Middleware

Threads. lightweight processes

Pthreads (2) Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

CMSC421: Principles of Operating Systems

Threads Tuesday, September 28, :37 AM

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

Shared-Memory Programming

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

Motivation and definitions Processes Threads Synchronization constructs Speedup issues

Thread Posix: Condition Variables Algoritmi, Strutture Dati e Calcolo Parallelo. Daniele Loiacono

CS 105, Spring 2007 Ring Buffer

CS Lecture 3! Threads! George Mason University! Spring 2010!

Threads. studykorner.org

Concurrent Programming

Computer Systems Laboratory Sungkyunkwan University

CSci 4061 Introduction to Operating Systems. (Threads-POSIX)

Transcription:

TCSS 422: OPERATING SYSTEMS OBJECTIVES Introduction to threads Concurrency: An Introduction Wes J. Lloyd Institute of Technology University of Washington - Tacoma Race condition Critical section Thread API L6.2 THREADS THREADS - 2 Enables a single process (program) to have multiple workers Supports independent path(s) of execution within a program Single Threaded Process SHARED Multiple Threaded Process Each thread has its own Thread Control Block (TCB) PC, registers, SP, and stack Code segment, memory, and heap are shared Alfred Park, http://randu.org/tutorials/threads L6.3 L6.4 PROCESS AND THREAD METADATA SHARED ADDRESS SPACE Thread Control Block vs. Process Control Block Every thread has it s own stack / PC L6.5 L6.6 Slides by Wes J. Lloyd L6.1

THREAD CREATION EXAMPLE POSSIBLE ORDERINGS OF EVENTS Starts running intmain() Thread 1 Thread 2 Prints main: begin Creates Thread 1 Creates Thread 2 Waits for T1 Prints A Waits for T2 Prints B Prints main: end L6.7 L6.8 POSSIBLE ORDERINGS OF EVENTS - 2 POSSIBLE ORDERINGS OF EVENTS - 3 intmain() Thread 1 Thread 2 intmain() Thread 1 Thread 2 Starts running Starts running Prints main: begin Prints main: begin Creates Thread 1 Creates Thread 1 Creates Thread 2 Creates Thread 2 Prints A Waits for T1 What if execution order of Prints B events in the program matters? Prints B Prints A Waits for T1 immediately Waits for T2 immediately Waits for T2 Immediately returns Prints main: end Prints main: end L6.9 L6.10 COUNTER EXAMPLE RACE CONDITION Show example A + B : ordering Counter: incrementing global variable by two threads Example when counter=50 Counter = counter + 1 If synchronized, counter will = 52 L6.11 L6.12 Slides by Wes J. Lloyd L6.2

CRITICAL SECTION LOCKS Code that accesses a shared variable must not be concurrently executed by more than one thread Multiple active threads inside a critical section produces a race condition. Atomicity of execution must be ensured in critical sections These sections must be mutually exclusive To demonstrate how critical section(s) can be executed atomically Chapter 27 & beyond introduce locks Counter example revisited L6.13 L6.14 THREAD CREATION pthread_create LINUX THREAD API thread: thread struct attr: stack size, scheduling priority start_routine: function pointer to thread routine arg: argument to pass to thread routine L6.15 L6.16 THREAD CREATION - 2 ADDING CASTS Casting Suppresses compiler warnings when passing typed data where (void) or (void *) is called for Example: uncasted capture in pthread_join pthread_int.c: : In function main : pthread_int.c:34:20: warning: passing argument 2 of pthread_join pthread_join from incompatible pointer type [-Wincompatible [ Wincompatible-pointer pointer-types] types] pthread_join(p1, &p1val); Example: uncasted return In file included from pthread_int.c:3:0: /usr usr/include/pthread.h:250:12: note: expected void ** but argument is of type int ** extern int pthread_join (pthread_t th th,, void ** thread_return thread_return); pthread_join int * p1val; int * p2val; pthread_join(p1, (void *)&p1val); pthread_join(p2, (void *)&p2val); return from thread function int * counterval = malloc(sizeof(int)); *counterval = counter; return (void *) counterval; L6.17 L6.18 Slides by Wes J. Lloyd L6.3

PTHREAD_CREATE PASS ANY DATA WAITING FOR THREADS TO FINISH thread: which thread? value_ptr: pointer to return value type is dynamic / agnostic Returned values *must* be on the heap Thread stacks destroyed upon thread termination (join) Pointers to thread stack memory addresses are invalid May appear as gibberish or lead to crash (segfault) Not all threads join What would be Examples?? L6.19 L6.20 struct myarg int a; int b; ; What will this code do? struct myarg int a; int b; ; How about this code? void *worker(void *arg) struct myarg *input = (struct myarg *) arg; printf("a=%d b=%d\n",input->a, input->b); struct myarg output; output.a = 1; output.b = 2; return (void *) &output; Data on thread stack int main (int argc, char * argv[]) pthread_t p1; struct myarg args; struct myarg *ret_args; args.a = 10; args.b = 20; pthread_create(&p1, NULL, worker, &args); pthread_join(p1, (void *)&ret_args); How can this code be fixed? printf("returned %d %d\n", ret_args->a, ret_args->b); return 0; $./pthread_struct a=10 b=20 Segmentation fault (core dumped) void *worker(void *arg) struct myarg *input = (struct myarg *) arg; printf("a=%d b=%d\n",input->a, input->b); input->a = 1; input->b = 2; return (void *) &input; int main (int argc, char * argv[]) pthread_t p1; struct myarg args; struct myarg *ret_args; args.a = 10; args.b = 20; pthread_create(&p1, NULL, worker, &args); pthread_join(p1, (void *)&ret_args); printf("returned %d %d\n", ret_args->a, ret_args->b); return 0; $./pthread_struct a=10 b=20 returned 1 2 L6.21 L6.22 PASSING A SINGLE VALUE LOCKS Here we cast the pointer to pass/return a primitive data type Using this approach on your CentOS 7 VM How large (in bytes) can the primitive data type be? How large (in bytes) can the primitive data type be on a 32-bit operating system? pthread_mutex_t data type /usr/include/bits/pthread_types.h // Global Address Space static volatile int counter = 0; pthread_mutex_t lock; void *worker(void *arg) int i; for (i=0;i<10000000;i++) int rc = pthread_mutex_lock(&lock); assert(rc==0); counter = counter + 1; pthread_mutex_unlock(&lock); return NULL; L6.23 L6.24 Slides by Wes J. Lloyd L6.4

LOCKS - 2 LOCK INITIALIZATION Ensure critical sections are executed atomically Provides implementation of Mutual Exclusion Assigning the constant API API call: Example w/o initialization & error checking Initializes mutex with attributes specified by 2 nd argument If NULL, then default attributes are used Blocks forever until lock can be obtained Enters critical section once lock is obtained Releases lock Upon initialization, the mutex is initialized and unlocked L6.25 L6.26 LOCKS - 3 CONDITIONS AND SIGNALS Error checking wrapper Condition variables support signaling between threads What if lock can t be obtained? pthread_cont_t datatype trylock returns immediately (fails) if lock is unavailable timelock tries to obtain a lock for a specified duration pthread_cond_wait() Waits (sleeps) Listens for a signal Releases the lock until signaled L6.27 L6.28 CONDITIONS AND SIGNALS - 2 CONDITIONS AND SIGNALS - 3 int pthread_cond_signal(pthread_cond_t * cond); int pthread_cond_broadcast(pthread_cond_t * cond); pthread_cond_signal() Called to send a signal to all listeners to wake them up The goal is to unblock (at least one) to respond to the signal pthread_cond_broadcast() Unblocks all threads currently blocked on the specified condition Used when all threads should respond to the signal Which thread is unblocked first? Determined by OS scheduler (based on priority) Thread(s) gain the lock individually (based on priority) as if they called pthread_mutex_lock() Wait example... wait puts thread to sleep, releases lock when awoken, lock reacquired (and released by this code) Another thread signals the thread Code performs required work before other thread(s) can continue L6.29 L6.30 Slides by Wes J. Lloyd L6.5

CONDITION AND SIGNALS - 4 DO-IT-YOURSELF LOCK initialized -- global variable shared by multiple threads Wait (client thread): Why do we wait inside a while loop? The while ensures upon awakening the condition is rechecked A signal may have been raised, but the condition to proceed has not been satisfied. Without checking the condition the thread may proceed to execute when it should not. Signal (parent thread): when ready How is this wait different that pthread_cond_wait()? Wastes CPU cycles effectively pegs a core at 100% Potential synchronization errors with changing the value of initialized Thread API is provided to advance the DO-IT-YOURSELF approach L6.31 L6.32 PTHREADS LIBRARY SAMPLE MAKEFILE Compilation gcc pthread pthread.c o pthread Requires explicitly linking the library with compiler flag List of pthread manpages man k pthread CC=gcc CFLAGS=-pthread -I. -Wall binaries=pthread pthread_int pthread_lock_cond pthread_struct all: $(binaries) pthread_mult: pthread.c pthread_int.c $(CC) $(CFLAGS) $^ -o $@ clean: $(RM) -f $(binaries) *.o Example builds multiple single file programs All target pthread_mult Example if multiple source files should produce a single executable clean target L6.33 L6.34 QUESTIONS L6.35 Slides by Wes J. Lloyd L6.6