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

Similar documents
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

Multi-core Programming System Overview

Processes and Non-Preemptive Scheduling. Otto J. Anshus

ELEC 377. Operating Systems. Week 1 Class 3

Kernel comparison of OpenSolaris, Windows Vista and. Linux 2.6

Operating System: Scheduling

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

An Implementation Of Multiprocessor Linux

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

Shared Address Space Computing: Programming

First-class User Level Threads

Kernel Types System Calls. Operating Systems. Autumn 2013 CS4023

Chapter 6, The Operating System Machine Level

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

Distributed File Systems

Scheduling. Scheduling. Scheduling levels. Decision to switch the running process can take place under the following circumstances:

CS161: Operating Systems

Thomas Fahrig Senior Developer Hypervisor Team. Hypervisor Architecture Terminology Goals Basics Details

Page 1 of 5. IS 335: Information Technology in Business Lecture Outline Operating Systems

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

Process Description and Control william stallings, maurizio pizzonia - sistemi operativi

Performance Comparison of RTOS

CPU Scheduling Outline

Example of Standard API

Multi-Threading Performance on Commodity Multi-Core Processors

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

POSIX. RTOSes Part I. POSIX Versions. POSIX Versions (2)

Chapter 5 Process Scheduling

Testing Database Performance with HelperCore on Multi-Core Processors

Real Time Programming: Concepts

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

The Native POSIX Thread Library for Linux

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

CHAPTER 1 INTRODUCTION

Symmetric Multiprocessing

General Introduction

COS 318: Operating Systems. Virtual Machine Monitors

Multiprocessor Scheduling and Scheduling in Linux Kernel 2.6

CS420: Operating Systems OS Services & System Calls

Chapter 5 Cloud Resource Virtualization

Road Map. Scheduling. Types of Scheduling. Scheduling. CPU Scheduling. Job Scheduling. Dickinson College Computer Science 354 Spring 2010.

Web Server Architectures

Control 2004, University of Bath, UK, September 2004

Automatic Logging of Operating System Effects to Guide Application-Level Architecture Simulation

This tutorial will take you through step by step approach while learning Operating System concepts.

Performance Evaluation and Optimization of A Custom Native Linux Threads Library

Windows8 Internals, Sixth Edition, Part 1

Module 8. Industrial Embedded and Communication Systems. Version 2 EE IIT, Kharagpur 1

Embedded Systems. 6. Real-Time Operating Systems

Introducing PgOpenCL A New PostgreSQL Procedural Language Unlocking the Power of the GPU! By Tim Child

CS222: Systems Programming

Operating System Tutorial

The Linux Kernel: Process Management. CS591 (Spring 2001)

Operating System Impact on SMT Architecture

Lecture 25 Symbian OS

Chapter 2 System Structures

Linux Kernel Architecture

Deciding which process to run. (Deciding which thread to run) Deciding how long the chosen process can run

Resource Containers: A new facility for resource management in server systems

Linux Process Scheduling Policy

COS 318: Operating Systems

Enabling Technologies for Distributed and Cloud Computing

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

Distributed Operating Systems. Cluster Systems

Course Development of Programming for General-Purpose Multicore Processors

OPERATING SYSTEM SERVICES

Job Scheduling Model

Chapter 5: CPU Scheduling. Operating System Concepts 8 th Edition

Real-Time Scheduling 1 / 39

Chapter 1 Computer System Overview

Operatin g Systems: Internals and Design Principle s. Chapter 10 Multiprocessor and Real-Time Scheduling Seventh Edition By William Stallings

Computer Science 4302 Operating Systems. Student Learning Outcomes

System Structures. Services Interface Structure

Industry First X86-based Single Board Computer JaguarBoard Released

Achieving Nanosecond Latency Between Applications with IPC Shared Memory Messaging

