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

Similar documents
Concurrent Server Design Multiple- vs. Single-Thread

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

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

Threads. Jo, Heeseung

POSIX Threads. Paolo Burgio

ANSI/IEEE POSIX Standard Thread management

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

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

Pthreads. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Lecture 19: Shared Memory & Synchronization

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

real time operating systems course

Threads. lightweight processes

Introduction to PThreads and Basic Synchronization

Threads. Jo, Heeseung

pthreads CS449 Fall 2017

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

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

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

THREADS. Jo, Heeseung

Threads. Threads (continued)

POSIX Threads. HUJI Spring 2011

CSE 333 SECTION 9. Threads

LSN 13 Linux Concurrency Mechanisms

High Performance Computing Course Notes Shared Memory Parallel Programming

CS 3305 Intro to Threads. Lecture 6

Pthread (9A) Pthread

User Space Multithreading. Computer Science, University of Warwick

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

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

Chapter 4 Threads. Images from Silberschatz 03/12/18. CS460 Pacific University 1

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

Threads. studykorner.org

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

PThreads in a Nutshell

POSIX PTHREADS PROGRAMMING

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

TCSS 422: OPERATING SYSTEMS

Network Programming TDC 561

Introduction to Threads

Thread and Synchronization

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

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

Posix Threads (Pthreads)

Computer Systems Laboratory Sungkyunkwan University

Threads (light weight processes) Chester Rebeiro IIT Madras

Multithreading Programming II

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

Operating systems fundamentals - B06

POSIX Threads: a first step toward parallel programming. George Bosilca

CMPSC 311- Introduction to Systems Programming Module: Concurrency

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

CMPSC 311- Introduction to Systems Programming Module: Concurrency

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

Operating System Labs. Yuanbin Wu

Parallel Programming with Threads

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

Operating Systems. Threads and Signals. Amir Ghavam Winter Winter Amir Ghavam

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas

Chapter 4 Concurrent Programming

CMPSC 311- Introduction to Systems Programming Module: Concurrency

ENCM 501 Winter 2019 Assignment 9

Shared Memory Programming Models III

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

Multi-threaded Programming

CS370 Operating Systems

Programming with Shared Memory. Nguyễn Quang Hùng

Introduction to Threads

Multithreaded Programming

CSE 306/506 Operating Systems Threads. YoungMin Kwon

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

Exercise (could be a quiz) Solution. Concurrent Programming. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - IV Threads

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

CS510 Operating System Foundations. Jonathan Walpole

Pthreads: POSIX Threads

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

Introduction to pthreads

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

Concurrency, Thread. Dongkun Shin, SKKU

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

Roadmap. Concurrent Programming. Concurrent Programming. Communication Between Tasks. Motivation. Tevfik Ko!ar

CS370 Operating Systems

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

Concurrency and Threads

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

CSC Systems Programming Fall Lecture - XVIII Concurrent Programming. Tevfik Ko!ar. Louisiana State University. November 11 th, 2008

Multicore and Multiprocessor Systems: Part I

Lecture 9. Remote Procedure Calls Introduction to Multithreaded Programming

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

Concurrency. Johan Montelius KTH

CS333 Intro to Operating Systems. Jonathan Walpole

CPSC 313: Intro to Computer Systems. POSIX Threads. Latency Hiding / Multiprogramming (covered earlier) Ease of Programming (covered now)

Concurrent Programming

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

Concurrent Programming

CS533 Concepts of Operating Systems. Jonathan Walpole

Shared Memory Programming: Threads and OpenMP. Lecture 6

Synchronization Primitives

Computer Systems Laboratory Sungkyunkwan University

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

Transcription:

Threads vs. Processes Lecture 4 Thread Programming January 21, 2005 fork() is expensive (time, memory) Interprocess communication is hard. Threads are lightweight processes: one process can contain several threads of execution. all threads execute the same program (different stages). all threads share instructions, global memory, open files, and signal handlers. each thread has own thread ID, stack, program counter and stack pointer, errno, signal mask. threads can communicate with shared memory. threads have special synchronization mechanisms. fork() Threads fork() Process B Thread 1 pthread_create() Thread 2 Threads in C POSIX threads (pthreads): standard for Unix OS must support it (Linux) Programs must be linked with -lpthread Creating a thread: int pthread_create(pthread_t *tid, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg); tid: thread id attr: options start_routine: function to be executed arg: parameter to thread 1

