CMPS 105 Systems Programming. Prof. Darrell Long E2.371

Size: px
Start display at page:

Download "CMPS 105 Systems Programming. Prof. Darrell Long E2.371"

Transcription

1 + CMPS 105 Systems Programming Prof. Darrell Long E2.371

2 + Chapter 12: Thread Control

3 + Thread Limits: Portability

4 + Thread Attributes n Thread attributes control the behavior of threads n Attribute objects n thread attributes n mutex attributes n etc n Thread control functions n Initialization function sets up attributes to default values n Destruction function frees attributes n Get attribute functions n Set attribute functions

5 + Thread Attributes: init and destroy n pthread_attr_init() - initializes attr with all the default thread attributes n int pthread_attr_init(pthread_attr_t* attr); n pthread_attr_destroy() destroys attr n int pthread_attr_destroy(pthread_attr_t* attr);

6 + Thread Attributes: n pthread_attr_setdetachstate() sets the detached state of a thread n int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate); n Two legal values: n PTHREAD_CREATE_DETACHED n PTHREAD_CREATE_JOINABLE n pthread_attr_getdetachstate() gets the current detach-state attribute n int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);

7 + Create a detached thread

8 + Thread Attributes: getstack, setstack n We have finite virtual address space n Each thread gets a stack in that address space n We may want to reduce/increase the default stack size n pthread_attr_setstack() set stack location and size n int pthread_attr_setstack(pthread_attr_t* attr, void* stackaddr, size_t stacksize) n pthread_attr_getstack() get stack location and size n int pthread_attr_getstack(const pthread_attr_t* restrict attr, void** restrict stackaddr, size_t* restrict stacksize)

9 + Thread Attributes: getstacksize, setstacksize n If we want to change the default stack size: n pthread_attr_getstacksize() get the size of the thread s stack n int pthread_attr_getstacksize(const pthread_attr_t *restrict attr, size_t *restrict stacksize); n pthread_attr_setstacksize() set the size of the thread s stack n int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);

10 + Thread Attributes: getguardsize, setguardsize n Memory guards are set up to keep stacks from running into one another, to manipulate the default guard values n pthread_attr_getguardsize() get current guard size n int pthread_attr_getguardsize(const pthread_attr_t* restrict attr, size_t* restrict guardsize); n pthread_attr_setguardsize() set current guard size n int pthread_attr_setguardsize(pthread_attr_t* attr, size_t* restrict guardsize);

11 + Synchronization Attributes n Mutex attributes n Reader-writer lock attributes n Condition variable attributes n Barrier attributes

12 + Mutex Attributes: init and destroy n Represented by pthread-mutexattr_t n Mutex attributes n Process-shared n Robust n Type n pthread_mutexattr_init() initializes mutex to default values n int pthread_mutexattr_init(pthread_mutexattr_t *attr); n pthread_mutexattr_destroy() destroy mutex attributes n pthread_mutexattr_destroy(pthread_mutexattr_t *attr);

13 + Mutex Attributes: process-shared n Provide more efficient mutex implementation n PTHREAD_PROCESS_PRIVATE n pthread_mutexattr_getpshared() get process-shared attribute n int pthread_mutexattr_getpshared(const pthread_mutexattr_t* restrict attr, int* restrict pshared); n pthread_mutexattr_setpshared() set process-shared attribute n int pthread_mutexattr_setpshared(pthread_mutexattr_t* attr, int* restrict pshared);

14 + Mutex Attributes: Robust n Robust is for mutexes that are shared among processes n PTHREAD_MUTEX_STALLED no action taken when process terminates holding mutex (default) n PTHREAD_MUTEX_ROBUST make pthread_mutex_lock() return EOWNERDEAD instead of 0 n pthread_mutexattr_getrobust() get robust attribute n int pthread_mutexattr_getrobust(const pthread_mutexattr_t* restrict attr, int* restrict robust); n pthread_mutexattr_setrobust() set robust attribute n int pthread_mutexattr_setrobust(pthread_mutexattr_t* attr, int* restrict robust);

15 + Thread Attributes: Robust consistency n If we get an EOWNERDEAD from pthread_mutex_lock() we need to call: n pthread_mutex_consistent() Allows mutex to behave normally after it is unlocked n int pthread_mutex_consistent(pthread-mutex_t* mutex);

16 + Thread Attributes: gettype, settype n Type determines whether it can be locked again by a thread that owns it n Default type is normal n pthread_mutexattr_gettype() get mutex type n int pthread_mutexattr_gettype(const pthread_mutexattr_t* restrict attr, int* restrict type); n pthread_mutexattr_settype() set mutex type n int pthread_mutexattr_settype(pthread_mutexattr_t* attr, int* type);

17 +

18 +

19 + Reader-Writer Locks Attributes n pthread_rwlockattr_init() Initialize attribute for readerwriter lock n int pthread_rwlockattr_init(pthread_rwlockattr_t* attr); n pthread_rwlockattr_destroy() Destroy attribute for readerwriter lock n int pthread_rwlockattr_destroy(pthread_rwlockattr_t* attr);

20 + RD/RW Lock Attributes: Process Shared n pthread_rwlockattr_getpshared() Get process-shared attribute n int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t* restrict attr, int* restrict pshared); n pthread_rwlockattr_setpshared() Set process-shared attribute n int pthread_rwlockattr_setpshared(pthread_rwlockattr_t* attr, int* pshared);