How To Understand How A Process Works In Unix (Shell) (Shell Shell) (Program) (Unix) (For A Non-Program) And (Shell).Orgode) (Powerpoint) (Permanent) (Processes

COS 318: Operating Systems. Virtual Machine Monitors

A Deduplication File System & Course Review

Why Threads Are A Bad Idea (for most purposes)

An examination of the dual-core capability of the new HP xw4300 Workstation

Operating Systems Concepts: Chapter 7: Scheduling Strategies

A Survey of Parallel Processing in Linux

Chapter 3 Operating-System Structures

CSC 2405: Computer Systems II

Lecture 6: Interrupts. CSC 469H1F Fall 2006 Angela Demke Brown

Virtualization Technologies and Blackboard: The Future of Blackboard Software on Multi-Core Technologies

Replication on Virtual Machines

Linux scheduler history. We will be talking about the O(1) scheduler

Lecture 11: Multi-Core and GPU. Multithreading. Integration of multiple processor cores on a single chip.

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

CPU Scheduling. Core Definitions

Operating System Components

Multi-core and Linux* Kernel

PikeOS: Multi-Core RTOS for IMA. Dr. Sergey Tverdyshev SYSGO AG , Moscow

Chapter 1 8 Essay Question Review

CS414 SP 2007 Assignment 1

Transcription:

Operating Systems 05. Threads Paul Krzyzanowski Rutgers University Spring 2015 February 9, 2015 2014-2015 Paul Krzyzanowski 1

Thread of execution Single sequence of instructions Pointed to by the program counter (PC) Executed by the processor Conventional programming model & OS structure: Single threaded One process = one thread February 9, 2015 2014-2015 Paul Krzyzanowski 2

Multi-threaded model A thread is a subset of a process: A process contains one or more kernel threads Share memory and open files BUT: separate program counter, registers, and stack Shared memory includes the heap and global/static data No memory protection among the threads Preemptive multitasking: Operating system preempts & schedules threads stack 1 stack 2 heap data+bss text February 9, 2015 2014-2015 Paul Krzyzanowski 3

Sharing Threads share: Text segment (instructions) Data segment (static and global data) BSS segment (uninitialized data) Open file descriptors Signals Current working directory User and group IDs Threads do not share: Thread ID Saved registers, stack pointer, instruction pointer Stack (local variables, temporary variables, return addresses) Signal mask Priority (scheduling information) February 9, 2015 2014-2015 Paul Krzyzanowski 4

Why is this good? Threads are more efficient Much less overhead to create: no need to create new copy of memory space, file descriptors, etc. Sharing memory is easy (automatic) No need to figure out inter-process communication mechanisms Take advantage of multiple CPUs just like processes Program scales with increasing # of CPUs Take advantage of multiple cores February 9, 2015 2014-2015 Paul Krzyzanowski 5

Implementation Process info (Process Control Block) contains one or more Thread Control Blocks (TCB): Thread ID Saved registers Other per-thread info (signal mask, scheduling parameters) PCB PCB PCB TCB TCB TCB TCB TCB TCB February 9, 2015 2014-2015 Paul Krzyzanowski 6

Scheduling A thread-aware operating system scheduler schedules threads, not processes A process is just a container for one or more threads February 9, 2015 2014-2015 Paul Krzyzanowski 7

Scheduling Challenges Scheduler has to realize: Context switch among threads of different processes is more expensive Flush cache memory (or have memory with process tags) Flush virtual memory TLB (or have tagged TLB) Replace page table pointer in memory management unit CPU Affinity Rescheduling threads onto a different CPU is more expensive The CPU s cache may have memory used by the thread cached Try to reschedule the thread onto the same processor on which it last ran February 9, 2015 2014-2015 Paul Krzyzanowski 8

Process vs. Thread context switch A thread switch within the same process is not a full context switch the address space (memory map) does not get switched linux/arch/i386/kernel/process.c: /* Re-load page tables for a new address space */ { unsigned long new_cr3 = next->tss.cr3; if (new_cr3!= prev->tss.cr3) asm volatile("movl %0,%%cr3": :"r" (new_cr3)); } This statement tests if the new task has the same memory map as the current one. If so, we re switching threads and will not run the instruction to switch the memory mapping tables February 9, 2015 2014-2015 Paul Krzyzanowski 9

Multi-threaded programming patterns Single task thread Do a specific job and then release the thread Worker threads Specific task for each worker thread Dispatch task to the thread that handles it Thread pools Create a pool of threads a priori Use an existing thread to perform a task; wait if no threads available Common model for servers February 9, 2015 2014-2015 Paul Krzyzanowski 10

Kernel-level threads vs. User-level threads Kernel-level Threads supported by operating system OS handles scheduling, creation, synchronization User-level Library with code for creation, termination, scheduling Kernel sees one execution context: one process May or may not be preemptive February 9, 2015 2014-2015 Paul Krzyzanowski 11

User-level threads Advantages Low-cost: user level operations that do not require switching to the kernel Scheduling algorithms can be replaced easily & custom to app Greater portability Disadvantages If a thread is blocked, all threads for the process are blocked Every system call needs an asynchronous counterpart Cannot take advantage of multiprocessing February 9, 2015 2014-2015 Paul Krzyzanowski 12

You can have both User-level thread library on top of multiple kernel threads 1:1 kernel threads only (1 user thread = 1 kernel thread N:1 user threads only (N user threads on 1 kernel thread/process) N:M hybrid threading (N user threads on M kernel threads) February 9, 2015 2014-2015 Paul Krzyzanowski 13

pthreads: POSIX Threads POSIX.1c, Threads extensions (IEEE Std 1003.1c-1995) Defines API for managing threads Linux: native POSIX Thread Library Also on Solaris, Mac OS X, NetBSD, FreeBSD API library on top of Win32 February 9, 2015 2014-2015 Paul Krzyzanowski 14

Using POSIX Threads Create a thread p t h r e a d _ t t ; p t h r e a d _ c r e a t e ( & t, N U L L, f u n c, a r g ) Create new thread t Start executing function func(arg) Join two threads (wait for thread t to terminate) v o i d * r e t _ v a l ; p t h r e a d _ j o i n ( t, & r e t _ v a l ) ; Thread t exits via return or pthread_exit No parent/child relationship among threads! Any one thread may wait (join) on another thread February 9, 2015 2014-2015 Paul Krzyzanowski 15

A different approach to threads Threads force a highly-shared model Memory map, signals, open files, current directory, etc. all shared Processes force a non-shared model Separate memory, open files, etc. What if we allow the user to specify what is shared when a new process is created? Then we don t need threads since processes can share all memory if they want to and open files and anything else February 9, 2015 2014-2015 Paul Krzyzanowski 16

Linux threads Linux has no concept of a thread All threads implemented as standard processes No special scheduling semantics All processes defined in the kernel by task_struct Support thread-like behavior via clone system call Designed to implement threads A process can control what gets shared with a new process Based on Plan 9 s rfork system call February 9, 2015 2014-2015 Paul Krzyzanowski 17

Linux clone() system call Clone a process, like fork, but: Specify function that the child will run (with argument) Child terminates when the function returns Specify location of the stack for the child Specify what s shared: Share memory (otherwise memory writes use new memory) Share open file descriptor table Share the same parent Share root directory, current directory, and permissions mask Share namespace (mount points creating a directory hierarchy) Share signals And more Used by pthreads February 9, 2015 2014-2015 Paul Krzyzanowski 18

Threading in hardware Hyper-Threading (HT) vs. Multi-core vs. Multi-processor One core = One CPU Hyper-Threading One physical core appears to have multiple processors Looks like multiple CPUs to the OS Separate registers & execution state Multiple threads run but compete for execution unit Events in the pipeline switch between the streams Threads do not have to belong to the same process But the processors share the same cache Performance can degrade if two threads compete for the cache Works well with instruction streams that have large memory latencies February 9, 2015 2014-2015 Paul Krzyzanowski 19

20.2 mm Example CPU Intel Core i7-5960x Extreme Edition (Haswell-E) 3.0 GHz up to 3.5 GHz (Turbo) 2.6B 22 nm Tri-Gate 3-D transistors 8 cores; 16 threads 17.6 mm Per-Core caches: 512 KB L1 cache (128 KB data; 128 KB instruction) 2 MB L2 cache 20 MB shared L3 cache February 9, 2015 2014-2015 Paul Krzyzanowski 20

Multi-core architecture CPU Core CPU Core CPU Core CPU Core CPU Core CPU Core Sample Sizes (Sandy Bridge) L1 cache L1 cache L1 cache L1 cache L1 cache L1 cache 64 KB L2 cache L2 cache L2 cache L2 cache L2 cache L2 cache 256 KB Shared L3 cache L3 cache (shared) 12 MB CPU Die external memory February 9, 2015 2014-2015 Paul Krzyzanowski 21

Stepping on each other Threads share the same data Mutual exclusion is critical Allow a thread be the only one to grab a critical section Others who want it go to sleep pthread_mutex_t = m = PTHREAD_MUTEX_INITIALIZER;... pthread_mutex_lock(&m); /* modify shared data */ pthread_mutex_unlock(&m); February 9, 2015 2014-2015 Paul Krzyzanowski 22

The End February 9, 2015 2014-2015 Paul Krzyzanowski 23