CSE 306/506 Operating Systems Threads. YoungMin Kwon

Similar documents
Threads. Threads (continued)

Operating System. Chapter 4. Threads. Lynn Choi School of Electrical Engineering

Threads. studykorner.org

ECE519 Advanced Operating Systems

Processes, Threads, SMP, and Microkernels

For use by students enrolled in #71251 CSE430 Fall 2012 at Arizona State University. Do not use if not enrolled.

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

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

pthreads CS449 Fall 2017

Chapter 3 Process Description and Control

Operating Systems: Internals and Design Principles. Chapter 4 Threads Seventh Edition By William Stallings

Threads Chapter 5 1 Chapter 5

Operating Systems. Engr. Abdul-Rahman Mahmood MS, PMP, MCP, QMR(ISO9001:2000) alphapeeler.sf.net/pubkeys/pkey.htm

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

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

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

Process Characteristics. Threads Chapter 4. Process Characteristics. Multithreading vs. Single threading

Threads Chapter 4. Reading: 4.1,4.4, 4.5

A Thread is an independent stream of instructions that can be schedule to run as such by the OS. Think of a thread as a procedure that runs

Threads. CS3026 Operating Systems Lecture 06

Threads. lightweight processes

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

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

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

CS333 Intro to Operating Systems. Jonathan Walpole

Chapter 4 Threads, SMP, and

Multithreaded Programming

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

Roadmap. Concurrent Programming. Concurrent Programming. Motivation. Why Threads? Tevfik Ko!ar. CSC Systems Programming Fall 2010

CSE 333 SECTION 9. Threads

Process Characteristics

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

Multicore and Multiprocessor Systems: Part I

Pthread (9A) Pthread

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

CS510 Operating System Foundations. Jonathan Walpole

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

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

Operating Systems. Lecture 4. Nachos Appetizer

CISC2200 Threads Spring 2015

Concurrent Programming

Computer Systems Laboratory Sungkyunkwan University

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Concurrency. Johan Montelius KTH

Threads, SMP, and Microkernels

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Today. Threads review Sharing Mutual exclusion Semaphores

CS 3305 Intro to Threads. Lecture 6

Concurrent Programming

Operating System 4 THREADS, SMP AND MICROKERNELS

Threads (light weight processes) Chester Rebeiro IIT Madras

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

THREADS. Jo, Heeseung

Process Description and Control

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

Threads. Jo, Heeseung

Chapter 4 Concurrent Programming

Total Score is updated. The score of PA4 will be changed! Please check it. Will be changed

CMPSC 311- Introduction to Systems Programming Module: Concurrency

CS 153 Lab4 and 5. Kishore Kumar Pusukuri. Kishore Kumar Pusukuri CS 153 Lab4 and 5

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

4. Concurrency via Threads

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

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

CS 326: Operating Systems. Process Execution. Lecture 5

Introduction to pthreads

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

Carnegie Mellon Performance of Mul.threaded Code

Operating Systems. Chapter 4 Threads. Lei Duan

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: Processes and Threads

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

CS370 Operating Systems

Lecture 4. Threads vs. Processes. fork() Threads. Pthreads. Threads in C. Thread Programming January 21, 2005

Concurrent Programming is Hard! Concurrent Programming. Reminder: Iterative Echo Server. The human mind tends to be sequential

CS510 Operating System Foundations. Jonathan Walpole

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: Processes and Threads

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

Operating systems fundamentals - B06

2017 Pearson Educa2on, Inc., Hoboken, NJ. All rights reserved.

Lecture 4 Threads. (chapter 4)

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

Threads. Still Chapter 2 (Based on Silberchatz s text and Nachos Roadmap.) 3/9/2003 B.Ramamurthy 1

4.8 Summary. Practice Exercises

ANSI/IEEE POSIX Standard Thread management

Chapter 4 Threads. Images from Silberschatz 03/12/18. CS460 Pacific University 1

CS370 Operating Systems

CSE 306/506 Operating Systems Concurrency: Mutual Exclusion and Synchronization YoungMin Kwon

CS 261 Fall Mike Lam, Professor. Threads

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

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Computer Systems Laboratory Sungkyunkwan University

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

Network Programming TDC 561

Concurrent Programming

COSC 6374 Parallel Computation. Shared memory programming with POSIX Threads. Edgar Gabriel. Fall References

Threads. CS 475, Spring 2018 Concurrent & Distributed Systems

Processes and Threads. Processes and Threads. Processes (2) Processes (1)

Processes and Threads

CS533 Concepts of Operating Systems. Jonathan Walpole

Transcription:

CSE 306/506 Operating Systems Threads YoungMin Kwon

Processes and Threads Two characteristics of a process Resource ownership Virtual address space (program, data, stack, PCB ) Main memory, I/O devices, files Scheduling/execution Execution of a process follows an execution path Execution may be interleaved with that of other processes

