Preview. The Thread Model Motivation of Threads Benefits of Threads Implementation of Thread

Similar documents
Motivation of Threads. Preview. Motivation of Threads. Motivation of Threads. Motivation of Threads. Motivation of Threads 9/12/2018.

System Call. Preview. System Call. System Call. System Call 9/7/2018

Chapter 4: Threads. Operating System Concepts 8 th Edition,

Processes and Threads

Chapter 2 - Processes and Threads

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

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

Chapter 3 Process Description and Control

Chapter 2 Processes and Threads

CS 450 Operating System Week 4 Lecture Notes

Process Description and Control

Lecture 17: Threads and Scheduling. Thursday, 05 Nov 2009

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

Chapter 4: Multithreaded Programming

Concurrency, Thread. Dongkun Shin, SKKU

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

Operating Systems Overview. Chapter 2

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Threads. CSE 410, Spring 2004 Computer Systems.

Processes The Process Model. Chapter 2. Processes and Threads. Process Termination. Process Creation

CSCE 313: Intro to Computer Systems

Learning Outcomes. Processes and Threads. Major Requirements of an Operating System. Processes and Threads

HY 345 Operating Systems

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

Processes and Threads

Processes The Process Model. Chapter 2 Processes and Threads. Process Termination. Process States (1) Process Hierarchies

OPERATING SYSTEM. Chapter 4: Threads

Operating Systems 2 nd semester 2016/2017. Chapter 4: Threads

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

CS Lecture 3! Threads! George Mason University! Spring 2010!

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

CSC 539: Operating Systems Structure and Design. Spring 2006

Process Description and Control

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

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

CSE 4/521 Introduction to Operating Systems

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

Chapter 4: Threads. Chapter 4: Threads

Chapter 4: Multithreaded

2 Processes. 2 Processes. 2 Processes. 2.1 The Process Model. 2.1 The Process Model PROCESSES OPERATING SYSTEMS

CS 3305 Intro to Threads. Lecture 6

Announcement. Exercise #2 will be out today. Due date is next Monday

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

Chapter 4: Threads. Chapter 4: Threads

Major Requirements of an OS

Chapter 4: Multithreaded Programming

Chapter 4: Multithreaded Programming

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

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

Running. Time out. Event wait. Schedule. Ready. Blocked. Event occurs

Frequently asked questions from the previous class survey

1 PROCESSES PROCESS CONCEPT The Process Process State Process Control Block 5

Processes and Threads. Processes: Review

Processes & Threads. Process Management. Managing Concurrency in Computer Systems. The Process. What s in a Process?

CISC 7310X. C03: Process. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 2/15/2018 CUNY Brooklyn College

PROCESSES & THREADS. Charles Abzug, Ph.D. Department of Computer Science James Madison University Harrisonburg, VA Charles Abzug

Chapter 5: Threads. Outline

Threads Assistant Professor DCS Operating System Concepts

CS604 - Operating System Solved Subjective Midterm Papers For Midterm Exam Preparation

Processes and Non-Preemptive Scheduling. Otto J. Anshus

CS450/550 Operating Systems

CS307: Operating Systems

CS 326: Operating Systems. Process Execution. Lecture 5

HY345 - Operating Systems

OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL. Chap. 2.2 Interprocess Communication

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

Chapter 4: Multi-Threaded Programming

Lecture 4 Threads. (chapter 4)

Process Description and Control. Major Requirements of an Operating System

Major Requirements of an Operating System Process Description and Control

آنستیتیوت تکنالوجی معلوماتی و مخابراتی ICTI

CS450/550 Operating Systems

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

Threads Chapter 5 1 Chapter 5

Process Description and Control. Chapter 3

Chapter 5: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads

LECTURE 3:CPU SCHEDULING

CS 31: Intro to Systems Operating Systems Overview. Kevin Webb Swarthmore College March 31, 2015

CSE 153 Design of Operating Systems Fall 2018

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

Processes and Threads

Operating System. Operating System Overview. Layers of Computer System. Operating System Objectives. Services Provided by the Operating System

Operating System Overview. Operating System

CS510 Operating System Foundations. Jonathan Walpole

Operating Systems : Overview

Yi Shi Fall 2017 Xi an Jiaotong University

Processes Prof. James L. Frankel Harvard University. Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved.

IT 540 Operating Systems ECE519 Advanced Operating Systems

Operating System Control Structures


Process Description and Control. Chapter 3

CHAPTER-1: INTRODUCTION TO OPERATING SYSTEM:

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

Process Description and Control

Chapter 4 Multithreaded Programming

Threads. Threads The Thread Model (1) CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5

Definition Multithreading Models Threading Issues Pthreads (Unix)

CONTENTS. Computer-System Structures

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

Precept 2: Non-preemptive Scheduler. COS 318: Fall 2018

Transcription:

Preview The Thread Model Motivation of Threads Benefits of Threads Implementation of Thread Implement thread in User s Mode Implement thread in Kernel s Mode CS 431 Operating System 1

The Thread Model In traditional operating systems, each process has an address space and a single thread of control. But in modern software, it is desirable to have multiple threads of control in the same address space. Each thread inside a process needs its independent space for running, but shares the same address space as the program. CS 431 Operating System 2

The Thread Model Threads Threads are processes in a process!!! multiple executions in the same process environment. Each thread has its own: thread ID, program counter, register set, and stack. Different threads are not quite as independent as different processes since they share same address space. It shares with other threads belonging to the same process its code section, data section and other operating system resources such as files and child processes. CS 431 Operating System 3

The Thread Model Multiple processes running on a computer Processes share physical memory, disks, printers and other resources. Multiple threads running on a process the threads share an address space, open files, and other resources. Multiple threads working together By sharing a set of resources, threads can work together closely to perform some task. CS 431 Operating System 4

