CSE 4/521 Introduction to Operating Systems

Similar documents
Chapter 4: Threads. Chapter 4: Threads

CS420: Operating Systems

Chapter 4: Threads. Chapter 4: Threads

Chapter 4: Multithreaded Programming

OPERATING SYSTEM. Chapter 4: Threads

Chapter 4: Threads. Operating System Concepts 9 th Edition

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

Chapter 4: Threads. Operating System Concepts 9 th Edition

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

Chapter 4: Multi-Threaded Programming

CS 450 Operating System Week 4 Lecture Notes

CS307: Operating Systems

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

Chapter 4: Multithreaded Programming

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

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

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

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

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

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

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

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

Chapter 4: Multithreaded

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

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

Concurrency, Thread. Dongkun Shin, SKKU

Chapter 4: Multithreaded Programming

Chapter 4: Multithreaded Programming

Processes and Threads

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

Chapter 4: Threads. Operating System Concepts 8 th Edition,

Chapter 3: Processes. Operating System Concepts 8th Edition

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

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

CSE Opera,ng System Principles

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

Processes and Threads

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

Multithreaded Programming

CS370 Operating Systems

Questions from last time

Chapter 4 Multithreaded Programming

Lecture 4 Threads. (chapter 4)

Operating Systems Prof. Ashok K Agrawala

Chapter 5: Threads. Single and Multithreaded Processes

4.8 Summary. Practice Exercises

Threads. Thread Concept Multithreading Models User & Kernel Threads Pthreads Threads in Solaris, Linux, Windows. 2/13/11 CSE325 - Threads 1

Chapter 5: Threads. Outline

Chapter 4: Threads. Operating System Concepts 9 th Edition

Process Description and Control

CS370 Operating Systems

CS370 Operating Systems

Motivation. Threads. Multithreaded Server Architecture. Thread of execution. Chapter 4

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

TDIU25: Operating Systems II. Processes, Threads and Scheduling

CISC2200 Threads Spring 2015

CS370 Operating Systems

CSE 4/521 Introduction to Operating Systems

Lecture 2 Process Management

Process Concept Process in Memory Process State new running waiting ready terminated Diagram of Process State

CHAPTER 2: PROCESS MANAGEMENT

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

CS370 Operating Systems

Threads. studykorner.org

Chapter 4: Processes. Process Concept

Chapter 4: Processes

Threads. CS3026 Operating Systems Lecture 06

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

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

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

Chapter 3: Processes

Threads Implementation. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

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

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

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

CSE 153 Design of Operating Systems Fall 2018

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

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

Semantics of fork() and exec()

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

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

Lecture Contents. 1. Overview. 2. Multithreading Models 3. Examples of Thread Libraries 4. Summary

Lecture 5: Process Description and Control Multithreading Basics in Interprocess communication Introduction to multiprocessors

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

ICS Principles of Operating Systems

Last Class: CPU Scheduling. Pre-emptive versus non-preemptive schedulers Goals for Scheduling: CS377: Operating Systems.

Chapter 3 Process Description and Control

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

CS420: Operating Systems

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

Chapter 3: Processes. Operating System Concepts 9 th Edit9on

From Processes to Threads

Process and Its Image An operating system executes a variety of programs: A program that browses the Web A program that serves Web requests

COP 4610: Introduction to Operating Systems (Spring 2014) Chapter 3: Process. Zhi Wang Florida State University

Processes and Threads. Processes: Review

Chapter 3: Process Concept

COP 4610: Introduction to Operating Systems (Spring 2016) Chapter 3: Process. Zhi Wang Florida State University

Chapter 3: Process Concept

Transcription:

CSE 4/521 Introduction to Operating Systems Lecture 5 Threads (Overview, Multicore Programming, Multithreading Models, Thread Libraries, Implicit Threading, Operating- System Examples) Summer 2018

Overview Objective: 1. To introduce the notion of a thread 2. Examine issues related to multicore programming, and multithreaded models 3. Discuss implicit threading strategies 4. To cover operating system support for threads on Linux and Windows Overview Multicore Programming Multithreaded Models Thread Libraries Implicit Threading Operating-System Examples 2

Recap Process Concept Structure of process, state of process, PCB Process Scheduling Scheduling queues, long/mid/short-term schedulers, context switch Operations on Processes Process creation (fork()), process termination Interprocess Communication Shared memory, message passing, producer-consumer problem Examples of IPC Systems POSIX, Mach, and Hybrid 3

Questions 1. What is the return address of fork()for child process and parent process? (Easy) 2. Why should a parent process wait() for a child process? (Medium) 3. What is the output at LINE A? (Hard) #include <unistd.h> int value = 5; int main() { pid_t pid; pid = fork(); if (pid == 0) { value += 15; return 0; } else if (pid > 0) { wait(null); printf( PARENT: value = %d, value); return 0; } } /*LINE A*/ 4

Table of Content Overview Multicore Programming Multithreaded Models Thread Libraries Implicit Threading Operating-System Examples 5

