From Processes to Threads

Similar documents
Processes, Threads and Processors

Processes and Threads. From Processes to Threads. The Case for Threads. The Case for Threads. Process abstraction combines two concepts.

From Processes to Threads

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

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

Yi Shi Fall 2017 Xi an Jiaotong University

Concurrent Programming with Threads: Why you should care deeply

COMP 530: Operating Systems Concurrent Programming with Threads: Why you should care deeply

Concurrent Programing: Motivation, Theory, Practice. Emmett Witchel First Bytes Teacher Conference July 2008

Concurrent Programing: Motivation, Theory, Practice. Emmett Witchel First Bytes Teacher Conference July 2008

CSE 153 Design of Operating Systems Fall 2018

Threads (light weight processes) Chester Rebeiro IIT Madras

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

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

THREADS. Jo, Heeseung

Chapter 4: Threads. Operating System Concepts. Silberschatz, Galvin and Gagne

OPERATING SYSTEM. Chapter 4: Threads

CSE 4/521 Introduction to Operating Systems

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

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

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

Multithreaded Programming

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

Chapter 4: Threads. Chapter 4: Threads

Chapter 4: Multithreaded Programming

Agenda. Threads. Single and Multi-threaded Processes. What is Thread. CSCI 444/544 Operating Systems Fall 2008

Chapter 5: Threads. Outline

4.8 Summary. Practice Exercises

CSE 120 Principles of Operating Systems

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

Chapter 3 Process Description and Control

Computer Systems Laboratory Sungkyunkwan University

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

CS 450 Operating System Week 4 Lecture Notes

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

CSci 4061 Introduction to Operating Systems. (Thread-Basics)

CS510 Operating System Foundations. Jonathan Walpole

CSE 120 Principles of Operating Systems

CS 318 Principles of Operating Systems

Chapter 4: Multithreaded

Last class: Today: Thread Background. Thread Systems

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

CS 318 Principles of Operating Systems

Processes and Threads

CS333 Intro to Operating Systems. Jonathan Walpole

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

Notes. CS 537 Lecture 5 Threads and Cooperation. Questions answered in this lecture: What s in a process?

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

Chapter 4: Threads. Chapter 4: Threads. Overview Multicore Programming Multithreading Models Thread Libraries Implicit Threading Threading Issues

CSE 120 Principles of Operating Systems

Chapter 4: Threads. Chapter 4: Threads

Processes, Threads, SMP, and Microkernels

Threads. CS-3013 Operating Systems Hugh C. Lauer. CS-3013, C-Term 2012 Threads 1

Concurrency, Thread. Dongkun Shin, SKKU

Chapter 4: Threads. Operating System Concepts 9 th Edition

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

Chapter 4: Threads. Operating System Concepts 9 th Edition

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

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

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

Last Class: CPU Scheduling. Pre-emptive versus non-preemptive schedulers Goals for Scheduling: CS377: Operating Systems.

Questions answered in this lecture: CS 537 Lecture 19 Threads and Cooperation. What s in a process? Organizing a Process

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

csci3411: Operating Systems

CS420: Operating Systems

Processes & Threads. (Chapter 3) CS 4410 Operating Systems. [R. Agarwal, L. Alvisi, A. Bracy, M. George, E. Sirer, R. Van Renesse]

Threads. Threads (continued)

CISC2200 Threads Spring 2015

Processes and Threads

Chapter 4: Multithreaded Programming

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

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

Process Description and Control

Processes. Dr. Yingwu Zhu

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

Chapter 4: Multithreaded Programming

Chapter 4: Multithreaded Programming

Processes and Threads

