CS420: Operating Systems

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

CS 450 Operating System Week 4 Lecture Notes

Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads. Operating System Concepts

Chapter 4: Threads. Chapter 4: Threads

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

Chapter 4: Threads. Operating System Concepts with Java 8 th Edition

Chapter 4: Multithreaded Programming Dr. Varin Chouvatut. Operating System Concepts 8 th Edition,

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

Chapter 4: Multithreaded Programming. Operating System Concepts 8 th Edition,

Chapter 4: Multithreaded Programming

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

CS307: Operating Systems

Chapter 4: Threads. Chapter 4: Threads

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

Outline. Threads. Single and Multithreaded Processes. Benefits of Threads. Eike Ritter 1. Modified: October 16, 2012

Chapter 4 Multithreaded Programming

CS420: Operating Systems

Chapter 4: Multithreaded

Chapter 5: Processes & Process Concept. Objectives. Process Concept Process Scheduling Operations on Processes. Communication in Client-Server Systems

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

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

OPERATING SYSTEM. Chapter 4: Threads

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

Chapter 4: Threads. Operating System Concepts 9 th Edition

Chapter 4: Threads. Operating System Concepts 9 th Edition

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Semantics of fork() and exec()

CSCE 313: Intro to Computer Systems

Operating Systems Prof. Ashok K Agrawala

Chapter 4: Multi-Threaded Programming

Chapter 5: Threads. Single and Multithreaded Processes

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

Chapter 4: Multithreaded Programming

Multithreaded Programming

Processes and Threads

Multithreading. Reading: Silberschatz chapter 5 Additional Reading: Stallings chapter 4

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

Chapter 4: Processes. Process Concept

Chapter 3: Process-Concept. Operating System Concepts 8 th Edition,

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

Chapter 4: Processes

Chapter 3: Processes. Operating System Concepts 8 th Edition,

Threads. CS-3013 Operating Systems Hugh C. Lauer. CS-3013, C-Term 2012 Threads 1

Chapter 3: Processes. Operating System Concepts 8 th Edition,

Chapter 3: Processes. Operating System Concepts 8th Edition

Questions from last time

CSE 4/521 Introduction to Operating Systems

Chapter 4: Multithreaded Programming

Chapter 4: Multithreaded Programming

CS420: Operating Systems. Interprocess Communication

Chapter 3: Processes

Chapter 3: Processes. Operating System Concepts Essentials 8 th Edition

Threads. CS3026 Operating Systems Lecture 06

Chapter 3: Processes. Chapter 3: Processes. Process in Memory. Process Concept. Process State. Diagram of Process State

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

Chapter 5: Threads. Outline

Lecture 2 Process Management

CSMC 412. Operating Systems Prof. Ashok K Agrawala Ashok Agrawala Set 4. September 2006 CMSC 412 Set 4

CISC2200 Threads Spring 2015

Last class: Today: Thread Background. Thread Systems

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

Processes. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

Processes and More. CSCI 315 Operating Systems Design Department of Computer Science

Chapter 3: Process Concept

Chapter 3: Processes. Operating System Concepts 9 th Edition

Processes. Process Concept

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

Part V. Process Management. Sadeghi, Cubaleska RUB Course Operating System Security Memory Management and Protection

CS420: Operating Systems. OS Services & System Calls

Chapter 3: Processes. Operating System Concepts 8 th Edition,

