Threads CSE 2431: Introduction to Operating Systems

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,
Chapter 3 Process Description and Control
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 Section 2.2. Introduction to threads A thread (of execution) is a light-weight process –Threads reside within processes. –They share one address.
Page 1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
3.5 Interprocess Communication
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
1 Chapter 4 Threads Threads: Resource ownership and execution.
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
Process by Dr. Amin Danial Asham. References Operating System Concepts ABRAHAM SILBERSCHATZ, PETER BAER GALVIN, and GREG GAGNE.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
CS333 Intro to Operating Systems Jonathan Walpole.
Chapter 2 Processes and Threads Introduction 2.2 Processes A Process is the execution of a Program More specifically… – A process is a program.
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.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Cs431-cotter1 Processes and Threads Tanenbaum 2.1, 2.2 Crowley Chapters 3, 5 Stallings Chapter 3, 4 Silberschaz & Galvin 3, 4.
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.
Advanced Operating Systems CS6025 Spring 2016 Processes and Threads (Chapter 2)
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
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,
Processes and threads.
Process Management Process Concept Why only the global variables?
Chapter 3: Process Concept
CS 6560: Operating Systems Design
OPERATING SYSTEMS CS3502 Fall 2017
Threads in C Caryl Rahn.
CS399 New Beginnings Jonathan Walpole.
Chapter 4: Multithreaded Programming
Chapter 4 Threads.
Intro to Processes CSSE 332 Operating Systems
Threads and Cooperation
Threads & multithreading
Lecture 10: Threads Implementation
Processes in Unix, Linux, and Windows
System Structure and Process Model
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Process & its States Lecture 5.
Realizing Concurrency using the thread model
Process Description and Control
Lecture Topics: 11/1 General Operating System Concepts Processes
Process Description and Control
Threads Chapter 4.
Chapter 2 Processes and Threads 2.1 Processes 2.2 Threads
Process Description and Control
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
Process Description and Control
Jonathan Walpole Computer Science Portland State University
Process Description and Control
Operating Systems Threads 1.
Process Description and Control
Realizing Concurrency using Posix Threads (pthreads)
Process Description and Control
Lecture 10: Threads Implementation
Realizing Concurrency using Posix Threads (pthreads)
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
Chapter 2 Processes and Threads 2.1 Processes 2.2 Threads
CS510 Operating System Foundations
Process Description and Control
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.
Chapter 3: Process Management
CS Introduction to Operating Systems
Presentation transcript:

Threads CSE 2431: Introduction to Operating Systems Instructor: Adam C. Champion, Ph.D. Reading: Chapter 4, [OSC] (except Section 4.5)

Contents What Are Threads? Why Use Threads? Thread Usage Thread Review Thread Implementations Issues Making Single-Threaded Code Multi-threaded

Process Review What Is A Process? What’s in a process? It’s one executing instance of a “program” It’s separate from other instances It can start (“launch”) other processes What’s in a process? Code (text), data, stack, heap Process control block Process state, priority, accounting Program counter, register variables, stack pointers, etc. Open files and devices

Process Model: Another Perspective Resource grouping Address space (text, data, etc.) Opened files, signal handlers Accounting info … A thread of execution Program counter Registers Stack State

Threads: “Lightweight Processes” 2 3 (a) Three processes with one thread each (b) One process with three threads

Thread Model Shared information (per process items) Process info: parent process, time, etc. Memory: segments, page table, etc. I/O and files: comm ports, directories and file descriptors, etc. Private state (per thread items) State (ready, running, and blocked) Registers Program counter Execution stack Each thread executes separately Any protection between threads?

Why Is Stack Private?

An Example Program #include “csapp.h" void *thread(void *vargp); int main() { pthread_t pid; // stores the new thread ID pthread_create(&tid, NULL, thread, NULL); // create a new thread pthread_join(tid, NULL); // main thread waits for // the other thread to terminate exit(0); /* main thread exits */ } void *thread(void *vargp) { /* thread routing */ printf("Hello, world! \n"); return NULL;