21 + Condition Variables n pthread_condattr_init() initialize condition variable attribute n int pthread_condattr_init(pthread_condattr_t* attr); n pthread_condattr_destroy() destroy condition variable attribute n int pthread_condattr_destroy(pthread_condattr_t* attr); n pthread_condattr_getpshared() get process shared attribute of conditional variable n int pthread_condattr_getpshared(const pthread_condattr_t* restrict attr, int* restrict pshared); n pthread_condattr_setpshared() set process shared attribute of conditional variable n int pthread_condattr_setpshared(pthread_condattr_t* attr, int pshared);

22 + Conditional Variables: Clocks n The clock attribute controls which clock is used for pthread_cond_timedwait() n pthread_condattr_getclock() get clock ID n int pthread_condattr_getclock(const pthread_condattr_t* restrict attr, int* restrict clock_id); n pthread_condattr_setpshared() set clock ID n int pthread_condattr_setclock(pthread_condattr_t* attr, int clock_id);

23 + Barrier Attributes n pthread_barrierattr_init() initialize barrier attribute n int pthread_barrierattr_init(pthread_barrierattr_t* attr); n pthread_barrierattr_destroy() destroy barrier attribute n int pthread_barrierattr_destroy(pthread_barrierattr_t* attr); n pthread_barrierattr_getpshared() get process shared attribute of barrier n int pthread_barrierattr_getpshared(const pthread_barrierattr_t* restrict attr, int* restrict pshared); n pthread_barrierattr_setpshared() set process shared attribute of barrier n int pthread_barrierattr_setpshared(pthread_barrierattr_t* attr, int pshared);

24 + Reentrant functions n A reentrant function can be interrupted in the middle of its execution and safely reentered again n A function is thread-safe if it can be called by multiple threads at the same time n A function is async-signal safe if it is safe to reenter it from an asynchronous signal handler

25 + Non Thread-Safe Functions

26 + File Locks n ftrylockfile() Check lock associated with given FILE n int ftrylockfile(file* fp); n flockfile() Lock given file n void flockfile(file* fp); n funlockfile() Unlock given file n void funlock(file* fp); n File lock is recursive, so locking while holding it does not cause a deadlock

27 + Character-at-a-time Locking n These functions should be surrounded by calls to flockfile() and funlockfile() n getchar_unlocked() get char from stdin n int getchar_unlocked(void); n getc_unlocked() get char from file fp n int getc_unlocked(file* fp); n putchar_unlocked() write char to stdout n int putchar_unlocked(int c); n putc_unlocked() write char to file fp n int putc_unlocked(int c, FILE* fp);

28 +

29 + Thread Specific Data n We may want to keep data specific to a thread, which is hard in a shared address space n why? n errono and other process based interfaces n keeping private data n We keep data private using a key: n pthread_key_create() Creates a key in order to store thread-private info n int pthread-key_create(pthread_key_t* keyp, void (*destructor) (void *)); n Destructor is called when thread exits

30 + Thread-Specific Data cont. n pthread_key_delete() Deletes key n int pthread_key_delete(pthread_key_t key); n Note: Calling pthread_key_delete() does not invoke the destructor n pthread_once() - solves race-conditions associated with keys n pthread_once_t initflag = PTHREAD_ONCE_INIT; n int pthread_once(pthread_once_t* initflag, void (*initfn)(void));

31 + Cancel Options n pthread_setcancelstate() changes cancelability state of thread n int pthread_setcancelstate(int state, int* oldstate); n PTHREAD_CANCEL_ENABLE n PTHREAD_CANCEL_DISABLE n pthread_testcancel() add cancellation points to program n int pthread_testcancel(void); n if a cancellation is pending it will cancel thread n unless cancellation is disabled

32 + Cancel Options n pthread_setcanceltype() set the type of cancellation of the thread n int pthread_setcancel(int type, int* oldtype); n Types: n PTHREAD_CANCEL_DEFERRED n PTHREAD_CANCEL_ASYNCHRONOUS

33 + Threads and Signals n Each thread has its own signal mask n Threads share signal disposition n All threads share signal handler actions n pthread_sigmask() examine and change blocked signal for a thread n int pthread_sigmask(int how, const sigset_t* restrict set, sigset_t* restrict oset);

34 + Threads and Signals n sigwait() wait for a signal n int sigwait(const sigset_t* restrict set, int* restrict signop); n pthread_kill() kill for threads n int pthread_kill(pthread_t thread, int signo);

35 + n pthread_atfork() establish fork handlers to clean up and lock state n pthread_atfork(void (*prepare),(void), void (*parent)(void), void (*child)(void)); n prepare handler is called in the parent before the fork n acquires all locks defined by parent n parent handler is called in the parent after the fork before the child has returned n unlocks all the clocks acquired by the prepare handler n child handler is called before the child returns n release all locks acquired by prepare handler

Posix Threads (Pthreads)

Posix Threads (Pthreads) Posix Threads (Pthreads) Reference: Programming with POSIX Threads by David R. Butenhof, Addison Wesley, 1997 Threads: Introduction main: startthread( funk1 ) startthread( funk1 ) startthread( funk2 )