Lecture Topics. Announcements. Today: Threads (Stallings, chapter , 4.6) Next: Concurrency (Stallings, chapter , 5.

CS 326: Operating Systems. Process Execution. Lecture 5

Processes. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

Lecture 4: Threads; weaving control flow

POSIX Threads: a first step toward parallel programming. George Bosilca

CS 3305 Intro to Threads. Lecture 6

4. Concurrency via Threads

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

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

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

Threads. Raju Pandey Department of Computer Sciences University of California, Davis Spring 2011

CS307: Operating Systems

!! How is a thread different from a process? !! Why are threads useful? !! How can POSIX threads be useful?

Operating Systems & Concurrency: Process Concepts

CS 31: Intro to Systems Threading & Parallel Applications. Kevin Webb Swarthmore College November 27, 2018

Definition Multithreading Models Threading Issues Pthreads (Unix)

Processes and Threads. Processes: Review

Chapter 4: Multi-Threaded Programming

OS lpr. www. nfsd gcc emacs ls 1/27/09. Process Management. CS 537 Lecture 3: Processes. Example OS in operation. Why Processes? Simplicity + Speed

殷亚凤. Processes. Distributed Systems [3]

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

Transcription:

From Processes to Threads 1

Processes, Threads and Processors Hardware can interpret N instruction streams at once Uniprocessor, N==1 Dual-core, N==2 Sun s Niagra T2 (2007) N == 64, but 8 groups of 8 An OS can run 1 process on each processor at the same time Concurrent execution increases perforamnce An OS can run 1 thread on each processor at the same time 2

Processes and Threads Process abstraction combines two concepts Concurrency Each process is a sequential execution stream of instructions Protection Each process defines an address space Address space identifies all addresses that t can be touched by the program Threads Key idea: separate the concepts of concurrency from protection A thread represents a sequential execution stream of instructions A process defines the address space that may be shared by multiple threads Threads can execute on different cores on a multicore CPU (parallelism for performance) and can communicate with other threads by updating memory 3

The Case for Threads Consider the following code fragment for(k = 0; k < n; k++) a[k] = b[k] * c[k] + d[k] * e[k]; Is there a missed opportunity here? On a Ui Uni-processor? On a Multi-processor? 4

The Case for Threads Consider a Web server get network message from client get URL data from disk compose response send response How well does this web server perform? 5

Introducing Threads A thread represents an abstract entity that executes a sequence of instructions It has its own set of CPU registers It has its own stack There is no thread-specific heap or data segment (unlike process) Threads are lightweight Creating a thread more efficient than creating a process. Communication between threads easier than btw. processes. Context switching between threads requires fewer CPU cycles and memory references than switching processes. Threads only track a subset of process state (share list of open files, pid, ) Examples: OS-supported: Sun s LWP, POSIX s threads Language-supported: Modula-3, Java 6

Programmer s View void fn1(int arg0, int arg1, ) { } main() { tid = CreateThread(fn1, arg0, arg1, ); } At the point CreateThread is called, execution continues in parent thread in main function, and execution starts at fn1 in the child thread, both in parallel 7

How Can it Help? Consider the following code fragment for(k = 0; k < n; k++) a[k] = b[k] * c[k] + d[k] * e[k]; Rewrite this code fragment as: CreateThread(fn, 0, n/2); CreateThread(fn, n/2, n); fn(l, m) { for(k = l; k < m; k++) a[k] = b[k] * c[k] + d[k] * e[k]; } What did we gain? 8

How Can it Help? Consider a Web server Create a number of threads, and for each thread do get network message from client get URL data from disk compose response send response What did we gain? 9

Overlapping Requests (Concurrency) Request 1 Request 2 get network message from client get URL data from disk compose response send response get network message from client get URL data from disk compose response send response Time 10

Threads vs. Processes Threads A thread has no data segment or heap A thread cannot live on its own, it must live within a process There can be more than one thread in a process, the first thread calls main & has the process s stack If a thread dies, its stack is reclaimed Inter-thread communication via memory. Each thread can run on a different physical processor Processes A process has code/data/heap & other segments There must be at least one thread in a process Threads within a process share code/data/heap, share I/O, but each has its own stack & registers If a process dies, its resources are reclaimed & all threads die Inter-process communication via OS and data copying. Each process can run on a 11

Implementing Threads Processes define an address space; threads share the address space Process Control Block (PCB) contains process-specific specific information Owner, PID, heap pointer, priority, active thread, and pointers to thread information TCB for Thread1 PC SP State Registers TCB for Thread2 Process s address space mapped segments DLL s Heap Stack thread2 Thread Control Block (TCB) PC Stack thread1 SP contains thread-specific State information Initialized data Registers Stack pointer, PC, thread state t (running, ), register Code values, a pointer to PCB, 12

Threads Life Cycle Threads (just like processes) go through a sequence of start, ready, running, waiting, and done states Start Done Ready Running Waiting 13

User-level vs. Kernel-level threads Process0 Process1 User-level threads (M to N model) + Fast to create and switch + Natural fit for language-level threads - All user-level threads in process block on OS calls E.g., g, read from file can block all threads -User-level scheduler can fight with kernel-level scheduler user kernel Kernel-level threads (1 to 1 model) + Kernel-level threads do not block process for syscall + Only one scheduler (and kernel has global view) - Can be difficult to make efficient (create & switch) 14

Languages vs. Systems Kernel-level threads have won for systems Linux, Solaris 10, Windows pthreads tends to be kernel-level threads User-level threads still used for languages (Java) User tells JVM how many underlying system threads Default: 1 system thread Java runtime intercepts blocking calls, makes them non- blocking JNI code that makes blocking syscalls can block JVM 15

Thread or Process Pool Creating a thread or process for each unit of work (e.g., user request) is dangerous High overhead to create & delete thread/process Can exhaust CPU & memory resource Thread/process pool controls resource use Allows service to be well conditioned. d Throu ughput Well conditioned Not well conditioned Load 16