ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-2: Threads Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.

Slides:



Advertisements
Similar presentations
1 Processes and Threads Creation and Termination States Usage Implementations.
Advertisements

Chap 4 Multithreaded Programming. Thread A thread is a basic unit of CPU utilization It comprises a thread ID, a program counter, a register set and a.
CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2.
Chapter 4: Multithreaded Programming
Chapter 5 Threads os5.
1 Created by Another Process Reason: modeling concurrent sub-tasks Fetch large amount data from network and process them Two sub-tasks: fetching  processing.
Threads Irfan Khan Myo Thein What Are Threads ? a light, fine, string like length of material made up of two or more fibers or strands of spun cotton,
Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
1 Threads CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5.
Threads Section 2.2. Introduction to threads A thread (of execution) is a light-weight process –Threads reside within processes. –They share one address.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Ceng Operating Systems Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
3.5 Interprocess Communication
Threads CSCI 444/544 Operating Systems Fall 2008.
Chapter 2 Processes and Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Objectives Thread definitions and relationship to process Multithreading.
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Multithreaded Programming.
ThreadsThreads operating systems. ThreadsThreads A Thread, or thread of execution, is the sequence of instructions being executed. A process may have.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
Processes Part I Processes & Threads* *Referred to slides by Dr. Sanjeev Setia at George Mason University Chapter 3.
1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
9/13/20151 Threads ICS 240: Operating Systems –William Albritton Information and Computer Sciences Department at Leeward Community College –Original slides.
Operating System Principles Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Processes and Threads.
1 Process States (1) Possible process states –running –blocked –ready Transitions between states shown.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Multithreading Allows application to split itself into multiple “threads” of execution (“threads of execution”). OS support for creating threads, terminating.
Threads, Thread management & Resource Management.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 13 Threads Read Ch 5.1.
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
CS333 Intro to Operating Systems Jonathan Walpole.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
1 Lecture 4: Threads Advanced Operating System Fall 2010.
Chapter 4: Threads. 2 Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads.
ITFN 3601 Introduction to Operating Systems Lecture 3 Processes, Threads & Scheduling Intro.
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.
Department of Computer Science and Software Engineering
1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication.
CSC 322 Operating Systems Concepts Lecture - 7: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Threads. Readings r Silberschatz et al : Chapter 4.
Cooperating Processes The concurrent processes executing in the operating system may be either independent processes or cooperating processes. A process.
Cs431-cotter1 Processes and Threads Tanenbaum 2.1, 2.2 Crowley Chapters 3, 5 Stallings Chapter 3, 4 Silberschaz & Galvin 3, 4.
Shahriar Pirnia Operating system سيستم عامل.
Processes and Threads MICROSOFT.  Process  Process Model  Process Creation  Process Termination  Process States  Implementation of Processes  Thread.
Operating System Concepts
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Threads by Dr. Amin Danial Asham. References Operating System Concepts ABRAHAM SILBERSCHATZ, PETER BAER GALVIN, and GREG GAGNE.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Threads prepared and instructed by Shmuel Wimer Eng. Faculty, Bar-Ilan University 1July 2016Processes.
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Threads & multithreading
Chapter 4: Threads.
Operating System Concepts
Operating Systems (CS 340 D)
Implementing Threads in User Space
Multithreaded Programming
Operating Systems (CS 340 D)
Prof. Leonardo Mostarda University of Camerino
Thread Model for Work Unit
Chapter 4: Threads.
What is a Thread? A thread is similar to a process, but it typically consists of just the flow of control. Multiple threads use the address space of a.
Threads CSE 2431: Introduction to Operating Systems
Presentation transcript:

ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-2: Threads Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습 2 Contents 1.Threads vs. Process 2.Thread Model 3.Thread Usage 4.Implementing Threads 5.Scheduler Activations 6.Pop-up Threads 7.Making Single-Threaded Code Multithreaded

ITEC502 컴퓨터 시스템 및 실습 3 Threads vs. Processes  The process model is based on two concepts: ‘ resource grouping ’ + ‘ execution ’  Resource grouping of a process –an address space for program text and data –other resources: open files, child processes, etc.  Execution of a process –a single control sequence (i.e., program counter) –why not multiple control sequences (threads)?  multi-threading!!!