Table of Content Overview Multicore Programming Multithreaded Models Thread Libraries Implicit Threading Operating-System Examples 6

Overview 7

Overview Process creation is heavy-weight while thread creation is light-weight Kernels are generally multithreaded Example for threads: Multiple tasks within the application can be implemented by separate threads. In web-browser: Update display Fetch data Spell checking Answer a network request 8

Overview Benefits of Threads Responsiveness may allow continued execution if part of process is blocked, especially important for user interfaces Resource Sharing threads share resources of process, easier than shared memory or message passing Economy cheaper than process creation, thread switching lower overhead than context switching Scalability process can take advantage of multiprocessor architectures 9

Table of Content Overview Multicore Programming Multithreaded Models Thread Libraries Implicit Threading Operating-System Examples 10

Multicore Programming Multicore or multiprocessor systems putting pressure on programmers, challenges include: Dividing activities Balance Testing and debugging Parallelism implies a system can perform more than one task simultaneously Concurrency supports more than one task making progress Single processor / core, scheduler providing concurrency 11

Multicore Programming Concurrent execution on single-core system: Parallelism on a multi-core system: 12

Table of Content Overview Multicore Programming Multithreaded Models Thread Libraries Implicit Threading Operating-System Examples 13

Multithreaded Models User threads and Kernel threads User threads - management done by user-level threads library Three primary thread libraries: POSIX Pthreads Windows threads Java threads Kernel threads - Supported by the Kernel Examples virtually all general purpose operating systems: Windows Solaris Linux Tru64 UNIX Mac OS X 14

Multithreaded Models 3 types: Many-to-One One-to-One Many-to-Many 15

Multithreaded Models : Many-to- One Many user-level threads mapped to single kernel thread One thread blocking causes all to block Multiple threads may not run in parallel on multicore system because only one may be in kernel at a time Few systems currently use this model Examples: Solaris Green Threads GNU Portable Threads 16

Multithreaded Models : One-to- One Each user-level thread maps to unique kernel thread Creating a user-level thread creates a kernel thread More concurrency than manyto-one Number of threads per process sometimes restricted due to overhead Examples Windows Linux Solaris 9 and later 17

Multithreaded Models : Many-to- Many Allows many user-level threads to be mapped to many kernel threads Allows the operating system to create a sufficient number of kernel threads Examples Solaris prior to version 9 18

Multithreaded Models: Two-level Similar to M:M, except that it allows a user thread to be bound to kernel thread Examples IRIX HP-UX Tru64 UNIX Solaris 8 and earlier 19

Table of Content Overview Multicore Programming Multithreaded Models Thread Libraries Implicit Threading Operating-System Examples 20

Thread Libraries Thread library provides programmer with API for creating and managing threads Two primary ways of implementing Library entirely in user space Kernel-level library supported by the OS Examples: Pthreads Java threads 21

Thread Libraries : Pthreads Pthreads in UNIX operating systems (Solaris, Linux, Mac OS X) 22

Thread Libraries : Pthreads Pthread code for joining 10 threads 23

Thread Libraries : Java Threads Java threads are managed by the JVM Typically implemented using the threads model provided by underlying OS Java threads may be created by: Extending Thread class Implementing the Runnable interface 24

Table of Content Overview Multicore Programming Multithreaded Models Thread Libraries Implicit Threading Operating-System Examples 25

Implicit Threading Creation and management of threads done by compilers and run-time libraries rather than programmers 2 methods explored Thread Pools OpenMP Other methods include Microsoft Threading Building Blocks (TBB), java.util.concurrent package 26

Implicit Threading : Thread Pools Create a number of threads in a pool where they await work Advantages: Faster to service a request with an existing thread than create a new thread Allows the number of threads in the application(s) to be bound to the size of the pool Windows API supports thread pools: 27

Implicit Threading : OpenMP Set of compiler directives and an API for C, C++, FORTRAN Provides support for parallel programming in shared-memory environments Identifies parallel regions blocks of code that can run in parallel #pragma omp parallel Creates as many threads as there are cores #pragma omp parallel for for(i=0;i<n;i++) { c[i] = a[i] + b[i]; } Above code runs for loop in parallel 28

Table of Content Overview Multicore Programming Multithreaded Models Thread Libraries Implicit Threading Operating-System Examples 29

Operating-System Examples Windows Threads Linux Threads 30

Operating-System Examples : Windows Threads Implements the one-to-one, kernel-level mapping. The primary data structures of a thread include: ETHREAD (executive thread block) includes pointer to process to which thread belongs and to KTHREAD, in kernel space KTHREAD (kernel thread block) scheduling and synchronization info, kernel-mode stack, pointer to TEB, in kernel space TEB (thread environment block) thread id, user-mode stack, threadlocal storage, in user space 31

Operating-System Examples : Linux Threads Thread creation is done through clone()system call clone()allows a child task to share the address space of the parent task (process) Flags control behavior (unlike fork() where behavior cannot be controlled) 32

Credential for slides Silberschatz, Galvin and Gagne 33