Processes and Threads Resource ownership and Scheduling/execution Could be treated independently by the OS Thread (lightweight process): the unit of dispatching Process (task): the unit of resource ownership

Multithreading Multithreading The ability of an OS to support multiple concurrent paths of execution within a single process Processes in a multithreaded environment Unit of resource allocation Unit of protection

Multithreading Threads within a process have Thread execution state (Running, Ready, ) Saved thread context (PC, registers, ) Execution stack Per thread static storage for local variables Access to resources shared with other threads in the process

Multithreading All threads of a process share the states and resources of the process If a thread alters data, other threads will see the change If a thread opens a file with a read privilege, others can also read the file

Multithreading

Multithreading Performance benefits It takes far less time to create a new thread than to create a new process Terminating a thread is faster than terminating a process Switch between threads is faster than switch between processes Threads enhance efficiency in communication between executing programs No need for the protection and communication mechanisms

Thread Functionality Thread states: Running, Ready, Blocked Suspend is for processes: if a process is swapped out, all of its threads are swapped out RPC (Remote Procedure Call) example

RPC example Thread Functionality

Thread Functionality Four basic thread operations Spawn: when a new process is created or when a thread spawns a new thread New thread gets its own register context, stack and placed on the Ready queue Block: waits for an event Unblock: when the event occurs, the thread is moved to the Ready queue Finish: completes its operation

POSIX Threads (Pthreads) A standard interface for manipulating threads from C programs Defines about 60 functions that allow programs to create, kill, and reap threads to share data safely with peer threads to notify peers about changes in the system state

Pthreads: Creating Threads #include <pthread.h> typedef void *(func)(void *); // To create a new thread int pthread_create(pthread_t *tid, pthread_attr_t *attr, func *f, void *arg); // To get the thread id of its own pthread_t pthread_self(void);

Pthreads: Terminating Threads #include <pthread.h> void pthread_exit(void *thread_return); // Terminate explicitly // If main thread calls pthread_exit, // it will wait for all other peer threads to // terminate, terminate itself, and terminate // the process int pthread_cancel(pthread_t tid); // Terminate the thread with the ID tid

Pthreads: Reaping Terminated Threads #include <pthread.h> int pthread_join(pthread_t tid, void **thread_return); // blocks until thread tid terminates, // update thread_return to point to the return // value of the thread routine, // reap the memory resourece

#include <pthread.h> #include <stdio.h> void *thread(void *vargp) { printf("%s\n", (char*)vargp); pthread_exit("world"); //return "world"; return NULL; } int main() { pthread_t tid; pthread_create(&tid, NULL, thread, "hello"); } void *ret; pthread_join(tid, &ret); printf("%s\n", (char*)ret); return 0;

Thread Synchronization All threads of a process share the same address space and resources Any alteration of a resource by one thread affects the environment of the other threads Need to synchronize the activities of various threads

Types of Threads Categories of thread implementation User level threads (ULTs) Kernel level threads (KLTs)

User Level Threads Pure ULTs Thread management is done by the application Kernel is not aware of the existence of threads Spawning: Create a data structure for the thread Pass control to a thread in the Ready state Context switch: Context comprises user registers, PC, stack pointer When control is passed to the library: save the context When control is passed from the library: restore the context

ULT: system call from a thread Thread 2 makes a blocking system call It blocks process B Thread Lib. thinks that thread 2 is in Running state

ULT: process time slice is expired Process B s time slice is expired and B is moved to the Ready queue Process B is in the Ready state Thread Lib. thinks that thread 2 is in Running state

ULT: blocked thread Thread 2 waits for an event from thread 1 Thread 2 transitions to the Blocked state Thread 1 transitions to the Running state

ULT vs KLT Advantages of ULT compared to KLT Thread switching does not require kernel mode privilege Saves 2 mode switches: user > kernel, kernel > user Scheduling can be application specific Portability: ULTs can run on any OS

ULT vs KLT Disadvantages of ULT compared to KLT Blocking system calls block not only the thread but all other threads in the process as well Cannot take advantage of multiprocessors Jacketing Converts blocking system calls into non blocking ones Use jacket routines that check if the device is busy If it is, make the thread blocked and schedule another thread Check the device again later

Kernel Level Threads Thread management is done by the kernel Process context and thread context are managed by the kernel Scheduling is done by the kernel on a thread basis Kernel can schedule multiple threads of a process to multiple processors If a thread is blocked, kernel can schedule another thread of the same process

Performance Improvements on Amdahl s law Multicore Systems 1 f: fraction of execution time that is inherently serial

Performance Improvements on Multicore Systems Small amount of serial code has a noticeable impact

Performance Improvements on Multicore Systems The curves peak when considering the overhead Communication, Distribution, Cache coherence,