ITEC502 컴퓨터 시스템 및 실습 4 Thread Model (1)  A process can have one or more threads  A thread consists of: –its own program counter (PC) –its own set of registers –its own (run-time) stack a frame for each invoked proc. that has not yet been done  Each thread is subject to scheduling  Also called lightweight process

ITEC502 컴퓨터 시스템 및 실습 5 Thread Model (2) (a) Three processes each with one thread (b) One process with three threads

ITEC502 컴퓨터 시스템 및 실습 6 Thread Model (3)  Items shared by all threads in a process  Items private to each thread

ITEC502 컴퓨터 시스템 및 실습 7 Thread Model (4) Each thread has its own stack

ITEC502 컴퓨터 시스템 및 실습 8 Thread Model (5)  Thread-related library procedures –thread_create() –thread_exit() –thread_wait() –thread_yield()

ITEC502 컴퓨터 시스템 및 실습 9 Thread Model (6)  Multi-threading may introduce lots of difficulties/complications to programming –what if a parent process calls fork() ? Should the child inherit threads too? –what if a thread closes a file while another thread is reading from the same file?  The problems can be solved with some efforts, but careful thought and design should be used!

ITEC502 컴퓨터 시스템 및 실습 10 What is a Thread?  is a flow of control within a process –single-threaded process, multi-threaded process  is a basic unit of CPU utilization, which comprise –a thread ID, program counter, register set, stack, state  shares with other threads belonging to the same process its code section, data section, and other OS resources (open files and signal)  If a process has multiple threads of control, it can perform more than one task at a time

ITEC502 컴퓨터 시스템 및 실습 11 Single & Multithreaded Processes

ITEC502 컴퓨터 시스템 및 실습 12 Thread Usage (1) When threads are useful?  In many applications, several activities are going at once –multi-threading may provide a simpler programming model  Threads do not have any resources to attached to them –much easier and cheaper to create and destroy them  If there are substantial computing and, also, I/O, then they can be overlap by threads –there is no performance gain from CPU bound applications  Threads are useful on systems with multiple CPUs

ITEC502 컴퓨터 시스템 및 실습 13 Thread Usage (2): Multithreaded programs  Many software packages that run on modern OS are multi-threaded  A web browser might have –One thread display images or text –Another thread retrieves data from the network  A word processor may have –A thread for displaying graphics –Another thread for responding to keystrokes from the user –A third thread for writing the contents of RAM to disk periodically

ITEC502 컴퓨터 시스템 및 실습 14 Thread Usage (3) A word processor with three threads

ITEC502 컴퓨터 시스템 및 실습 15 Thread Usage (4): Multithreaded programs  Web Server, for example –Single-threaded web server: a client might have to wait for its request to be serviced –Multi-processes web server: used before threads become popular, much overhead in creating a new process –Multi-threaded web server: less overhead in thread creation, concurrent service to multiple client  Many OS kernels are now multi-threaded –Several threads operates in the kernel –Each thread performs a specific task, such as managing devices or interrupt handling

ITEC502 컴퓨터 시스템 및 실습 16 Thread Usage (5) A multithreaded Web server

ITEC502 컴퓨터 시스템 및 실습 17 Thread Usage (6)  Rough outline of code for previous slide (a) Dispatcher thread (b) Worker thread

ITEC502 컴퓨터 시스템 및 실습 18 Thread Usage (7) Three ways to construct a server

ITEC502 컴퓨터 시스템 및 실습 19 Benefits of multi-threaded Programming  Responsiveness –Multithreading an interactive application may allow a program to continue running even if part of it is blocked or doing a lengthy operation  Resource Sharing –Threads share the memory and the resources of the process to which they belong  Economy –Because threads in a process shares the resources, it is more economical to create and context-switch threads –Creating process is about thirty times slower than creating thread in Solaris  Utilization of Multi-Processor Architectures –Threads may be running in parallel on different processors