Lecture Topics. Announcements. Today: Threads (Stallings, chapter , 4.6) Next: Concurrency (Stallings, chapter , 5.

Processes & Threads. Recap of the Last Class. Microkernel System Architecture. Layered Structure

Process Description and Control

Processes and Threads

Lecture 4 Threads. (chapter 4)

Chapter 3: Processes. Operating System Concepts Essentials 2 nd Edition

CS370 Operating Systems

CS370 Operating Systems

Chapter 3: Processes

CS420: Operating Systems. Kernel I/O Subsystem

Chapter 3: Process Concept

Chapter 3: Processes. Operating System Concepts 9 th Edit9on

Processes & Threads. Recap of the Last Class. Layered Structure. Virtual Machines. CS 256/456 Dept. of Computer Science, University of Rochester

CS370 Operating Systems

Definition Multithreading Models Threading Issues Pthreads (Unix)

4.8 Summary. Practice Exercises

SMD149 - Operating Systems

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

Chapter 3: Processes. Operating System Concepts 9 th Edition

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

CS420: Operating Systems

CSC 539: Operating Systems Structure and Design. Spring 2006

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - III Processes. Louisiana State University. Processes. September 1 st, 2009

THREADS. Jo, Heeseung

Questions answered in this lecture: CS 537 Lecture 19 Threads and Cooperation. What s in a process? Organizing a Process

CS370 Operating Systems

Thread. Operating Systems (Fall/Winter 2018) Yajin Zhou ( Zhejiang University

OPERATING SYSTEMS. UNIT II Sections A, B & D. An operating system executes a variety of programs:

Processes. Operating System Concepts with Java. 4.1 Sana a University, Dr aimen

What Is A Process? Process States. Process Concept. Process Control Block (PCB) Process State Transition Diagram 9/6/2013. Process Fundamentals

Transcription:

Threading Issues James Moscola Department of Engineering & Computer Science York College of Pennsylvania Based on Operating System Concepts, 9th Edition by Silberschatz, Galvin, Gagne

Threading Issues There are a variety of issues to consider with multithreaded programming - Semantics of fork() and exec() system calls - Thread cancellation Asynchronous or deferred - Signal handling Synchronous and asynchronous - Thread pooling - Thread-specific data Create facility needed for data private to thread 2

Semantics of fork() and exec() Recall that when fork() is called, a separate, duplicate process is created How should fork() behave in a multithreaded program? - Should all threads be duplicated? - Should only the thread that made the call to fork() be duplicated? In some systems, different versions of fork() exist depending on the desired behavior - Some UNIX systems have fork1() and forkall() fork1() only duplicates the calling thread forkall() duplicates all of the threads in a process - In a POSIX-compliant system, fork() behaves the same as fork1() 3

Semantics of fork() and exec() The exec() system call continues to behave as expected - Replaces the entire process that called it, including all threads If planning to call exec() after fork(), then there is no need to duplicate all of the threads in the calling process - All threads in the child process will be terminated when exec() is called - Use fork1(), rather than forkall() if using in conjunction with exec() 4

Thread Cancellation Thread cancellation is the act of terminating a thread before it has completed - Example - clicking the stop button on your web browser will stop the thread that is rendering the web page The thread to be cancelled is called the target thread Threads can be cancelled in a couple of ways - Asynchronous cancellation terminates the target thread immediately Thread may be in the middle of writing data... not so good - Deferred cancellation allows the target thread to periodically check if it should be cancelled Allows thread to terminate itself in an orderly fashion 5

Signal Handling Signals are used in UNIX systems to notify a process that a particular event has occurred - CTRL-C is an example of an asynchronous signal that might be sent to a process An asynchronous signal is one that is generated from outside the process that receives it - Divide by 0 is an example of a synchronous signal that might be sent to a process A synchronous signal is delivered to the same process that caused the signal to occur All signals follow the same basic pattern: - A signal is generated by particular event - The signal is delivered to a process - The signal is handled by a signal handler (all signals are handled exactly once) 6

Signal Handling Signal handling is straightforward in a single-threaded process - The one (and only) thread in the process receives and handles the signal In a multithreaded program, where should signals be delivered? - Options: (1) Deliver the signal to the thread to which the signal applies (2) Deliver the signal to every thread in the process (3) Deliver the signal only to certain threads in the process (4) Assign a specific thread to receive all signals for the process 7

Signal Handling Option 1 - Deliver the signal to the thread to which the signal applies - Most likely option when handling synchronous signals (e.g. only the thread that attempts to divide by zero needs to know of the error) Option 2 - Deliver the signal to every thread in the process - Likely to be used in the event that the process is being terminated (e.g. a CTRL- C is sent to terminate the process, all threads need to receive this signal and terminate) 8

Thread Pools In applications where threads are repeatedly being created/destroyed thread pools might provide a performance benefit - Example: A server that spawns a new thread each time a client connects to the system and discards that thread when the client disconnects A thread pool is a group of threads that have been pre-created and are available to do work as needed - Threads may be created when the process starts - A thread may be kept in a queue until it is needed - After a thread finishes, it is placed back into a queue until it is needed again - Avoids the extra time needed to spawn new threads when they re needed 9

Thread Pools Advantages of thread pools: - Typically faster to service a request with an existing thread than create a new thread (performance benefit) - Bounds the number of threads in a process The only threads available are those in the thread pool If the thread pool is empty, then the process must wait for a thread to re-enter the pool before it can assign work to a thread Without a bound on the number of threads in a process, it is possible for a process to create so many threads that all of the system resources are exhausted 10

Thread-Specific Data Thread-specific data - in some applications it may be useful for each thread to have its own copy of data - May also be referred to as Thread-local storage or Thread-static variables - The errno variable is thread-specific // In C# class FooBar { [ThreadStatic] static int foo; } // In Java private static ThreadLocal<Integer> threadlocalint = new ThreadLocal<Integer>(); // In a POSIX-compliant system // see pthread_key_create() // pthread_setspecific() // pthread_getspecific() 11

Thread Examples - Windows XP Implements threads using the one-to-one thread model Also implements a fiber that uses a many-to-many model - A fiber is a unit of execution that must be manually scheduled by the application Each thread contains - A thread id - Register set - Separate user and kernel stacks - Private data storage area 12

Linux Threads Linux oftentimes uses the term task rather than process or thread Thread creation is done through the clone() system call - The clone() can create either threads or processes depending on the options passed to clone() The options passed to clone determine how much sharing is taking place between the parent and the child - A process can still be created using the fork() system call - Provided a specific set of options and the clone() and fork() systems calls behave identically 13

Linux Threads (Cont.) The following table shows the various flags that can be passed to clone to determine how much sharing is taking place between the parent and the child 14