More information

POSIX Threads. Paolo Burgio

POSIX Threads. Paolo Burgio POSIX Threads Paolo Burgio paolo.burgio@unimore.it The POSIX IEEE standard Specifies an operating system interface similar to most UNIX systems It extends the C language with primitives that allows the

More information

real time operating systems course

real time operating systems course real time operating systems course 4 introduction to POSIX pthread programming introduction thread creation, join, end thread scheduling thread cancellation semaphores thread mutexes and condition variables

More information

OS Abstractor with POSIX

OS Abstractor with POSIX www.mapusoft.com OS Abstractor with POSIX OS Abstractor provides you a robust and standard OS interface architecture for flexible application development while eliminating the risks associated with selecting

More information

Lw and ULw POSIX AEPs for Resource Constrained Processors. Document WINNF-14-S-0009

Lw and ULw POSIX AEPs for Resource Constrained Processors. Document WINNF-14-S-0009 Lw and ULw POSIX AEPs for Resource Constrained Processors Document WINNF-14-S-0009 Version V1.0.0 25 July 2014 TERMS, CONDITIONS & NOTICES This document has been prepared by the work group of WInnF project

More information

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

POSIX Threads: a first step toward parallel programming. George Bosilca POSIX Threads: a first step toward parallel programming George Bosilca bosilca@icl.utk.edu Process vs. Thread A process is a collection of virtual memory space, code, data, and system resources. A thread

More information

CS 33. Multithreaded Programming IV. CS33 Intro to Computer Systems XXXV 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

CS 33. Multithreaded Programming IV. CS33 Intro to Computer Systems XXXV 1 Copyright 2017 Thomas W. Doeppner. All rights reserved. CS 33 Multithreaded Programming IV CS33 Intro to Computer Systems XXXV 1 Copyright 2017 Thomas W. Doeppner. All rights reserved. Deviations Signals vs. Cancellation tamed lightning CS33 Intro to Computer

More information

Guide to DECthreads. Order Number: AA Q2DPD TK. December 1997

Guide to DECthreads. Order Number: AA Q2DPD TK. December 1997 Guide to DECthreads Order Number: AA Q2DPD TK December 1997 This guide reviews the principles of multithreaded programming, as reflected in the IEEE POSIX 1003.1c-1995 standard, and provides implementation

More information

Pthreads Library Interface. Frank Mueller. Florida State University. phone: (904) , July 22, 1993.

Pthreads Library Interface. Frank Mueller. Florida State University. phone: (904) ,   July 22, 1993. Pthreads Library Interface Frank Mueller Department of Computer Science, B-173 Florida State University Tallahassee, Florida 32306-4019 phone: (904) 644-3441, e-mail: mueller@cs.fsu.edu July 22, 1993 Abstract

More information

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

CPSC 313: Intro to Computer Systems. POSIX Threads. Latency Hiding / Multiprogramming (covered earlier) Ease of Programming (covered now) Why Threads? Latency Hiding / Multiprogramming (covered earlier) Ease of Programming (covered now) (R&R, Chapter 12) Thread Management Thread Safety Thread Attributes Why Threads? Latency Hiding / Multiprogramming

More information

Guide to the POSIX Threads Library

Guide to the POSIX Threads Library Guide to the POSIX Threads Library Order Number: AA QSBPD TE April 2001 This guide reviews the principles of multithreaded programming, as reflected in the IEEE POSIX 1003.1-1996 standard, and provides

More information

Deviations are things that modify a thread s normal flow of control. Unix has long had signals, and these must be dealt with in multithreaded

Deviations are things that modify a thread s normal flow of control. Unix has long had signals, and these must be dealt with in multithreaded Deviations are things that modify a thread s normal flow of control. Unix has long had signals, and these must be dealt with in multithreaded improvements to Unix. There are actually two fairly different

More information

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

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto Ricardo Rocha Department of Computer Science Faculty of Sciences University of Porto Slides based on the book Operating System Concepts, 9th Edition, Abraham Silberschatz, Peter B. Galvin and Greg Gagne,

More information

CSCE 313 Introduction to Computer Systems. Instructor: Dr. Dezhen Song

