Threads Section 2.2. Introduction to threads A thread (of execution) is a light-weight process –Threads reside within processes. –They share one address.

Slides:



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

Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Operating Systems COMP 4850/CISG 5550 Processes Introduction to Threads Dr. James Money.
1 Created by Another Process Reason: modeling concurrent sub-tasks Fetch large amount data from network and process them Two sub-tasks: fetching  processing.
Day 10 Threads. Threads and Processes  Process is seen as two entities Unit of resource allocation (process or task) Unit of dispatch or scheduling (thread.
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.
CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Threads CS 342 – Operating Systems Ibrahim Korpeoglu Bilkent University.
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
Chapter 2 Processes and Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
Threads. Processes and Threads  Two characteristics of “processes” as considered so far: Unit of resource allocation Unit of dispatch  Characteristics.
Process Concept An operating system executes a variety of programs
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
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.
Operating Systems CSE 411 CPU Management Sept Lecture 11 Instructor: Bhuvan Urgaonkar.
System Calls 1.
9/13/20151 Threads ICS 240: Operating Systems –William Albritton Information and Computer Sciences Department at Leeward Community College –Original slides.
CS 153 Design of Operating Systems Spring 2015
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
Threads, Thread management & Resource Management.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 2 Processes and Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
 2004 Deitel & Associates, Inc. All rights reserved. 1 Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 13 Threads Read Ch 5.1.
Threads G.Anuradha (Reference : William Stallings)
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
Chapter 4: Threads. 2 Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads.
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.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication.
Processes & Threads Introduction to Operating Systems: Module 5.
CSC 322 Operating Systems Concepts Lecture - 7: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
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 سيستم عامل.
OPERATING SYSTEM LESSON 5 THREADS. 2 The process model is based on two independent concepts: resource grouping and execution. A process is the way to.
Processes and Threads MICROSOFT.  Process  Process Model  Process Creation  Process Termination  Process States  Implementation of Processes  Thread.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-2: Threads Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
2.2 Threads  Process: address space + code execution  There is no law that states that a process cannot have more than one “line” of execution.  Threads:
Processes Chapter 3. Processes in Distributed Systems Processes and threads –Introduction to threads –Distinction between threads and processes Threads.
Threads prepared and instructed by Shmuel Wimer Eng. Faculty, Bar-Ilan University 1July 2016Processes.
Chapter 4 – Thread Concepts
Introduction to threads
CS 6560: Operating Systems Design
Chapter 4 – Thread Concepts
Thread Programming.
Process Management Presented By Aditya Gupta Assistant Professor
CS 3305 System Calls Lecture 7.
Chapter 4 Threads.
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Lecture 10: Threads Implementation
CSE 153 Design of Operating Systems Winter 2018
Fast Communication and User Level Parallelism
Threads Chapter 4.
Chapter 2 Processes and Threads 2.1 Processes 2.2 Threads
Multithreaded Programming
Operating Systems Threads 1.
Lecture 10: Threads Implementation
CSE 153 Design of Operating Systems Winter 2019
CS703 – Advanced Operating Systems
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:

Threads Section 2.2

Introduction to threads A thread (of execution) is a light-weight process –Threads reside within processes. –They share one address space, which means they share data as well as files A process initially has one thread of execution –The initial thread can create multiple threads to accomplish distinct tasks (more on this soon)

The goals The ability for multiple threads of execution to share a set of resources so they can work together to perform some larger task (pp ) The use of threads allows each one to use blocking system calls without affecting the other threads of the same process. –While one thread is blocked on I/O, another thread can execute within the same process. –The effect is that the process completes execution more quickly

Why use threads? Many applications are comprised of distinct multiple activities. Threads simplify the programming model. –Word processors, servers They are –more efficient to create and destroy –especially efficient in processes that have a good mix of CPU and I/O activities. –able to exploit the processing power of multiple CPU’s.

A multithreaded word processor These three threads cooperate with each other Communicates with user Reformats document AutoSave

A multithreaded web server Client request for service

Rough outline of code for multithreaded web server Dispatcher thread Worker thread

What threads offer ~ web server example ~ A single-thread web server would result in much idle time for the CPU –No other request could be served while the thread is blocked and waiting for I/O Multiple threads make it possible to achieve parallelism –This improves performance

The Thread Model Three unrelated processes, each with one initial thread Three related threads within one process

Contrast processes with threads Text Process Status Data Stack Kernel File Resources Text Data Thread Status Text Data Process Status ProcessThread Grouping related resources Executable entities Stack PC

Contrast processes with threads Items shared by all threads within a process Items private to each thread

Each thread has its own stack Why does each thread need its own stack?

Procedures for manipulating threads thread_create –Issued by a thread wishing to create another thread thread_exit –Issued by a thread that is done executing thread_wait –Issued by a thread waiting for another thread to exit thread_yield –Issued by a thread voluntarily surrendering the CPU to another thread (no time-sharing within a process)

Where are threads managed? Either in user space or in the kernel There are advantages and disadvantages to either approach. Originally, no operating systems supported threads, so user space libraries were developed to define threads packages. –Today, both Windows and Linux offer kernel support for threads.

Implementing Threads in User Space

Each process maintains its own thread table. If Thread A is running but must wait for Thread B to complete some work, we say that Thread A is “locally blocked”. –Thread A puts itself into a blocked state by pushing its register contents onto the process’ thread table, searching the table for a ready thread to run, and reloading the CPU registers with the new thread’s saved register contents. The new thread now begins executing. –These are just a few quick instructions, so very efficient. If a thread is done running for the time being, it calls thread_yield –The code of thread_yield saves the thread’s information in the thread table, and then calls the thread scheduler to pick another thread to run. –Saving the thread’s state and scheduling threads are accomplished by local procedures, and do not require a time-consuming system call.

More advantages of user-space thread management Each process can implement a customized thread scheduling algorithm because it knows the tasks its threads are performing. Even if many processes generate many threads, each process maintains its own thread table, so a large thread table in the kernel is not a potential problem.

One big problem How to handle blocking system calls? –There is no convenient way to ensure that, once a thread is blocked, other threads within the same process will be able to execute. –One solution is through the select system call This returns information about the system and tells the threads whether or not a read call will result in blocking

Implementing Threads in the Kernel

The kernel maintains one thread table. –When a thread wants to create, destroy or block another thread, it must make a system call. More time-consuming than in user-space When a thread is blocked, the kernel chooses the next thread to run, either from the current process, or from another process. But, the kernel also engages in thread recycling to improve performance time.

Attempts to combine both approaches Hybrid implementation Scheduler activation

Pop-up threads Useful in server processes. The arrival of a message (request) causes the creation of a new thread to “pop up” and handle it (instead of waking up a blocked thread). Less overhead involved.