The Thread Model (a) Each of them operates in a different address space. (b) All three of them share the same address space. CS 431 Operating System 5

The Thread Model The CPU switches rapidly back and forth among the threads providing the illusion that the threads are running in parallel. (Same idea as multiprogramming) No protection between threads using same address space. (e.g. one thread can completely wipe out another thread s stack) A thread can be in any one of several states: running, blocked, ready. (like a traditional process) CS 431 Operating System 6

The Thread Model CS 431 Operating System 7

The Thread Model Each thread has its own stack Each thread s stack contains one frame for each procedure called but not returned. Multithreading processes normally start with a single thread present. This thread has the ability to create new threads by calling a library procedure (e.g. thread_create). When a thread has finished its work, it can exit by calling a library procedure (e.g. thread_exit). CS 431 Operating System 8

The Thread Model Each thread has its own stack for storing its execution history. CS 431 Operating System 9

Motivation of Threads With threads Instead of thinking about interrupts and context switches, we can think about parallel executions. Ability for the parallel entities to share an address space and all of its data. This is essential for certain applications. Faster creation: easier to create/destroy than processes. Useful on system with multiple CPUs, where real parallelism is possible CS 431 Operating System 10

Motivation of Threads Many software packages that run on modern desktop PC are multithreaded, e.g. web server, word processor. An application typically is implemented as a separate process with several threads of control. CS 431 Operating System 11

Motivation of Threads Ex) Web browser 1. A thread for displaying images or text 2. A thread for retrieving data from network Ex) Word processor 1. A thread for displaying graphics 2. A thread for reading input from keyboard 3. A thread for performing spelling and grammar checking in the background CS 431 Operating System 12

Benefits of Thread Threads share resources. Light weighted allocating memory and resources for each process creation is expensive. Utilization of multi-processor system each thread in a process can run on one CPU in multi-processor system. It can greatly shorten the run time. CS 431 Operating System 13

Threads Implementation Implementing thread in user space Threads are handled by the run-time system OS does not know the existence of threads Implementing thread in the kernel Thread are handled by OS CS 431 Operating System 14

Implementing Threads in User s space CS 431 Operating System 15

Implementing Threads in User s space The kernel does not know anything about threads. It is just managing single-thread process. The first advantage user-level threads package can be implemented on an operating system that does not support threads. The threads run on top of a run-time system (correction of procedures that manage threads thread_create, thread_exit, thread_yield) CS 431 Operating System 16

Implementing Threads in User s space Each process needs its own private thread table to keep track of the threads in that process (managed by the run-time system). Thread table stack pointer, program counter, registers, state, and so on When a thread s state become ready or blocked state, all information regarding the thread is stored in its own thread table. CS 431 Operating System 17

Implementing Threads in User s space When a running thread needed to be blocked (waiting for a job done by other thread), 1. it calls a run-time system procedure. 2. The run-time system procedure saves the thread s information 3. Choose one thread from the ready queue 4. Load new thread s information and run it When a thread is finished running for the moment, thread_yield is called (which is running in user s space) saves the thread s information and call scheduler, which is also running on user s space. CS 431 Operating System 18

Implementing Threads in User s space User-level thread allows each process to have its own customized scheduling algorithm User-level threads are generally fast to create and manage, but they have drawbacks. If the kernel is single-threaded, then any user-level thread performing a blocking system call will cause the entire process to block, even though other threads are available to run within the application. POSIX (Pthreads), Math (C-threads), Solaris 2 (UIthreads) CS 431 Operating System 19

Implementing Threads in Kernel s Space CS 431 Operating System 20

Implementing Threads in Kernel s Space It is directly supported by operating system The kernel performs thread creation, scheduling and management (keeps track of a thread table for each thread) in kernel s space. Because thread management is done by the operating system, kernel threads are generally slower to create and manage than the user-level thread. CS 431 Operating System 21

Implementing Threads in Kernel s Space Since the kernel is managing the threads, if a thread performs a blocking system call, the kernel can schedule another thread in the application for execution. In a multiprocessor environment, the kernel can schedule threads on different processors. Windows NT, Windows 2000, Solaris 2, BeOS, Tru64 UNIX CS 431 Operating System 22

Interprocess Communication Three issues in interprocess communication 1. How one process can pass information to another? 2. How to make sure two or more processes do not get into the critical region? 3. Proper sequencing when dependencies are present (e.g. A create outputs, B consume the outputs). CS 431 Operating System 23

Interprocess Communication (Race Condition) Race Condition A situation where two or more processes are reading or writing some shared data and the final result depends on the order in which the processes run. CS 431 Operating System 24

Interprocess Communication (Race Condition) Slots for file names When a process wants to print a file, it enters a file name in a special spooler directory Printer daemon periodically checks spooler directory if any file needs to be printed. CS 431 Operating System 25

Interprocess Communication (Race Condition) How to avoid race condition? Mutual exclusion some way of making sure that if one process is using a shared variable or file, the other processes will be excluded from using the same variable or file. The choice of the algorithm for achieving mutual exclusion is a major design issue in any operating system. CS 431 Operating System 26

Interprocess Communication (Race Condition) CS 431 Operating System 27

Interprocess Communication (Race Condition) Critical section (critical region) The part of program where the shared memory is accessed. If we could arrange matters such that no two processes were ever in their critical regions at the same time, we can avoid race conditions. CS 431 Operating System 28

Interprocess Communication (Race Condition) A solution for the race condition should have following conditions 1. No two processes may be simultaneously inside their critical regions mutual exclusion 2. No process running outside its critical region may block other processes 3. No process should have to wait forever to enter critical region 4. No assumptions may be made about speeds or the number of CPUs. CS 431 Operating System 29