Threads. studykorner.org

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

Multithreaded Programming

pthreads CS449 Fall 2017

Pthread (9A) Pthread

Threads. Threads (continued)

CSE 306/506 Operating Systems Threads. YoungMin Kwon

Threads. CS3026 Operating Systems Lecture 06

Chapter 4: Threads. Operating System Concepts with Java 8 th Edition

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

LSN 13 Linux Concurrency Mechanisms

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

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

CS 3305 Intro to Threads. Lecture 6

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

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

I.-C. Lin, Assistant Professor. Textbook: Operating System Concepts 8ed CHAPTER 4: MULTITHREADED PROGRAMMING

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

THREADS. Jo, Heeseung

CS370 Operating Systems

CS420: Operating Systems

Processes and Threads

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

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

Computer Systems Laboratory Sungkyunkwan University

Introduction to pthreads

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

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

CSE 333 SECTION 9. Threads

Lecture 4 Threads. (chapter 4)

Chapter 4: Threads. Chapter 4: Threads

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

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

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

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

Definition Multithreading Models Threading Issues Pthreads (Unix)

Concurrency. Johan Montelius KTH

CISC2200 Threads Spring 2015

Chapter 4: Multithreaded Programming. Operating System Concepts 8 th Edition,

CS370 Operating Systems

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

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

Threads Chapter 4. Reading: 4.1,4.4, 4.5

CSE 4/521 Introduction to Operating Systems

Multicore and Multiprocessor Systems: Part I

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

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

Threads. Jo, Heeseung

4. Concurrency via Threads

Chapter 3 Process Description and Control

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

Threads Chapter 5 1 Chapter 5

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

CSMC 412. Operating Systems Prof. Ashok K Agrawala Ashok Agrawala Set 4. September 2006 CMSC 412 Set 4

Concurrency, Thread. Dongkun Shin, SKKU

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

CS307: Operating Systems

Multithreaded Programming Guide. Beta

CS370 Operating Systems

OPERATING SYSTEM. Chapter 4: Threads

Introduction to PThreads and Basic Synchronization

Operating Systems Prof. Ashok K Agrawala

Chapter 4: Multi-Threaded Programming

Chapter 4: Multithreaded

Chapter 5: Threads. Single and Multithreaded Processes

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

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

4.8 Summary. Practice Exercises

CS333 Intro to Operating Systems. Jonathan Walpole

CS510 Operating System Foundations. Jonathan Walpole

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

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!

Last class: Today: Thread Background. Thread Systems

Operating systems fundamentals - B06

CMPSC 311- Introduction to Systems Programming Module: Concurrency

CS370 Operating Systems

Chapter 4: Threads. Chapter 4: Threads

CS 450 Operating System Week 4 Lecture Notes

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

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

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads. Operating System Concepts

Chapter 4 Multithreaded Programming

Threaded Programming. Lecture 9: Alternatives to OpenMP

POSIX Threads. HUJI Spring 2011

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

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Introduction to Threads

CSCE 313: Intro to Computer Systems

CS510 Operating System Foundations. Jonathan Walpole

Chapter 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Threads. lightweight processes

Computer Systems Laboratory Sungkyunkwan University

CPSC 313: Intro to Computer Systems. POSIX Threads. Latency Hiding / Multiprogramming (covered earlier) Ease of Programming (covered now)

Thread and Synchronization

ECE 598 Advanced Operating Systems Lecture 23

Processes, Threads, SMP, and Microkernels

Threads. Jo, Heeseung

Outline. Threads. Single and Multithreaded Processes. Benefits of Threads. Eike Ritter 1. Modified: October 16, 2012

Transcription:

Threads

Thread Subpart of a process Basic unit of CPU utilization Smallest set of programmed instructions, can be managed independently by OS No independent existence (process dependent) Light Weight Process

Light Weight Process Process Single thread system Every process have its separate resources Heavy for memory Heavy for process context switching Heavy IPC Thread Threads have own resources and shared Threads of a process share resources Light for memory Light for thread context switching Light ITC

Advantages of multithreaded system Responsiveness Resource sharing Code, process resources, address space Economy Utilization of multiprocessor architectures

Types of Threads User threads User level library Kernel threads Kernel space

