Implementing Lightweight Threads

Similar documents
Implementing Lightweight Threads

ECE 650 Systems Programming & Engineering. Spring 2018

Chapter 4 Multithreaded Programming

SPARCompiler Ada Multithreading Your Ada Application

Signals and Session Management. Signals. Mechanism to notify processes of system events

SMD149 - Operating Systems

Unix System Programming - Chapter 8

Signals: Management and Implementation. Sanjiv K. Bhatia Univ. of Missouri St. Louis

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

Chapter 4: Multi-Threaded Programming

I.-C. Lin, Assistant Professor. Textbook: Operating System Concepts 8ed CHAPTER 4: MULTITHREADED PROGRAMMING

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

Concurrent Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

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

Chapter 3 Process Description and Control

Overview. Administrative. * HW 1 grades. * HW 2 Due. Topics. * 5.1 What is a Signal? * Dealing with Signals - masks, handlers

Operating Systems 2010/2011

Signals, Synchronization. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Operating Systems 2 nd semester 2016/2017. Chapter 4: Threads

Signals. CSC209: Software Tools and Systems Programming. Furkan Alaca & Paul Vrbik

Lecture 4 Threads. (chapter 4)

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

IMPLEMENTATION OF SIGNAL HANDLING. CS124 Operating Systems Fall , Lecture 15

* What are the different states for a task in an OS?

EF HLD on SMP machines. Sub-farm design Inter-thread communications and error handling Future developments. Andrea NEGRI D.F.N.T. and I.N.F.N.

Each terminal window has a process group associated with it this defines the current foreground process group. Keyboard-generated signals are sent to

Threads Chapter 5 1 Chapter 5

Chapter 4: Threads. Operating System Concepts. Silberschatz, Galvin and Gagne

Chapter 4: Threads. Chapter 4: Threads

!! How is a thread different from a process? !! Why are threads useful? !! How can POSIX threads be useful?

Last class: Today: Thread Background. Thread Systems

Signals. Joseph Cordina

Shell and Signals. Computer Organization 3/17/2015. CSC252 - Spring The World of Multiprogramming or Multitasking. Unix Process Hierarchy

Shell Execution of Programs. Process Groups, Session and Signals 1

A read or write being atomic means that its effect is as if it happens instantaneously.

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

COSC243 Part 2: Operating Systems

Chapter 5: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads

CSC209H Lecture 8. Dan Zingaro. March 4, 2015

Signals are a kernel-supported mechanism for reporting events to user code and forcing a response to them. There are actually two sorts of such

OPERATING SYSTEM. Chapter 4: Threads

Acontecimentos assíncronos (POSIX signals) Sincronização com múltiplos acontecimentos

Course Syllabus. Operating Systems, Spring 2016, Meni Adler, Danny Hendler & Amnon Meisels

Interprocess Communication

Page 1. Analogy: Problems: Operating Systems Lecture 7. Operating Systems Lecture 7

Chapter 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

CSCE Operating Systems Interrupts, Exceptions, and Signals. Qiang Zeng, Ph.D. Fall 2018

Lesson 3. The func procedure allows a user to choose the action upon receipt of a signal.

! How is a thread different from a process? ! Why are threads useful? ! How can POSIX threads be useful?

Concurrency: Deadlock and Starvation

Chapter 4: Threads. Chapter 4: Threads. Overview Multicore Programming Multithreading Models Thread Libraries Implicit Threading Threading Issues

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

Review: Easy Piece 1

CS 318 Principles of Operating Systems

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

Last time: Scheduling. Goals today. Recap: Hardware support for synchronization. Disabling interrupts. Disabling interrupts

CSE 153 Design of Operating Systems Fall 2018

Operating Systems Overview. Chapter 2

Chapter 4: Threads. Operating System Concepts 9 th Edition

VEOS high level design. Revision 2.1 NEC

Chapter 4: Threads. Operating System Concepts 9 th Edition

Multitasking. Programmer s model of multitasking. fork() spawns new process. exit() terminates own process

Process Characteristics. Threads Chapter 4. Process Characteristics. Multithreading vs. Single threading

Threads Chapter 4. Reading: 4.1,4.4, 4.5

Threading and Synchronization. Fahd Albinali


Che-Wei Chang Department of Computer Science and Information Engineering, Chang Gung University

Operating System. Chapter 4. Threads. Lynn Choi School of Electrical Engineering

Processes The Process Model. Chapter 2 Processes and Threads. Process Termination. Process States (1) Process Hierarchies

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18

Embedded Systems Programming

CS213. Exceptional Control Flow Part II. Topics Process Hierarchy Signals

CSE 153 Design of Operating Systems Fall 18

Operating Systems: Internals and Design Principles. Chapter 4 Threads Seventh Edition By William Stallings

Concurrency, Thread. Dongkun Shin, SKKU

The Kernel. wants to be your friend

Processes The Process Model. Chapter 2. Processes and Threads. Process Termination. Process Creation

3.1 Introduction. Computers perform operations concurrently

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

Native POSIX Thread Library (NPTL) CSE 506 Don Porter

Process Description and Control

Overview. POSIX signals. Generation of signals. Handling signals. Some predefined signals. Real-time Systems D0003E 3/3/2009

Processes Prof. James L. Frankel Harvard University. Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved.

CSE 410: Computer Systems Spring Processes. John Zahorjan Allen Center 534

ECE 574 Cluster Computing Lecture 8

Agenda. Threads. Single and Multi-threaded Processes. What is Thread. CSCI 444/544 Operating Systems Fall 2008

Chapter 2 Processes and Threads

