Threads (light weight processes) Chester Rebeiro IIT Madras

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

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

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

pthreads CS449 Fall 2017

CMPSC 311- Introduction to Systems Programming Module: Concurrency

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

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

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Multithreaded Programming

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

Operating systems fundamentals - B06

Threads. Threads (continued)

CSE 333 SECTION 9. Threads

CMPSC 311- Introduction to Systems Programming Module: Concurrency

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

From Processes to Threads

Processes and Threads

Introduction to PThreads and Basic Synchronization

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

CS333 Intro to Operating Systems. Jonathan Walpole

Multicore and Multiprocessor Systems: Part I

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

CSE 306/506 Operating Systems Threads. YoungMin Kwon

Threads and Too Much Milk! CS439: Principles of Computer Systems January 31, 2018

Threads. studykorner.org

Yi Shi Fall 2017 Xi an Jiaotong University

Concurrency. Johan Montelius KTH

CSC209H Lecture 11. Dan Zingaro. March 25, 2015

Processes, Threads and Processors

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

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

Introduction to pthreads

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

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

CS 3305 Intro to Threads. Lecture 6

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

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

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

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

CS510 Operating System Foundations. Jonathan Walpole

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

Page 1. Analogy: Problems: Operating Systems Lecture 7. Operating Systems Lecture 7

Computer Systems Laboratory Sungkyunkwan University

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

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Getting to know you. Anatomy of a Process. Processes. Of Programs and Processes

Threads. CS3026 Operating Systems Lecture 06

Pthread (9A) Pthread

Threads. lightweight processes

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control

Last class: Today: Thread Background. Thread Systems

Section 4: Threads and Context Switching

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

CISC2200 Threads Spring 2015

Section 4: Threads CS162. September 15, Warmup Hello World Vocabulary 2

CS 326: Operating Systems. Process Execution. Lecture 5

THREADS. Jo, Heeseung

Processes and Threads. From Processes to Threads. The Case for Threads. The Case for Threads. Process abstraction combines two concepts.

Operating Systems. Lab. Class Project #5

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

Threads and Too Much Milk! CS439: Principles of Computer Systems February 6, 2019

LSN 13 Linux Concurrency Mechanisms

Creating Threads. Programming Details. COMP750 Distributed Systems

Concurrent Server Design Multiple- vs. Single-Thread

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

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

W4118 Operating Systems. Junfeng Yang

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

Processes and Threads

Concurrency and Threads

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

Learning Outcomes. Processes and Threads Implementation. Processes Process s user-level stack and execution state. Threads The Thread Model.

Processes and Threads Implementation

Notes. CS 537 Lecture 5 Threads and Cooperation. Questions answered in this lecture: What s in a process?

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

Processes, Threads, SMP, and Microkernels

Single and mul,threaded processes

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

POSIX Threads. HUJI Spring 2011

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

A process. the stack

Concurrent Programming

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

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

Creating Threads COMP755

Concurrent Programming

Programming Assignment #4

Agenda. Process vs Thread. ! POSIX Threads Programming. Picture source:

POSIX Threads Programming

Processes. Johan Montelius KTH

Chapter 3 Process Description and Control

CS Operating Systems: Threads (SGG 4)

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

Operating Systems. Lecture 4. Nachos Appetizer

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

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

Processes. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

CS370 Operating Systems

CS 5523 Operating Systems: Thread and Implementation

CS510 Operating System Foundations. Jonathan Walpole

Transcription:

Threads (light weight processes) Chester Rebeiro IIT Madras 1

Processes Separate streams of execution Each process isolated from the other Process state contains Process ID Environment Working directory. Program instructions Registers Stack Heap File descriptors Created by the OS using fork Significant overheads 2

Threads Separate streams of execution within a single process Threads in a process not isolated from each other Each thread state (thread control block) contains Registers (including EIP, ESP) stack 3

Why threads? Lightweight Cost of creating 50,000 processes / threads (https://computing.llnl.gov/tutorials/pthreads/) Efficient communication between entities Efficient context switching 4

Threads vs Processes A thread has no data segment or heap A thread cannot live on its own. It needs to be attached to a process There can be more than one thread in a process. Each thread has its own stack If a thread dies, its stack is reclaimed A process has code, heap, stack, other segments A process has at-least one thread. Threads within a process share the same I/O, code, files. If a process dies, all threads die. Based on Junfeng Yang s lecture slides http://www.cs.columbia.edu/~junfeng/13fa-w4118/lectures/l08-thread.pdf 5

pthread library Create a thread in a process int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); Thread identifier (TID) much like Pointer to a function, which starts execution in a different thread Arguments to the function Destroying a thread void pthread_exit(void *retval); Exit value of the thread 6

pthread library contd. Join : Wait for a specific thread to complete int pthread_join(pthread_t thread, void **retval); TID of the thread to wait for Exit status of the thread what is the difference with wait()? 7

Example How many threads are there in this program? 3 Note. You need to link the pthread library 8

Other thread libraries Windows threads Boost (in C++) LinuxThreads etc. 9

Who manages threads? Two strategies User threads Thread management done by user level thread library. Kernel knows nothing about the threads. Kernel threads Threads directly supported by the kernel. Known as light weight processes. 10

User level threads Advantages: Fast (really lightweight) (no system call to manage threads. The thread library does everything). Can be implemented on an OS that does not support threading. Switching is fast. No, switch from user to protected mode. Disadvantages: Scheduling can be an issue. (Consider, one thread that is blocked on an IO and another runnable.) Lack of coordination between kernel and threads. (A process with 1000 threads competes for a timeslice with a process having just 1 thread.) Requires non-blocking system calls. (If one thread invokes a system call, all threads need to wait) 11

Kernel level threads Advantages: Scheduler can decide to give more time to a process having large number of threads than process having small number of threads. Kernel-level threads are especially good for applications that frequently block. Disadvantages: The kernel-level threads are slow (they involve kernel invocations.) Overheads in the kernel. (Since kernel must manage and schedule threads as well as processes. It require a full thread control block (TCB) for each thread to maintain information about threads.) 12

Thread Models Many-to-one model one-to-one model Many-to-many model 13

Many-to-one model user thread Many user level threads map to a single kernel thread Pros: Fast. No system calls to manage threads. No mode change for switching threads Cons: No parallel execution of threads. All threads block when one has a system call. Not suited for multi-processor systems. kernel thread 14

One-to-one model user thread Each user thread associated with one kernel thread. Pros. Better suited for multiprocessor environments. When one thread blocks, the other threads can continue to execute. Cons. Expensive. Kernel is involved. kernel thread 15

Many-to-Many model Many user threads mapped to many kernel threads Supported by some unix and windows versions Pros: flexible OS creates kernel threads as required Process creates user threads as needed Cons: Complex Double management 16

Threading issues What happens when a thread invokes fork? Duplicate all threads? Not easily done other threads may be running or blocked in a system call Duplicate only the caller thread? More feasible. Segmentation fault in a thread. Should only the thread terminate or the entire process? 17

Typical usage of threads Event? event occurred No event occurred create thread Service event terminate thread Creating and terminating thread lead to overheads 18

Thread pools Event? event occurred No event occurred Thread pool assign job to thread from pool service event Number of threads in pool is critical! Block thread 19

Assignment Write a multi-threaded program to perform matrix multiplication. The input matrices are of dimension 1000 x 1000 Tabulate time taken vs number of threads used. 20