ECE 598 Advanced Operating Systems Lecture 23

Similar documents
ECE 598 Advanced Operating Systems Lecture 11

ECE 574 Cluster Computing Lecture 8

ECE 598 Advanced Operating Systems Lecture 22

CS333 Intro to Operating Systems. Jonathan Walpole

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

CS510 Operating System Foundations. Jonathan Walpole

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

POSIX threads CS 241. February 17, Copyright University of Illinois CS 241 Staff

pthreads CS449 Fall 2017

Definition Multithreading Models Threading Issues Pthreads (Unix)

Multithreaded Programming

Threads. studykorner.org

CS 3305 Intro to Threads. Lecture 6

CISC2200 Threads Spring 2015

CS 326: Operating Systems. Process Execution. Lecture 5

Lecture 4 Threads. (chapter 4)

CPSC 341 OS & Networks. Threads. Dr. Yingwu Zhu

Multiprocessors 2007/2008

Processes and Threads

Last class: Today: Thread Background. Thread Systems

Inter-Process Communication

CS510 Operating System Foundations. Jonathan Walpole

Thread. Disclaimer: some slides are adopted from the book authors slides with permission 1

Overview. Administrative. * HW 2 Grades. * HW 3 Due. Topics: * What are Threads? * Motivating Example : Async. Read() * POSIX Threads

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

CSci 4061 Introduction to Operating Systems. (Threads-POSIX)

Threaded Programming. Lecture 9: Alternatives to OpenMP

A process. the stack

Threads. To do. Why threads? Thread model & implementation. q q q. q Next time: Synchronization

Operating Systems. VI. Threads. Eurecom. Processes and Threads Multithreading Models

Threads. lightweight processes

Processes. Johan Montelius KTH

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

Chapter 10. Threads. OS Processes: Control and Resources. A process as managed by the operating system groups together:

High Performance Computing Course Notes Shared Memory Parallel Programming

Threads. Today. Next time. Why threads? Thread model & implementation. CPU Scheduling

pthreads Announcement Reminder: SMP1 due today Reminder: Please keep up with the reading assignments (see class webpage)

Pre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012

Unix. Threads & Concurrency. Erick Fredj Computer Science The Jerusalem College of Technology

CSE 153 Design of Operating Systems Fall 2018

Threads. CS3026 Operating Systems Lecture 06

CS533 Concepts of Operating Systems. Jonathan Walpole

CS370 Operating Systems

Concurrency, Thread. Dongkun Shin, SKKU

CS370 Operating Systems

Thread and Synchronization

Operating systems fundamentals - B06

Outline. CS4254 Computer Network Architecture and Programming. Introduction 2/4. Introduction 1/4. Dr. Ayman A. Abdel-Hamid.

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

Introduction to pthreads

CS 5523 Operating Systems: Thread and Implementation

Introduction to PThreads and Basic Synchronization

CSCI4430 Data Communication and Computer Networks. Pthread Programming. ZHANG, Mi Jan. 26, 2017

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

CS370 Operating Systems

Programming with Shared Memory. Nguyễn Quang Hùng

LSN 13 Linux Concurrency Mechanisms

ECE 598 Advanced Operating Systems Lecture 12

EPL372 Lab Exercise 2: Threads and pthreads. Εργαστήριο 2. Πέτρος Παναγή

CS Operating Systems: Threads (SGG 4)

CSE 333 SECTION 9. Threads

Computer Systems Assignment 2: Fork and Threads Package

Chapter 4 Concurrent Programming

ECE 574 Cluster Computing Lecture 10

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

CS420: Operating Systems

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

Processes, Threads, SMP, and Microkernels

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

CS370 Operating Systems

Design Overview of the FreeBSD Kernel CIS 657

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Processes and threads

Native POSIX Thread Library (NPTL) CSE 506 Don Porter

THREADS. Jo, Heeseung

4. Concurrency via Threads

Threads. Threads (continued)

PROCESS CONCEPTS. Process Concept Relationship to a Program What is a Process? Process Lifecycle Process Management Inter-Process Communication 2.

