Chapter 4: Threads. Operating System Concepts 8th Edition, modified by Stewart Weiss

Similar documents
Operating Systems. 05. Threads. Paul Krzyzanowski. Rutgers University. Spring 2015

Kernel comparison of OpenSolaris, Windows Vista and. Linux 2.6

Objectives. Chapter 2: Operating-System Structures. Operating System Services (Cont.) Operating System Services. Operating System Services (Cont.

Chapter 6, The Operating System Machine Level

Multi-core Programming System Overview

Kernel Types System Calls. Operating Systems. Autumn 2013 CS4023

CS420: Operating Systems OS Services & System Calls

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

OPERATING SYSTEM SERVICES

CS 377: Operating Systems. Outline. A review of what you ve learned, and how it applies to a real operating system. Lecture 25 - Linux Case Study

System Structures. Services Interface Structure

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

CSE 544 Principles of Database Management Systems. Magdalena Balazinska Fall 2007 Lecture 5 - DBMS Architecture

Why Threads Are A Bad Idea (for most purposes)

Chapter 3 Operating-System Structures

Operating Systems for Parallel Processing Assistent Lecturer Alecu Felician Economic Informatics Department Academy of Economic Studies Bucharest

Processes and Non-Preemptive Scheduling. Otto J. Anshus

CHAPTER 1 INTRODUCTION

Operating System Components

Lecture 1 Operating System Overview

Chapter 2 System Structures

Web Server Architectures

First-class User Level Threads

How do Users and Processes interact with the Operating System? Services for Processes. OS Structure with Services. Services for the OS Itself

Operating System: Scheduling

Multi-core architectures. Jernej Barbic , Spring 2007 May 3, 2007

Performance Comparison of RTOS

PART IV Performance oriented design, Performance testing, Performance tuning & Performance solutions. Outline. Performance oriented design

ò Paper reading assigned for next Thursday ò Lab 2 due next Friday ò What is cooperative multitasking? ò What is preemptive multitasking?

Operating System Structures

How To Write A Windows Operating System (Windows) (For Linux) (Windows 2) (Programming) (Operating System) (Permanent) (Powerbook) (Unix) (Amd64) (Win2) (X

Chapter 2: OS Overview

CS3600 SYSTEMS AND NETWORKS

Real-Time Systems Prof. Dr. Rajib Mall Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Components of a Computing System. What is an Operating System? Resources. Abstract Resources. Goals of an OS. System Software

Building Applications Using Micro Focus COBOL

Operating Systems. Rafael Ramirez (T, S)

Shared Address Space Computing: Programming

Overview of Operating Systems Instructor: Dr. Tongping Liu

Operating System Overview. Otto J. Anshus

ODROID Multithreading in Android

Lesson Objectives. To provide a grand tour of the major operating systems components To provide coverage of basic computer system organization

Operating System Tutorial

How To Understand The History Of An Operating System

1. Computer System Structure and Components

Effective Computing with SMP Linux

The Xen of Virtualization

IBM Software Group. Lotus Domino 6.5 Server Enablement

Operating Systems OBJECTIVES 7.1 DEFINITION. Chapter 7. Note:

ELEC 377. Operating Systems. Week 1 Class 3

The Native POSIX Thread Library for Linux

Making Multicore Work and Measuring its Benefits. Markus Levy, president EEMBC and Multicore Association

Multi-core and Linux* Kernel

BLM 413E - Parallel Programming Lecture 3

Tools Page 1 of 13 ON PROGRAM TRANSLATION. A priori, we have two translation mechanisms available:

Programming real-time systems with C/C++ and POSIX

Example of Standard API

Final Report. Cluster Scheduling. Submitted by: Priti Lohani

Real Time Programming: Concepts

Intel DPDK Boosts Server Appliance Performance White Paper

Introduction. System Calls

Operating Systems OS Architecture Models

Linux Process Scheduling Policy

Chapter 16: Virtual Machines. Operating System Concepts 9 th Edition

Chapter 5: System Software: Operating Systems and Utility Programs

Operating Systems and Networks

Web Server Software Architectures

Distributed Operating Systems. Cluster Systems

SYSTEM ecos Embedded Configurable Operating System

Operating System Software

CS161: Operating Systems

A Comparative Study on Vega-HTTP & Popular Open-source Web-servers

Outline: Operating Systems

Real-time Process Network Sonar Beamformer

Chapter 3: Operating-System Structures. Common System Components

COS 318: Operating Systems

Chapter 3: Operating-System Structures. System Components Operating System Services System Calls System Programs System Structure Virtual Machines

Why Computers Are Getting Slower (and what we can do about it) Rik van Riel Sr. Software Engineer, Red Hat


Red Hat Linux Internals

Parallel Firewalls on General-Purpose Graphics Processing Units

Networking Operating Systems (CO32010)

CSC 2405: Computer Systems II

Using Power to Improve C Programming Education

PERFORMANCE ENHANCEMENTS IN TreeAge Pro 2014 R1.0

Introduction to Cloud Computing

So#ware Tools and Techniques for HPC, Clouds, and Server- Class SoCs Ron Brightwell

Chapter 1 8 Essay Question Review

Multilevel Load Balancing in NUMA Computers

CSE 120 Principles of Operating Systems. Modules, Interfaces, Structure

OPERATING SYSTEMS STRUCTURES

1 Organization of Operating Systems

Process Scheduling CS 241. February 24, Copyright University of Illinois CS 241 Staff

Threads Scheduling on Linux Operating Systems

Transcription:

Chapter 4: Threads,

Chapter 4: Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Linux Threads 4.2

Objectives To introduce the notion of a thread a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems To discuss the APIs for the Pthreads thread library To examine issues related to multithreaded programming 4.3

Why? Context switches are expensive entire context must be copied (slow!!) Sometimes new process needs to share part of context of parent, so it is wasteful to copy the entire context Threads were invented to overcome these two problems. Example: a concurrent web server is one that creates a new process each time a client attempts a connection. A traditional server would fork a new process with its own address space, slowing things down. A multi threaded server would fork a thread, much faster, that could use parent's context. 4.4

What is a Thread? A thread is a basic unit of CPU utilization consisting of a program counter, register set, a stack., local data, and other thread specific properties. It is also called a lightweight process (LWP). In contrast, a traditional process is a heavyweight process. Threads can share the context of their parents, and hence siblings can share context, and thus a whole process tree can share it. E.g. can share code, variables, open files, data. The collection of all threads is called a task. 4.5

Single and Multithreaded Processes 4.6

Benefits Responsiveness: multithreading an application increases responsiveness. One thread can issue I/O request and be blocked while another does computation. While data loads, another user request may be satisfied. Resource Sharing: threads share memory and resources without special OS primitives like message passing or shared memory system calls, making it easier and faster. BUT puts burden on programer to prevent race conditions (Chapter 6) Economy: much faster to create a thread than a process; also because resources are shared, fewer resources are used in total. Scalability: threads can be scheduled on separate processors when they are available, making execution faster. So can processes, but they require more resources, so mroe threads can be scheduled than processors, when the number of processors is very large. 4.7

Comparison with Processes The differences between a thread and a process: Exists within a process and uses the process resources Has its own independent flow of control as long as its parent process exists and the OS supports it Duplicates only the resources it needs to be independently schedulable May share process resources with other threads that act equally independently (and dependently) Is terminated if the parent process dies 4.8

Multicore Programming A multicore computer has multiple processors on a single chip. Each core is an independent CPU. An operating system has greater challenges when using a multicore system, including Multiprocessor scheduling: deciding which CPU to put each process on Load Balancing : deciding how to rebalance queues to make sure each CPU is kept equally busy Cache and Data Coherence: when processes move from one CPU to another, making that caches and data remain consistent and coherent 4.9

Concurrent Execution Threads serialized on Single Core System Threads executed in parallel on Two Core System 4.10

Multicore Programming Multicore systems putting pressure on application programmers also; challenges include Dividing activities: finding tasks in applications that can run concurrently Balance: trying to balance the amount of work allocated to each thread of execution Data splitting: dividing up the data into that which is global and that which is local to each thread Data dependency: analyzing which data will be accessed by multiple threads in order to decide how to synchronize access to that data Testing and debugging: testing multi threaded programs is much harder because executions are not repeatable. 4.11

User Threads There are two types of threads: user threads and kernel threads. User threads are supported by user level libraries outside of the kernel and do not need calls to the OpSys for switching. Easier to implement cooperation among concurrent processes with user threads Disadvantage: If kernel is single threaded and a user thread makea system call, entire task is blocked. Three primary user level thread libraries: POSIX Pthreads Win32 threads Java threads 4.12

Kernel Threads Supported by the Kernel: kernel manages creation, deletion, and all thread operations. Examples Windows XP/2000 Solaris Linux Tru64 UNIX Mac OS X 4.13

Multithreading Models User threads are mapped onto kernel threads. Many to One: many user threads per kernel thread One to One: one user thread per kernel thread Many to Many: user threads multiplexed across multiple kernel threads 4.14

Many-to-One Many user level threads mapped to single kernel thread Examples: Solaris Green Threads GNU Portable Threads 4.15

Many-to-One Model 4.16

Many-to-One: Pros & Cons Pros Efficient and fast Cons If any one of the threads mapping to the same kernel thread makes a blocking call, all threads of the group are blocked because the kernel thread gets blocked. It is not true concurrency; only one user thread per kernel thread can run at a time. 4.17

One-to-One Each user level thread maps to kernel thread Examples Windows NT/XP/2000 Linux Solaris 9 and later 4.18

One-to-one Model 4.19

One-to-One: Pros & Cons Pros: More concurrency because each user thread can run even if others are blocked Cons More overhead in OpSys 4.20

Many-to-Many Model Allows many user level threads to be mapped to many kernel threads Allows the operating system to create a sufficient number of kernel threads Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package 4.21

Many-to-Many Model 4.22

Many-to-Many Model: Pros and Cons Pros When a user thread blocks, the kernel thread that it was running on is blocked, but other user threads can be scheduled on other kernel threads. The degree of concurrency is equal to the number of kernel threads. Cons Greater complexity in the kernel. 4.23

Two-level Model Similar to M:M, except that it allows a user thread to be bound to kernel thread also, so it is both one one and many many. Examples IRIX HP UX Tru64 UNIX Solaris 8 and earlier 4.24

Two-level Model (2) 4.25

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 4.26

Pthreads May be provided either as user level or kernel level A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization API specifies behavior of the thread library, implementation is up to development of the library Common in UNIX operating systems (Solaris, Linux, Mac OS X) 4.27

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 4.28

Threading Issues Semantics of fork() and exec() system calls: If a thread in a multithreaded program calls fork() are all threads duplicated, or just one? (Linux duplicates just the calling thread) If a thread calls an exec() call, the entire task is replaced, including all other threads. Thread cancellation of target thread (Cancellation means termination.) Asynchronous or deferred: asynchronous terminated by some other thread unpredictably deferred like polling; thread checks periodically whether to terminate Issue is how to reclaim resources in an asynchronous cancellation 4.29

Example: Linux Threads Linux provides clone() to create threads, tkill() to send signals to or kill them, and a few other system calls to mage their resources. The clone() call is passed parameters that specify how the thread is to be created: who is the parent, what resources are shared, etc. Examples of those parameters: 4.30

Linux Threads continued Linux refers to them as tasks rather than threads clone() allows a child task to share the address space of the parent task (process) 4.31

End of Chapter 4,