Windows Threads

Discussion Processes vs. Threads Real life analogies? Similarities Differences Real life analogies?

Real Life Analogy Process Threads Share Some may concurrently execute Working on Lab 1 assignment Threads Looking for references Coding Running & Testing Writing report Share The same context (Lab 1) Some may concurrently execute May affect each other

Contents What Are Threads? Why Use Threads? Thread Usage Thread Review Thread Implementations Issues Making Single-Threaded Code Multi-threaded

Why Threads? Good things about threads Thread usage examples Light-weight Easy for sharing Thread usage examples Word processor Web server

Thread Usage: Word Processor A thread can wait for I/O, while the other threads are still running. What if it is single-threaded?

Thread Usage: Web Server (1)

Thread Usage: Web Server (2) (a) Dispatcher thread (b) Worker thread How to do it with a single thread? What’s the tradeoff? How do you do it with a single thread while still providing parallelism?

Tradeoffs Three ways to construct a server

Thread Review A lightweight process Efficient for creation Easy for sharing Sharing resources with other threads in same process Address space (text, data, etc.) Opened files, signal handlers Accounting info Thread-private items (for execution) Program counter Registers Stack Execution state

Contents What Are Threads? Why Use Threads? Thread Usage Thread Implementations Issues Making Single-Threaded Code Multi-threaded

Thread Implementations User-level threads Kernel-level threads Hybrid implementation

Implementing Threads in User Space A user-level thread package

Challenges How to implement a thread package at user-level (without modifying the OS)? Is your implementation most efficient? Any performance problem? How to address it? Blocking I/O? Page faults? Infinite loop?

Blocking Vs. Non-blocking System Calls Usually I/O related: read(), fread(), getc(), write() Doesn’t return until the call completes The process/thread is switched to blocked state When I/O completes, process/thread becomes ready Simple Real-life example: a phone call Non-blocking system calls for I/O Asynchronous I/O Complicated The call returns once I/O is initiated, and caller continues Once I/O completes, an interrupt is delivered to the caller Real-life example: registered mail

Implementing Threads in the Kernel A thread package managed by the kernel

User-Level vs. Kernel-Level What is the difference between user-level and kernel-level threads? What are the tradeoffs? When a user-level thread is blocked on an I/O event, the whole process is blocked A context switch of kernel-level threads is expensive Can we do better?

Hybrid Implementations Multiplexing user-level threads onto some kernel-level threads

Scheduler Activations Another way to combine advantages of user-level, kernel-level threads A little complicated Involving upcalls Violating some fundamental principles Read Section 4.4.6 or: T. Anderson, B. Bershad, E. Lazowska, and H. Levy. “Scheduler Activations: Effective Kernel Support for the User-Level Management of Parallelism”. In ACM Trans. on Computer Systems, 10(1), Feb. 1992, pp. 53-79.

Contents What Are Threads? Why Use Threads? Thread Usage Thread Review Thread Implementations Issues Making Single-Threaded Code Multi-threaded

Issues Making Single-Threaded Code Multi-Threaded Sharing of Global Variables Non-reentrant library procedures Signals Stack Management

Example: Sharing Global Variable Problem Conflicts between threads over use of a global variable

Solution: Private Global Variables

Non-Reentrant Library Routines addNodeToTail(List l, Node m) { tail = getTail(l); // interrupted tail->next = m; l->tail = m; } addNodeToTail(List l, Node m) { tail = getTail(l); tail->next = m; l->tail = m; }

Signals Delivered the signal to One thread? A set of threads? All threads? Confliction of signal handler registration

Stack Management Single-threaded process Automatically grow once the stack overflows (until some point) How about multi-threaded process? Kernel may not be aware of multiple threads in one process

Summary What Is Thread? Why Thread? Thread Usages Thread Review Thread Implementations Issues Making Single-Threaded Code Multi-threaded