Computer Systems Laboratory Sungkyunkwan University

Lecture 4: Threads; weaving control flow

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

Multiprocessors 2014/2015

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

Processes. CS3026 Operating Systems Lecture 05

C Grundlagen - Threads

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

4.8 Summary. Practice Exercises

What is concurrency? Concurrency. What is parallelism? concurrency vs parallelism. Concurrency: (the illusion of) happening at the same time.

Design Overview of the FreeBSD Kernel. Organization of the Kernel. What Code is Machine Independent?

Chapter 4 Multithreaded Programming

POSIX PTHREADS PROGRAMMING

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

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

ECE 574 Cluster Computing Lecture 13

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

The course that gives CMU its Zip! Concurrency I: Threads April 10, 2001

Concurrency. Johan Montelius KTH

Lecture 2: February 6

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209 Review. Yeah! We made it!

Transcription:

ECE 598 Advanced Operating Systems Lecture 23 Vince Weaver http://web.eece.maine.edu/~vweaver vincent.weaver@maine.edu 21 April 2016

Don t forget HW#9 Midterm next Thursday Announcements 1

Process States Running on CPU Ready ready but no CPU available Blocked waiting on I/O or resource Terminated 2

The Linux Scheduler People often propose modifying the scheduler. That is tricky. Scheduler picks which jobs to run when. Optimal scheduler hard. What makes sense for a longrunning HPC job doesn t necessarily make sense for an interactive GUI session. Also things like I/O (disk) get involved. You don t want it to have high latency 3

Linux originally had a simple circular scheduler. for 2.4 through 2.6 had an O(N) scheduler Then Then in 2.6 until 2.6.23 had an O(1) scheduler (constant time, no many how many processes). Currently the Completely Fair Scheduler (with lots of drama). Is O(log N). Implementation of weighted fair queuing How do you schedule? Power? Per-task (5 jobs, each get 20%). Per user? (5 users, each get 20%). 4

Per-process? Per-thread? Multi-processors? Hyperthreading? Heterogeneous cores? Thermal issues? 5

Threads Each process has one address space and single thread of control. It might be useful to have multiple threads share one address space GUI: interface thread and worker thread? Game: music thread, AI thread, display thread? Webserver: can handle incoming connections then pass serving to worker threads Why not just have one process that periodically switches? 6

Lightweight Process, multithreading Implementation: Each has its own PC Each has its own stack Why do it? shared variables, faster communication multiprocessors? mostly if does I/O that blocks, rest of threads can keep going allows overlapping compute and I/O 7

Problems: What if both wait on same resource (both do a scanf from the keyboard?) On fork, do all threads get copied? What if thread closes file while another reading it? 8

Common Thread Routines pthreads thread init() thread create() specify function thread exit() thread yield() if cooperative 9

Thread Implementations Cause of many flamewars over the years 10

User-Level Threads (N:1 one process many Benefits threads) Kernel knows nothing about them. Can be implemented even if kernel has no support. Each process has a thread table When it sees it will block, it switches threads/pc in user space Different from processes? When thread yield() called it can switch without calling into the kernel (no slow 11

kernel context switch) Can have own custom scheduling algorithm Scale better, do not cause kernel structures to grow Downsides How to handle blocking? Can wrap things, but not easy. Also can t wrap a pagefault. Co-operative, threads won t stop unless voluntarily give up. Can request periodic signal, but too high a rate is inefficient. 12

Can t take advantage of multiple CPUs 13

Kernel-Level Threads (1:1 process to Benefits thread) Kernel tracks all threads in system Handle blocking better Downsides Thread control functions are syscalls When yielding, might yield to another process rather than a thread 14

Might be slower 15

Hybrid (M:N) Can have kernel threads with user on top of it. Fast context switching, but can have odd problems like priority inversion. 16

Green Threads Managed by virtual machine Java 17

Misc Pop-up threads? Thread created for incoming message? adding multithreading to code? How to handle global variables (errno?) Thread-safe functions. Is strtok thread-safe? malloc? any routine that might not be re-entrant How are multiple stacks handled? One option each thread gets own copy of global variables. This can t be expressed by default in C, you need special routines, thread-local variables. 18