Shared Memory Parallel Programming with Pthreads An overview

For use by students enrolled in #71251 CSE430 Fall 2012 at Arizona State University. Do not use if not enrolled.

More on Synchronization and Deadlock

Processes and Threads

Questions from last time

Programming Assignments will be.. All the PAs are continuous 3 major factors that you should consider

Process management. What s in a process? What is a process? The OS s process namespace. A process s address space (idealized)

Threads. Threads The Thread Model (1) CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5

CSE 451: Operating Systems Winter Module 4 Processes. Mark Zbikowski Allen Center 476

CSE 153 Design of Operating Systems

Threads Questions Important Questions

Chapter 6 Concurrency: Deadlock and Starvation

Transcription:

Implementing Lightweight Threads paper Implementing Lightweight Threads by Stein and Shah implementation report thread-specific data, signals and threads signal handling also in Nichols et al. Ch. 5

Library Model described library is many-to-many scheduling user-level threads onto LWPs threads can be bound and unbound this is not a Pthreads implementation, but is close

Thread Structure Each thread has the standard set of data associated with it thread ID execution context signal mask priority pointer to thread stack Stacks have a red zone if they are allocated automatically (page after stack is invalid) or users can specify own stack and do whatever they want

Thread-Specific Data thread-specific data useful Pthreads library offers an interface for maintaining thread-specific data implementation techniques: through a library API (as with Pthreads) with compiler/linker support compiler recognizes special variables (e.g., #pragma unshared) and the linker defines a symbol to represent their size the library then allocates space from the base of the stack object files with thread-specific data cannot be loaded dynamically (dlopen()) since the stack will then be already active h/w support makes this more convenient: the %g7 register on a SPARC is reserved (compilers should not use it when generating code), and can be used to hold a pointer to thread-specific data

Preemption Two cases: a newly runnable thread has a higher priority than an active one an active thread s priority is lowered below that of a runnable thread The library handles preemption by recognizing these cases (they happen through library calls) sending an SIGLWP signal to the LWP that needs to take action (a handler is installed by the library) Possible problem: the preemted thread may be in the kernel executing a system call library has no way of doing anything, (it can only see the user stack) the call will be restarted (unsafe!)

Threads and LWPs The thread library offers a way to ask for a number of LWPs thr_setconcurrency() New LWPs are created then all existing ones are blocked and runnable threads exist the kernel sends a SIGWAITING signal when all LWPs in a process are blocked LWP can idle wait for more work, or can be parked wait for specific thread to become runnable again for bound threads always park

Traditional Signal Processing A process can associate an action with the signals it may receive (sigaction) SIG_IGN: ignore signal SIG_DFL: default action usually terminate or ignore, but can be stop (SIGSTOP), suspend (SIGTSTP), resume (SIGCONT) execute handler Signals can be generated by the system or by other processes, can be synchronous or asynchronous A process can block (mask) signals. If they are later unmasked, they get delivered then, but they are not queued! total number of signals received <= number of signals sent

Signal handling in thread libraries a single-threaded process should continue to use signals the way it always has, even if linked with a threads library solution: signals are still delivered to the entire process, i.e. sigaction table is common for entire process in a multithreaded process one thread must be selected to process the signal (run signal handler) solution: per thread signal masks let the threads declare what they want to handle a signal handler has to work in a way such that it does not interfere with the thread execution solution: async safety

Signal handling kinds of signals synchronous undirect SIGFPE (e.g. divide by zero), SIGSEGV (access to protected memory), SIGPIPE (use of broken pipe) synchronous directed: thread_kill asynchronous (undirected): kill SIGKILL, SIGALRM, the first two kinds are delivered to the thread directly. the last can be delivered to any one thread that can handle it (only one!)

Thread libraries and Async Signals problem: user-level threads and their masks are invisible to the kernel signal delivery depends on the thread signal mask kernel cannot see it, so cannot determine whether to deliver an asynchronous signal to the thread or not kernel cannot deliver signal to a thread that has it masked, but cannot drop it just because currently executing thread has blocked it

Async Safety another goal of thread libraries: async safe critical sections a function is async-safe if it is reentrant while handling a signal if a call is not async-safe, then calling it again from a signal handler may result in a deadlock (i.e. the signal handler will block waiting for the interrupted code) Pthreads mutexes and condition variables are not asyncsafe (POSIX semaphores are) Async safety is a standard property of system routines

Async Safety in User libraries how does a library enable some operations to be async-safe? just mask the signals in the critical section e.g., an async-safe mutex_lock operation will mask the signals and a mutex_unlock will unmask them such critical sections could be in user code or the thread library code itself since the first signal handler to run is inside the library, masking signals could have very low overhead (just change library data, no system call)

Solutions in Thread Libraries how can signals be handled by a user-level thread library? the LWP signal mask is set to the union of all thread signal masks or at least it s less restrictive than currently active thread the library installs its own signal handlers signals are not first handled by the current thread, but by the library when a signal is delivered, the library signal handler finds a thread that can receive it and delivers it to it (this possibly causes an inactive thread to be scheduled) if the signal is masked by all threads (not really masked, but deferred for async safety), the LWP signal mask is changed and the signal is resent to the process (if unidirected) or the LWP (if directed) so that the default action is taken similarly inside the kernel! (substitute process for LWP and LWP for thread )

Destroying threads Deteached threads on exit -> put on deathrow and periodically destroyed by reaper thread Undeteached threads which exit are reaped on thread_join (join code should implement appropriate operations) Thread structures/stacks are placed on stack -> not to wait for new page allocs, etc.