Stopping a pthread: a thread stops when the process stops, the parent thread stops, its start_routine function return, or it calls pthread_exit: void pthread_exit(void *retval); Threads must be waited for: int pthread_join(pthread_t tid, void **status); Example void *func(void *param) { int *p = (int *) param; printf( This is a new thread (%d)\n, *p); return NULL; int main () { pthread_t id; int x = 100; pthread_create(&id, NULL, func, (void *) &x); pthread_join(id, NULL); A thread can be joinable or detached. Detached: on termination all thread resources are released, does not stop when parent thread stops, does not need to be pthread_join()ed. Default: joinable (attached), on termination thread ID and exit status are saved by OS. Creating a detached thread: pthread_t id; pthread_attr_t attr; A thread can join another: int pthread_join (pthread_t tid, void ** status); Call waits until specified thread exits. pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); pthread_create(&id, &attr, func, NULL); pthread_detach() 2

int counter = 0; void *thread_code (void *arg) { counter++; printf( Thread %u is number %d\n, pthread_self(), counter); main () { int i; pthread_t tid; for (i = 0; i < 10; i++) pthread_create(&tid, NULL, thread_code, NULL); Pthread Mutual exclusion: pthread_mutex_t counter_mtx = PTHREAD_MUTEX_INITIALIZER; Locking (blocking call): pthread_mutex_lock(pthread_mutex_t *mutex); Unlocking: pthread_mutex_unlock(pthread_mutex_t *mutex); Quiz A server creates a thread for each client. No more than n threads can be active (or n clients can be serviced). How can we let the main thread know that a thread terminated and that it can service a new client? What About pthread_join? pthread_join() is kinda like wait(). Requires thread id, so we can wait for thread xy, but not for the next thread. What About? Thread startup: acquire lock on the variable increment variable release lock Thread termination: acquire lock on the variable decrement variable release lock What About the Main Loop? active_threads = 0; // start up first n threads for first n clients // make sure they are running while (1) { // have to lock/release active_threads; if (active_threads < n) // start up thread for next client busy_waiting(is_bad); 3

Allow one thread to wait/sleep for event generated by another thread. Allows us to avoid busy waiting. pthread_cond_t foo = PTHREAD_COND_INITIALIZER; Condition variable is ALWAYS used with a mutex. pthread_cond_wait(pthread_cond_t *cptr, pthread_mutex_t *mptr); pthread_cond_signal(pthread_cond_t *cptr); Problem Revisited Each thread decrements active_threads when terminating and calls pthread_cond_signal() to wake up main loop. The main thread increments active_threads when a thread is started and waits for changes by calling pthread_cond_wait. All changes to active_threads must be within a mutex. If two threads exit simultaneously, the second one must wait until the first one is recognized by the main loop. Condition signals are NOT lost. int active_threads = 0; pthread_mutex_t at_mutex; pthread_cond_t at_cond; void *handler_fct(void *arg) { // handle client pthread_mutex_lock(&at_mutex); active_threads--; pthread_cond_signal(&at_cond); pthread_mutex_unlock(&at_mutex); return(); active_threads = 0; while (1) { pthread_mutex_lock(&at_mutex); while (active_threads < n) { active_threads++; pthread_start(...); pthread_cond_wait(&at_cond, &at_mutex); pthread_mutex_unlock(&at_mutex); Multiple waiting threads: signal wakes up exactly one, but not specified which one. pthread_cond_wait atomically unlocks mutex. When handling signal, pthread_cond_wait atomically re-acquires mutex. Avoids race conditions: a signal cannot be sent between the time a thread unlocks a mutex and begins to wait for a signal. 4

Thread Safeness Some Unix primitives and library functions have internal race conditions and were not designed with threads in mind! Check on the man pages if library functions are thread-safe. Example: gethostbyname is not thread-safe, use it within a mutex protection. Java thread is a class which inherits from Thread, you must overload its run() method: public class MyThread extends Thread { private int argument; MyThread(int arg) { argument = arg; public void run() { System.out.println( New thread created! Arg= + argument); Starting a thread: MyThread t = new MyThread(42); t.start(); Stopping a thread: when run() returns, no join needed for thread to finish, but you can: MyThread t = new MyThread(42); t.start();... t.join(); Monitor (similar to mutex): public class AnotherClass { synchronized public void methodone() {... synchronized public void methodtwo() {... public void methodthree() {... Each object contains one mutex which is locked when entering a synchronized method and unlocked when leaving. Two synchronized methods from same object cannot be executing simultaneously. Two different objects from the same class can be executing the same synchronized method simult. 5