# include < pthread.h> # include <stdio.h> # include <stdlib.h> # include <assert.h> # define NUM_THREADS 10 POSIX Threads (pthreads) void * perform_work ( void * argument ) { int value ; value = *(( int *) argument ); printf (" Thread with argument %d!\n", value ); } return NULL ; int main ( int argc, char ** argv ) { pthread_t threads [ NUM_THREADS ]; int thread_args [ NUM_THREADS ]; 19

int result, i; /* create threads one by one */ for ( i = 0; i < NUM_THREADS ; i ++) { thread_args [i]=i; printf (" Main : creating thread %d\n", i); result = pthread_create (& threads [ i], NULL, perform_work, ( void *) & thread_args [i ]); if ( result!=0) { fprintf ( stderr," ERROR!\n"); return -1; } } /* wait for each thread to complete */ for ( i = 0; i < NUM_THREADS ; i ++) { /* block until each thread completes */ result = pthread_join ( threads [ i], NULL ); printf (" MAIN : thread %d has completed \n", i); if ( result!=0) { fprintf ( stderr," ERROR!\n"); return -1; } } 20

printf (" MAIN : All threads completed successfully \n"); } return 0; 21

POSIX Threads (pthreads) programming Pass -pthread to gcc Thread management pthread create (thread,attr,start routine,arg) pthread exit (status) pthread cancel (thread) pthread attr init (attr) pthread attr destroy (attr) pthread join (threadid,status) blocks thread 22

until specified thread finishes pthread detach (threadid) pthread attr setdetachstate (attr,detachstate) pthread attr getdetachstate (attr,detachstate) pthread attr getstacksize (attr, stacksize) pthread attr setstacksize (attr, stacksize) pthread attr getstackaddr (attr, stackaddr) pthread attr setstackaddr (attr, stackaddr) Mutexes (synchronization) pthread mutex init (mutex,attr) 23

pthread mutex destroy (mutex) pthread mutexattr init (attr) pthread mutexattr destroy (attr) pthread mutex lock (mutex) pthread mutex trylock (mutex) pthread mutex unlock (mutex) Condition Variables another way to synchronize Synchronization 24

Linux Posix Threads Originally used only userspace implementations. portable threads. GNU LinuxThreads use clone syscall, SIGUSR1 SIGUSR2 for communicating. Could not implement full POSIX threads, especially with signals. Replaced by NPTL Hard thread-local storage 25

Needed extra helper thread to handle signals Problems, what happens if helper thread killed? Signals broken? 8192 thread limit? proc/top clutter up with processed, not clear they are subthreads NPTL New POSIX Thread Library Kernel threads Clone. Add new futex system calls. Drepper and Molnar at RedHat Why kernel? Linux has very fast context switch compared to some OSes. Need new C library/abi to handle location of thread- 26

local storage On x86 the fs/gs segment used. Others need spare register. Signal handling in kernel Clone handles setting TID (thread ID) exit group() syscall added that ends all threads in process, exit() just ends thread. exec() kills all threads before execing Only main thread gets entry in proc 27

Misc adding multithreading to code? How to handle global variables (errno?) Thread-safe functions. Is strtok thread-safe? malloc? any routine that might not be re-entrant How are multiple stacks handled? One option each thread gets own copy of global variables. This can t be expressed by default in C, you need special routines, thread-local variables. 28

IPC Inter-Process Communication Processes want to communicate with each other. Examples? Two issues: getting the message across synchronizing signals network, message passing (send, receive) 29

shared memory (mmap) 30

Linux Signals and Signal handlers Very much like interrupts Concurrency issues much like threading Pipes stdout of one program to stdin of another one-way (half duplex) ls sort pipe system call / dup 31

C library has popen() FIFOs (named pipes) exist as file on filesystem SystemV IPC shared memory, semaphores ipcs Just use mmap Unix domain sockets Can send file descriptors across 32

Splice move data from fd to pipe w/o a copy? VM magic? Sendfile. zero copy? 33