User level threads Created and managed in user space The kernel is not aware of thread activity but it is still managing process activity Thread switching does not involve the kernel Scheduling can be application specific ULTs can run on any OS -- Only needs a thread library For a single kernel process, they cannot be distributed among multiple processors. I/O or other blocking system call has to be replaced with non-blocking system calls. kernel can only assign processes to processors

Kernel Level Threads Created and managed in kernel space by kernel Slower to create and to manage If a thread performs a blocking system call, the kernel can schedule another thread in the application for execution.

Implementing Threads in User Space

ULT Idea Thread management done by user-level threads library. Threads library contains code for: creating and destroying threads. passing messages and data between threads. scheduling thread execution. saving and restoring thread contexts. Three primary thread libraries: POSIX Pthreads Win32 threads Java threads

POSIX Pthreads A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization. May be provided either as ULT or KLT. 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).

Some of the Pthreads function calls

Kernel activity for ULTs The kernel is not aware of thread activity but it is still managing process activity. When a thread makes a system call, the whole task will be blocked. But for the thread library that thread is still in the running state. So thread states are independent of process states.

Advantages and inconveniences of ULT Advantages Thread switching does not involve the kernel: no mode switching. Scheduling can be application specific: choose the best algorithm. ULTs can run on any OS. Only needs a thread library. Inconveniences The kernel blocks processes. So all threads within the process will be blocked. The kernel can only assign processes to processors. Two threads within the same process cannot run simultaneously on two processors.

Thread Library Creating and destroying threads Passing messages and data between threads Scheduling thread execution Saving and restoring thread contexts

Implementing Threads in the Kernel

Advantages and inconveniences of KLT Advantages the kernel can simultaneously schedule many threads of the same process on many processors. blocking is done on a thread level. Inconveniences thread switching within the same process involves the kernel. We have 2 mode switches per thread switch. this results in a significant slow down.

Hybrid ULT/KLT Approaches Thread creation done in the user space. Bulk of scheduling and synchronization of threads done in the user space. The programmer may adjust the number of KLTs. May combine the best of both approaches. Example is Solaris prior to version 9.

Hybrid Implementation Multiplexing user-level threads onto kernel-level threads.

ULT, KLT and Combined Approaches

Multithreading Models Many-to-One One-to-One Many-to-Many

Many-to-One Many user-level threads mapped to single kernel thread. Thread management is done in user space. Used on systems that do not support kernel threads. Since only one thread can access kernel at a time, multiple threads are unable to run in parallel on multiprocessors.

Many-to-One Model

One-to-One Each user-level thread maps to kernel thread. More concurrency. Every user thread requires creating a corresponding kernel thread. Examples - Windows 95/98/NT/2000 - Linux

One-to-one Model

Many-to-Many Model Allows many user level threads to be mapped to many kernel threads. The number of kernel threads may be specific to a particular application or particular machine. (More kernel threads on a multiprocessor m/c) Allows the operating system to create a sufficient number of kernel threads. Solaris 2, IRIX, HP-UX

Many-to-Many Model

Thread Implementation Creating a (Default) Thread pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);

Cont #include <pthread.h> pthread_attr_t tattr; pthread_t tid; extern void *start_routine(void *arg); void *arg; int ret; /* default behavior*/ ret = pthread_create(&tid, NULL, start_routine, arg); /* initialized with default attributes */ ret = pthread_attr_init(&tattr); /* default behavior specified*/ ret = pthread_create(&tid, &tattr, start_routine, arg);

Cont When pthread_create is successful, the ID of the thread created is stored in the location referred to as tid. pthread_create() function is called with attr. pthread_create() returns a zero and exits when it completes successfully

#include <pthread.h> pthread_t tid; int ret; int status; /* waiting to join thread "tid" with status */ ret = pthread_join(tid, &status); /* waiting to join thread "tid" without status/* ret = pthread_join(tid, NULL);

The pthread_join() function blocks the calling thread until the specified thread terminates.

void mainline (...) { struct phonebookentry *pbe; pthread_attr_t tattr; pthread_t helper; int status; pthread_create(&helper, NULL, fetch, &pbe); /* do something else for a while */ pthread_join(helper, &status); /* it's now safe to use result */ } void fetch(struct phonebookentry *arg) { struct phonebookentry *npbe; /* fetch value from a database */ npbe = search (prog_name) if (npbe!= NULL) *arg = *npbe; pthread_exit(0); } struct phonebookentry { char name[64]; char phonenumber[32]; char flags[16]; }