CSCE 313 Introduction to Computer Systems. Instructor: Dr. Dezhen Song CSCE 313 Introduction to Computer Systems Instructor: Dr. Dezhen Song POSIX Threads Why Threads? Latency Hiding / Multiprogramming (covered earlier) Ease of Programming (covered now) POSIX Threads (R&R,

More information

Programming with POSIX Threads

Programming with POSIX Threads Programming with POSIX Threads David R. Butenhof :vaddison-wesley Boston San Francisco New York Toronto Montreal London Munich Paris Madrid Capetown Sidney Tokyo Singapore Mexico City Contents List of

More information

CSCE 313: Intro to Computer Systems

CSCE 313: Intro to Computer Systems CSCE 313 Introduction to Computer Systems Instructor: Dr. Guofei Gu http://courses.cse.tamu.edu/guofei/csce313/ Why Threads? Latency Hiding / Multiprogramming (covered earlier) Ease of Programming (covered

More information

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

Threads need to synchronize their activities to effectively interact. This includes: KING FAHD UNIVERSITY OF PETROLEUM AND MINERALS Information and Computer Science Department ICS 431 Operating Systems Lab # 8 Threads Synchronization ( Mutex & Condition Variables ) Objective: When multiple

More information

THREADS VS. PROCESSES

THREADS VS. PROCESSES THREADS VS. PROCESSES Threads Stefan D. Bruda Winter 2018 We have seen how to use concurrent processes, with one of execution each Concurrency can be also implemented using one process with multiple s

More information

ANSI/IEEE POSIX Standard Thread management

ANSI/IEEE POSIX Standard Thread management Pthread Prof. Jinkyu Jeong( jinkyu@skku.edu) TA Jinhong Kim( jinhong.kim@csl.skku.edu) TA Seokha Shin(seokha.shin@csl.skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu The

More information

RTEMS POSIX API Guide. Release (master) Copyright 2018, RTEMS Project (built 22nd October 2018)

RTEMS POSIX API Guide. Release (master) Copyright 2018, RTEMS Project (built 22nd October 2018) RTEMS POSIX API Guide Release 5.0.0 (master) Copyright 2018, RTEMS Project (built 22nd October 2018) CONTENTS 1 Preface 3 1.1 Acknowledgements.................................. 4 2 Process Creation and

More information

Concurrency, Parallel and Distributed

Concurrency, Parallel and Distributed Threads (Chapter 11) Process -- Program, Memory (text, data, bss, heap, stack), execution stack - directly linked to execution function call frame, on the stack CPU -- execution "engine" Early computers:

More information

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

Programming with Shared Memory. Nguyễn Quang Hùng Programming with Shared Memory Nguyễn Quang Hùng Outline Introduction Shared memory multiprocessors Constructs for specifying parallelism Creating concurrent processes Threads Sharing data Creating shared

More information

POSIX Condition Variables ADT

POSIX Condition Variables ADT 28 POSIX Condition Variables ADT One actual Attributes * 1. CV-list = list ids of thread waiting on CV - Domain: empty-list, list w/ 1 tid,... Two logical Attributes * 1. Boolean Condition C - Condition

More information

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

Chapter 10. Threads. OS Processes: Control and Resources. A process as managed by the operating system groups together: SFWR ENG 3BB4 Software Design 3 Concurrent System Design 2 SFWR ENG 3BB4 Software Design 3 Concurrent System Design 10.11 13 OS Processes: Control and Resources Chapter 10 Threads A process as managed

More information

Pthreads. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Pthreads. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University Pthreads Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu The Pthreads API ANSI/IEEE POSIX1003.1-1995 Standard Thread management Work directly on

More information

Pthread (9A) Pthread

Pthread (9A) Pthread Pthread (9A) Pthread Copyright (c) 2012 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later

More information

Definition Multithreading Models Threading Issues Pthreads (Unix)

Definition Multithreading Models Threading Issues Pthreads (Unix) Chapter 4: Threads Definition Multithreading Models Threading Issues Pthreads (Unix) Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads 1 Thread A Unix process (heavy-weight process HWP)

More information

THREADS. Jo, Heeseung

THREADS. Jo, Heeseung THREADS Jo, Heeseung TODAY'S TOPICS Why threads? Threading issues 2 PROCESSES Heavy-weight A process includes many things: - An address space (all the code and data pages) - OS resources (e.g., open files)

More information

ECE 598 Advanced Operating Systems Lecture 23

ECE 598 Advanced Operating Systems Lecture 23 ECE 598 Advanced Operating Systems Lecture 23 Vince Weaver http://web.eece.maine.edu/~vweaver vincent.weaver@maine.edu 21 April 2016 Don t forget HW#9 Midterm next Thursday Announcements 1 Process States

More information

Computer Systems Laboratory Sungkyunkwan University

Computer Systems Laboratory Sungkyunkwan University Threads Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Today s Topics Why threads? Threading issues 2 Processes Heavy-weight A process includes

More information

Multithreading Programming II

Multithreading Programming II Multithreading Programming II Content Review Multithreading programming Race conditions Semaphores Thread safety Deadlock Review: Resource Sharing Access to shared resources need to be controlled to ensure

More information

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

CS 153 Lab4 and 5. Kishore Kumar Pusukuri. Kishore Kumar Pusukuri CS 153 Lab4 and 5 CS 153 Lab4 and 5 Kishore Kumar Pusukuri Outline Introduction A thread is a straightforward concept : a single sequential flow of control. In traditional operating systems, each process has an address

More information

LSN 13 Linux Concurrency Mechanisms

LSN 13 Linux Concurrency Mechanisms LSN 13 Linux Concurrency Mechanisms ECT362 Operating Systems Department of Engineering Technology LSN 13 Creating Processes fork() system call Returns PID of the child process created The new process is

More information

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

pthreads Announcement Reminder: SMP1 due today Reminder: Please keep up with the reading assignments (see class webpage) pthreads 1 Announcement Reminder: SMP1 due today Reminder: Please keep up with the reading assignments (see class webpage) 2 1 Thread Packages Kernel thread packages Implemented and supported at kernel

More information

Shared Memory Parallel Programming with Pthreads An overview

Shared Memory Parallel Programming with Pthreads An overview Shared Memory Parallel Programming with Pthreads An overview Part II Ing. Andrea Marongiu (a.marongiu@unibo.it) Includes slides from ECE459: Programming for Performance course at University of Waterloo

More information

Real Time Operating Systems and Middleware

Real Time Operating Systems and Middleware Real Time Operating Systems and Middleware POSIX Threads Synchronization Luca Abeni abeni@dit.unitn.it Real Time Operating Systems and Middleware p. 1 Threads Synchronisation All the threads running in

More information

Pthreads: POSIX Threads

Pthreads: POSIX Threads Shared Memory Programming Using Pthreads (POSIX Threads) Lecturer: Arash Tavakkol arasht@ipm.ir Some slides come from Professor Henri Casanova @ http://navet.ics.hawaii.edu/~casanova/ and Professor Saman

More information

NETWORK PROGRAMMING. Ipv4 and Ipv6 interoperability

NETWORK PROGRAMMING. Ipv4 and Ipv6 interoperability NETWORK PROGRAMMING UNIT V ADVANCED SOCKETS Ipv4 and Ipv6 interoperability threaded servers thread creation and termination TCP echo server using threads Mutexes condition variables raw sockets raw socket

More information

High Performance Computing Course Notes Shared Memory Parallel Programming

High Performance Computing Course Notes Shared Memory Parallel Programming High Performance Computing Course Notes 2009-2010 2010 Shared Memory Parallel Programming Techniques Multiprocessing User space multithreading Operating system-supported (or kernel) multithreading Distributed

More information

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

Chapter 4 Threads. Images from Silberschatz 03/12/18. CS460 Pacific University 1 Chapter 4 Threads Images from Silberschatz Pacific University 1 Threads Multiple lines of control inside one process What is shared? How many PCBs? Pacific University 2 Typical Usages Word Processor Web

More information

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

CSci 4061 Introduction to Operating Systems. (Threads-POSIX) CSci 4061 Introduction to Operating Systems (Threads-POSIX) How do I program them? General Thread Operations Create/Fork Allocate memory for stack, perform bookkeeping Parent thread creates child threads

More information

POSIX Threads Programming

POSIX Threads Programming Tutorials Exercises Abstracts LC Workshops Comments Search Privacy & Legal Notice POSIX Threads Programming Table of Contents 1. 2. 3. 4. 5. 6. 7. 8. 9. Pthreads Overview 1. What is a Thread? 2. What are

More information

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

COSC 6374 Parallel Computation. Shared memory programming with POSIX Threads. Edgar Gabriel. Fall References COSC 6374 Parallel Computation Shared memory programming with POSIX Threads Fall 2012 References Some of the slides in this lecture is based on the following references: http://www.cobweb.ecn.purdue.edu/~eigenman/ece563/h

More information

Thread and Synchronization

Thread and Synchronization Thread and Synchronization pthread Programming (Module 19) Yann-Hang Lee Arizona State University yhlee@asu.edu (480) 727-7507 Summer 2014 Real-time Systems Lab, Computer Science and Engineering, ASU Pthread

More information

Task Parallelism with KTC & Bionic/OpenMP for Multicore-based Android

Task Parallelism with KTC & Bionic/OpenMP for Multicore-based Android www.kandroid.org 2013, 11th Kandroid Conference : "The Gate of the AOSP #4: Gerrit, Memory and Performance" Task Parallelism with KTC & Bionic/OpenMP for Multicore-based Android Mar-29-2013 13:30 ~ 14:20

More information

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

POSIX Threads: a first step toward parallel programming. George Bosilca POSIX Threads: a first step toward parallel programming George Bosilca bosilca@icl.utk.edu Process vs. Thread A process is a collection of virtual memory space, code, data, and system resources. A thread

More information

POSIX PTHREADS PROGRAMMING

POSIX PTHREADS PROGRAMMING POSIX PTHREADS PROGRAMMING Download the exercise code at http://www-micrel.deis.unibo.it/~capotondi/pthreads.zip Alessandro Capotondi alessandro.capotondi(@)unibo.it Hardware Software Design of Embedded

More information

Parallel Computing: Programming Shared Address Space Platforms (Pthread) Jin, Hai

Parallel Computing: Programming Shared Address Space Platforms (Pthread) Jin, Hai Parallel Computing: Programming Shared Address Space Platforms (Pthread) Jin, Hai School of Computer Science and Technology Huazhong University of Science and Technology Thread Basics! All memory in the

More information

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

Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits CS307 What is a thread? Threads A thread is a basic unit of CPU utilization contains a thread ID, a program counter, a register set, and a stack shares with other threads belonging to the same process

More information

A Note About Makefiles. In OneFS/BSD be sure to build using gmake You may have to remove -pedantic-errors from CFLAGS

A Note About Makefiles. In OneFS/BSD be sure to build using gmake You may have to remove -pedantic-errors from CFLAGS PThreads A Note About Makefiles In OneFS/BSD be sure to build using gmake You may have to remove -pedantic-errors from CFLAGS Creating/Starting a Thread (1 of 4) #include typedef void *THREAD_PROC_t(

More information

RTEMS POSIX API User s Guide

RTEMS POSIX API User s Guide RTEMS POSIX API User s Guide Edition 4.6.0, for RTEMS 4.6.0 30 August 2003 On-Line Applications Research Corporation On-Line Applications Research Corporation TEXinfo 2002-11-25.11 COPYRIGHT c 1988-2003.

More information

Xu Liu Derived from John Mellor-Crummey s COMP422 at Rice University

Xu Liu Derived from John Mellor-Crummey s COMP422 at Rice University Programming Shared-memory Platforms with Pthreads Xu Liu Derived from John Mellor-Crummey s COMP422 at Rice University Topics for Today The POSIX thread API (Pthreads) Synchronization primitives in Pthreads

More information

UMTX OP (2) FreeBSD System Calls Manual UMTX OP (2)

UMTX OP (2) FreeBSD System Calls Manual UMTX OP (2) NAME _umtx_op interface for implementation of userspace threading synchronization primitives LIBRARY Standard C Library (libc, lc) SYNOPSIS #include #include int _umtx_op(void,

More information

In the example, since printf() was never declared, the compiler had to try to figure out what kind of function it is.

In the example, since printf() was never declared, the compiler had to try to figure out what kind of function it is. Ans 1.a Dynamic lookup means that when a message is sent to an object, the method to be executed is selected dynamically, at run time, according to the implementation of the object that receives the message.

More information

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

Agenda. Process vs Thread. ! POSIX Threads Programming. Picture source: Agenda POSIX Threads Programming 1 Process vs Thread process thread Picture source: https://computing.llnl.gov/tutorials/pthreads/ 2 Shared Memory Model Picture source: https://computing.llnl.gov/tutorials/pthreads/

More information

TS Thread Library Reference. Version 6.2, December 2004

TS Thread Library Reference. Version 6.2, December 2004 TS Thread Library Reference Version 6.2, December 2004 IONA, IONA Technologies, the IONA logo, Orbix, Orbix/E, Orbacus, Artix, Orchestrator, Mobile Orchestrator, Enterprise Integrator, Adaptive Runtime

More information

Thread Concept. Thread. No. 3. Multiple single-threaded Process. One single-threaded Process. Process vs. Thread. One multi-threaded Process

Thread Concept. Thread. No. 3. Multiple single-threaded Process. One single-threaded Process. Process vs. Thread. One multi-threaded Process EECS 3221 Operating System Fundamentals What is thread? Thread Concept No. 3 Thread Difference between a process and a thread Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

More information

RTEMS POSIX API User s Guide

RTEMS POSIX API User s Guide RTEMS POSIX API User s Guide Edition 4.10.2, for RTEMS 4.10.2 13 December 2011 On-Line Applications Research Corporation On-Line Applications Research Corporation TEXinfo 2009-08-14.15 COPYRIGHT c 1988-2011.

More information

COP 4610: Introduction to Operating Systems (Spring 2015) Chapter 4: Threads. Zhi Wang Florida State University

COP 4610: Introduction to Operating Systems (Spring 2015) Chapter 4: Threads. Zhi Wang Florida State University COP 4610: Introduction to Operating Systems (Spring 2015) Chapter 4: Threads Zhi Wang Florida State University Contents Thread overview Multithreading models Thread libraries Threading issues Operating

More information

Orbix TS Thread Library Reference

Orbix TS Thread Library Reference Orbix 6.3.9 TS Thread Library Reference Micro Focus The Lawn 22-30 Old Bath Road Newbury, Berkshire RG14 1QN UK http://www.microfocus.com Copyright Micro Focus 2017. All rights reserved. MICRO FOCUS, the

More information

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

POSIX threads CS 241. February 17, Copyright University of Illinois CS 241 Staff POSIX threads CS 241 February 17, 2012 Copyright University of Illinois CS 241 Staff 1 Recall: Why threads over processes? Creating a new process can be expensive Time A call into the operating system

More information

CS370 Operating Systems

CS370 Operating Systems CS370 Operating Systems Colorado State University Yashwant K Malaiya Spring 2018 Lecture 7 Threads Slides based on Text by Silberschatz, Galvin, Gagne Various sources 1 1 FAQ How many processes can a core

More information

Concurrent Server Design Multiple- vs. Single-Thread

Concurrent Server Design Multiple- vs. Single-Thread Concurrent Server Design Multiple- vs. Single-Thread Chuan-Ming Liu Computer Science and Information Engineering National Taipei University of Technology Fall 2007, TAIWAN NTUT, TAIWAN 1 Examples Using

More information

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

Concurrency and Synchronization. ECE 650 Systems Programming & Engineering Duke University, Spring 2018 Concurrency and Synchronization ECE 650 Systems Programming & Engineering Duke University, Spring 2018 Concurrency Multiprogramming Supported by most all current operating systems More than one unit of

More information

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

Lecture 4. Threads vs. Processes. fork() Threads. Pthreads. Threads in C. Thread Programming January 21, 2005 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

More information

Lecture 19: Shared Memory & Synchronization

Lecture 19: Shared Memory & Synchronization Lecture 19: Shared Memory & Synchronization COMP 524 Programming Language Concepts Stephen Olivier April 16, 2009 Based on notes by A. Block, N. Fisher, F. Hernandez-Campos, and D. Stotts Forking int pid;

More information

Synchronization. Semaphores implementation

Synchronization. Semaphores implementation Synchronization Semaphores implementation Possible implementations There are seeral possible implementations (standard and non standard)of a semaphore Semaphores through pipe POSIX semaphores Linux semaphores

More information

Dead ways in multithreaded programing. Zdeněk Kotala Revenue Product Engineer Sun Microsystems

Dead ways in multithreaded programing. Zdeněk Kotala Revenue Product Engineer Sun Microsystems Dead ways in multithreaded programing Zdeněk Kotala Revenue Product Engineer Sun Microsystems 1 Agenda Process life Signals Atfork handlers Memory access Sessions 2 Why Nine years ago I spent one month

More information

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

Outline. CS4254 Computer Network Architecture and Programming. Introduction 2/4. Introduction 1/4. Dr. Ayman A. Abdel-Hamid. Threads Dr. Ayman Abdel-Hamid, CS4254 Spring 2006 1 CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department Virginia Tech Threads Outline Threads (Chapter

More information

Questions from last time

Questions from last time Questions from last time Pthreads vs regular thread? Pthreads are POSIX-standard threads (1995). There exist earlier and newer standards (C++11). Pthread is probably most common. Pthread API: about a 100

More information

POSIX Threads Programming

POSIX Threads Programming Tutorials Exercises Abstracts LC Workshops Comments Search Privacy & Legal Notice POSIX Threads Programming Table of Contents 1. Abstract 2. Pthreads Overview 1. What is a Thread? 2. What are Pthreads?

More information

Introduction to parallel computing

Introduction to parallel computing Introduction to parallel computing Shared Memory Programming with Pthreads (3) Zhiao Shi (modifications by Will French) Advanced Computing Center for Education & Research Vanderbilt University Last time

More information

CS370 Operating Systems

CS370 Operating Systems CS370 Operating Systems Colorado State University Yashwant K Malaiya Fall 2017 Lecture 8 Slides based on Text by Silberschatz, Galvin, Gagne Various sources 1 1 FAQ How many partners can we cave for project:

More information

Parallel Programming with Threads

Parallel Programming with Threads Thread Programming with Shared Memory Parallel Programming with Threads Program is a collection of threads of control. Can be created dynamically, mid-execution, in some languages Each thread has a set

More information

Network Programming TDC 561

Network Programming TDC 561 Network Programming TDC 561 Lecture # 6: Multithreaded Servers Dr. Ehab S. Al-Shaer School of Computer Science & Telecommunication DePaul University Chicago, IL 1 Introduction to Multithreaded Network

More information

Scheduling Policies. Threads Library. Lightweight Processes

Scheduling Policies. Threads Library. Lightweight Processes Scheduling Policies User Unbound Threads Bound Threads Threads Library Kernel Lightweight Processes The threads library uses underlying threads of control called lightweight processes that are supported

More information

Paralleland Distributed Programming. Concurrency

Paralleland Distributed Programming. Concurrency Paralleland Distributed Programming Concurrency Concurrency problems race condition synchronization hardware (eg matrix PCs) software (barrier, critical section, atomic operations) mutual exclusion critical

More information

Lecture 7: Signals and Events. CSC 469H1F Fall 2006 Angela Demke Brown

Lecture 7: Signals and Events. CSC 469H1F Fall 2006 Angela Demke Brown Lecture 7: Signals and Events CSC 469H1F Fall 2006 Angela Demke Brown Signals Software equivalent of hardware interrupts Allows process to respond to asynchronous external events (or synchronous internal

More information

pthreads CS449 Fall 2017

pthreads CS449 Fall 2017 pthreads CS449 Fall 2017 POSIX Portable Operating System Interface Standard interface between OS and program UNIX-derived OSes mostly follow POSIX Linux, macos, Android, etc. Windows requires separate

More information

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

CSCI4430 Data Communication and Computer Networks. Pthread Programming. ZHANG, Mi Jan. 26, 2017 CSCI4430 Data Communication and Computer Networks Pthread Programming ZHANG, Mi Jan. 26, 2017 Outline Introduction What is Multi-thread Programming Why to use Multi-thread Programming Basic Pthread Programming

More information

für Mathematik in den Naturwissenschaften Leipzig

für Mathematik in den Naturwissenschaften Leipzig Max-Planck-Institut für Mathematik in den Naturwissenschaften Leipzig Implementation and Usage of a Thread PoolbasedonPOSIXThreads by Ronald Kriemann Technical Report no.: 2 2003 Implementation and Usage

More information

CS551 Warm-up Project #2

CS551 Warm-up Project #2 CS55 Warm-up Project #2 Bill Cheng http://merlot.usc.edu/cs55-f2 Multi-threading Exercise Make sure you are familiar with the pthreads library good source is the book by Nichols, Buttlar, and Farrell Pthreads

More information

Topics: Threads (SGG, Chapter 04) (USP, Chapter 12) CS 3733 Operating Systems

Topics: Threads (SGG, Chapter 04) (USP, Chapter 12) CS 3733 Operating Systems Topics: Threads (SGG, Chapter 04) (USP, Chapter 12) CS 3733 Operating Systems Instructor: Dr. Turgay Korkmaz Department Computer Science The University of Texas at San Antonio Office: NPB 3.330 Phone:

More information

CS370 Operating Systems

CS370 Operating Systems CS370 Operating Systems Colorado State University Yashwant K Malaiya Spring 2019 Lecture 7 Threads Slides based on Text by Silberschatz, Galvin, Gagne Various sources 1 1 FAQ Amdahls law example: Person

More information

Modern Computer Architecture. Lecture 10 Multi-Core: pthreads, OpenMP Segmented Scan

Modern Computer Architecture. Lecture 10 Multi-Core: pthreads, OpenMP Segmented Scan Modern Computer Architecture Lecture 10 Multi-Core: pthreads, OpenMP Segmented Scan Outline Multi-Core: pthreads OpenMP Segmented Scan 2 Multi-Core: pthreads, OpenMP Multi- and Many-Core Systems I Tightly

More information

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

Thread. Disclaimer: some slides are adopted from the book authors slides with permission 1 Thread Disclaimer: some slides are adopted from the book authors slides with permission 1 IPC Shared memory Recap share a memory region between processes read or write to the shared memory region fast

More information

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks CSci 4061 Introduction to Operating Systems Synchronization Basics: Locks Synchronization Outline Basics Locks Condition Variables Semaphores Basics Race condition: threads + shared data Outcome (data

More information

Multithreaded Programming

Multithreaded Programming Multithreaded Programming The slides do not contain all the information and cannot be treated as a study material for Operating System. Please refer the text book for exams. September 4, 2014 Topics Overview

More information

ECE 598 Advanced Operating Systems Lecture 11

ECE 598 Advanced Operating Systems Lecture 11 ECE 598 Advanced Operating Systems Lecture 11 Vince Weaver http://www.eece.maine.edu/~vweaver vincent.weaver@maine.edu 27 February 2018 Announcements Homework #5 Posted Some notes: Review of C string handling,

More information

Midterm Exam CPS 210: Operating Systems Spring 2013

Midterm Exam CPS 210: Operating Systems Spring 2013 Your name: Sign for your honor: Midterm Exam CPS 210: Operating Systems Spring 2013 The last page of this exam is a list of terms used in this class, and whose meanings you should know. You may detach

More information

Systems Programming/ C and UNIX

Systems Programming/ C and UNIX Systems Programming/ C and UNIX A. Fischer CSCI 4547 / 6647 November 16, 2017 A. Fischer CSCI 4547 / 6647 Systems Programming Lecture 8... 1/20 November 16, 2017 1 / 20 Outline 1 Signals for Threads Signals

More information

COMP6771 Advanced C++ Programming

COMP6771 Advanced C++ Programming 1. COMP6771 Advanced C++ Programming Week 9 Multithreading (continued) 2016 www.cse.unsw.edu.au/ cs6771 2. So Far Program Workflows: Sequential, Parallel, Embarrassingly Parallel Memory: Shared Memory,

More information

CMSC421: Principles of Operating Systems

CMSC421: Principles of Operating Systems CMSC421: Principles of Operating Systems Nilanjan Banerjee Assistant Professor, University of Maryland Baltimore County nilanb@umbc.edu http://www.csee.umbc.edu/~nilanb/teaching/421/ Principles of Operating

More information

ECE 574 Cluster Computing Lecture 8

ECE 574 Cluster Computing Lecture 8 ECE 574 Cluster Computing Lecture 8 Vince Weaver http://web.eece.maine.edu/~vweaver vincent.weaver@maine.edu 16 February 2017 Announcements Too many snow days Posted a video with HW#4 Review HW#5 will

More information

Semantics of fork() and exec()

Semantics of fork() and exec() Threads Week 3.2 Threading Issues Semantics of fork() and exec() system calls Signal handling Synchronous and asynchronous Thread cancellation of target thread Asynchronous or deferred Thread-local storage

More information

Introduction to PThreads and Basic Synchronization

Introduction to PThreads and Basic Synchronization Introduction to PThreads and Basic Synchronization Michael Jantz, Dr. Prasad Kulkarni Dr. Douglas Niehaus EECS 678 Pthreads Introduction Lab 1 Introduction In this lab, we will learn about some basic synchronization

More information

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017 CS 471 Operating Systems Yue Cheng George Mason University Fall 2017 1 Review: Sync Terminology Worksheet 2 Review: Semaphores 3 Semaphores o Motivation: Avoid busy waiting by blocking a process execution

More information

CSC 4320 Test 1 Spring 2017

CSC 4320 Test 1 Spring 2017 CSC 4320 Test 1 Spring 2017 Name 1. What are the three main purposes of an operating system? 2. Which of the following instructions should be privileged? a. Set value of timer. b. Read the clock. c. Clear

More information

Lecture 4 Threads. (chapter 4)

Lecture 4 Threads. (chapter 4) Bilkent University Department of Computer Engineering CS342 Operating Systems Lecture 4 Threads (chapter 4) Dr. İbrahim Körpeoğlu http://www.cs.bilkent.edu.tr/~korpe 1 References The slides here are adapted/modified

More information

Xilkernel (v5.00.a) Overview. Why Use a Kernel? Key Features. UG708 March 1, 2011

Xilkernel (v5.00.a) Overview. Why Use a Kernel? Key Features. UG708 March 1, 2011 Xilkernel (v5.00.a) UG708 March 1, 2011 Overview Why Use a Kernel? Key Features Xilkernel is a small, robust, and modular kernel. It is highly integrated with the Platform Studio framework and is a free

More information