ITEC502 컴퓨터 시스템 및 실습 20 Two types of threads  User Thread  Kernel Thread  User-level thread are threads that are visible to the programmer and are unknown to the kernel  OS kernel supports and manages kernel-level threads  In general, user-level threads are faster to create and manage than are kernel threads, because no intervention from the kernel is required

ITEC502 컴퓨터 시스템 및 실습 21 Implementing Threads in User Space A user-level threads package

ITEC502 컴퓨터 시스템 및 실습 22 Pros & Cons of User Threading  All thread-related activities are user procedures –no system calls or context switching –very fast  Each process can have a customized scheduler  BUT …  How blocking system calls are implemented? –blocking vs. non-blocking system calls –can use hints whether a system call will be blocking or not  jacket or wrapper  A single thread may run forever  Many applications (e.g., web servers) where threading is useful generally block often – if they block, often no more work is left to be done!

ITEC502 컴퓨터 시스템 및 실습 23 Implementing Threads in the Kernel A threads package managed by the kernel No run-time system, nor thread table in user space needed! all thread-related procedure are now system calls! the cost for creating and destroying threads can be high!  why not recycle threads!!!

ITEC502 컴퓨터 시스템 및 실습 24 Pros & Cons of Kernel Threading  No need for non-blocking system calls  Time-out is supported by OS  BUT …  System calls for thread management can be high  Less flexible –Custom scheduler not possible unless kernel is recompiled (and installed)

ITEC502 컴퓨터 시스템 및 실습 25 Hybrid Implementations Multiplexing user-level threads onto kernel- level threads

ITEC502 컴퓨터 시스템 및 실습 26 Scheduler Activations (1)  Goal – mimic functionality of kernel threads –gain performance of user space threads  Avoids unnecessary user/kernel transitions  Kernel assigns virtual processors to each process –lets runtime system allocate threads to processors  Basic mechanism: –when a thread makes a blocking system call, the kernel activates the user run-time system  upcall –when an interrupt occurs, the kernel also let the user run-time system decide which thread to run

ITEC502 컴퓨터 시스템 및 실습 27 Scheduler Activations (2)  Problem: –fundamental reliance on kernel (lower layer) –calling procedures in user space (higher layer)  violation of a fundamental principle!

ITEC502 컴퓨터 시스템 및 실습 28 Pop-Up Threads (1)  Can be useful in distributed systems –handling incoming messages (service requests)

ITEC502 컴퓨터 시스템 및 실습 29 Pop-Up Threads (2)  Pop-up threads are all brand-new! –no history to restore –they are also all identical –it is cheap to create them!  Pop-up threads in kernel space –easier and faster than in user space –direct access to kernel resources (tables, IO devices … ) –but, what if they are buggy??

ITEC502 컴퓨터 시스템 및 실습 30 Making Single-Threaded Code Multithreaded (1) Conflicts between threads over the use of a global variable

ITEC502 컴퓨터 시스템 및 실습 31 Making Single-Threaded Code Multithreaded (2) Threads can have private global variables

ITEC502 컴퓨터 시스템 및 실습 32 Making Single-Threaded Code Multithreaded (3)  Other problems –Many library functions are not reentrant –Difficulty with signals/interrupts –Difficulty with stack management  Those problems may be solved one way or another –But tricky –May require substantial system redesign, redefining system calls and library functions  also should be backward compatible!!!!

ITEC502 컴퓨터 시스템 및 실습 33 Summary  A thread is a flow of control within process –Multithreaded process contains several different flows of control within the same address space  Benefits of multi-threading includes –Increased responsiveness, Resource sharing within the process –Economy, Ability to take advantage of multiprocessor architecture  User-level thread are thread that are visible to the programmer and are unknown to the kernel  OS kernel supports and manages kernel-level threads

ITEC502 컴퓨터 시스템 및 실습 34 Review 1.Threads vs. Process 2.Thread Model 3.Thread Usage 4.Implementing Threads 5.Scheduler Activations 6.Pop-up Threads 7.Making Single-Threaded Code Multithreaded