Threads. Announcements CSUGLab accounts are ready Fixed homework 1 submissions using CMS.

Slides:



Advertisements
Similar presentations
Operating Systems ECE344 Lecture 4: Threads Ding Yuan.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Multithreaded Programming.
Threads. Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Threads.
Chapter 4: Multithreaded Programming
Chapter 5 Threads os5.
Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
Course: Operating Systems Instructor: Umar Kalim NUST Institute of Information Technology, Pakistan Operating Systems.
Threads. Announcements Cooperating Processes Last time we discussed how processes can be independent or work cooperatively Cooperating processes can.
Threads. Announcements CS 4410 Homework is TODAY (11:59pm) –available and turn in via CMS CS 4411 initial design documents yesterday/today –Project due.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Chapter 4: Threads. 2 What’s in a process? A process consists of (at least): –an address space –the code for the running program –the data for the running.
Threads vs. Processes April 7, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
3.5 Interprocess Communication
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
Threads CSCI 444/544 Operating Systems Fall 2008.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Chapter 4: Threads.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Chapter 4: Threads.
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 Threads Implementation.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Chapter 4: Threads READ 4.1 & 4.2 NOT RESPONSIBLE FOR 4.3 &
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.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
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.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
CS 153 Design of Operating Systems Spring 2015
1 From Processes to Threads. 2 Processes, Threads and Processors Hardware can interpret N instruction streams at once  Uniprocessor, N==1  Dual-core,
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Chapter 4 MultiThreaded Programming. 2015/9/18os Outline Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows XP/2000.
Chapter 4: Threads. From Processes to Threads 4.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Threads.
Thread. A basic unit of CPU utilization. It comprises a thread ID, a program counter, a register set, and a stack. It is a single sequential flow of control.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
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.
Chapter 4: Threads. 4.2 Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2011Operating System Concepts Essentials – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Operating System Concepts Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Review The Joys and Pains of Threads and Multithreading –what is a thread –threads vs. processes –opportunities and risks.
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. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads Modified from the slides of the text book. TY, Sept 2010.
Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts What is Thread “Thread is a part of a program.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Department of Computer Science and Software Engineering
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Processes & Threads Introduction to Operating Systems: Module 5.
CSE 153 Design of Operating Systems Winter 2015 Lecture 4: Threads.
Saurav Karmakar. Chapter 4: Threads  Overview  Multithreading Models  Thread Libraries  Threading Issues  Operating System Examples  Windows XP.
Threads, SMP, and Microkernels Chapter 4. Processes and Threads Operating systems use processes for two purposes - Resource allocation and resource ownership.
Lecturer 3: Processes multithreaded Operating System Concepts Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
Threads & Multithreading
CSE 120 Principles of Operating
CSE 153 Design of Operating Systems Winter 2018
Threads vs. Processes Hank Levy 1.
CSE 153 Design of Operating Systems Winter 2019
Threads.
Presentation transcript:

Threads

Announcements CSUGLab accounts are ready Fixed homework 1 submissions using CMS

Cooperating Processes Processes can be independent or work cooperatively Cooperating processes can be used: –to gain speedup by overlapping activities or working in parallel –to better structure an application as set of cooperating processes –to share information between jobs Sometimes processes are structured as a pipeline –each produces work for the next stage that consumes it

main() read_data() for(all data) compute(); CreateProcess(write_data()); endfor Case for Parallelism main() read_data() for(all data) compute(); write_data(); endfor

Consider the following code fragment for(k = 0; k < n; k++) a[k] = b[k] * c[k] + d[k] * e[k]; CreateProcess(fn, 0, n/2); CreateProcess(fn, n/2, n); fn(l, m) for(k = l; k < m; k++) a[k] = b[k] * c[k] + d[k] * e[k]; Case for Parallelism

Consider a Web server create a number of process, and for each process do – get network message from client –get URL data from disk –compose response –send response

Processes and Threads A full process includes numerous things: –an address space (defining all the code and data pages) –OS resources and accounting information –a “thread of control”, defines where the process is currently executing That is the PC and registers Creating a new process is costly –all of the structures (e.g., page tables) that must be allocated Communicating between processes is costly –most communication goes through the OS

Parallel Programs To build parallel programs, such as: –Parallel execution on a multiprocessor –Web server to handle multiple simultaneous web requests We will need to: –Create several processes that can execute in parallel –Cause each to map to the same address space because they’re part of the same computation –Give each its starting address and initial parameters –The OS will then schedule these processes in parallel Overheads: Cost of creation Cost of coordination Lots of duplication, e.g. address space, protection, etc.

“Lightweight” Processes What’s similar in these processes? –They all share the same code and data (address space) –They all share the same privileges –They share almost everything in the process What don’t they share? –Each has its own PC, registers, and stack pointer Idea: why don’t we separate the idea of process (address space, accounting, etc.) from that of the minimal “thread of control” (PC, SP, registers)?

Threads and Processes Most operating systems therefore support two entities: –the process, which defines the address space and general process attributes –the thread, which defines a sequential execution stream within a process A thread is bound to a single process. –For each process, however, there may be many threads. Threads are the unit of scheduling Processes are containers in which threads execute

Multithreaded Processes

Threads vs. Processes 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 Inexpensive creation Inexpensive context switching If a thread dies, its stack is reclaimed 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 Expensive creation Expensive context switching If a process dies, its resources are reclaimed & all threads die

How OSes support threads? example: MS/DOSexample: Unix example: Xerox Pilot example: Mach, OSF, Chorus, NT = address space = thread

Separating Threads and Processes Makes it easier to support multithreaded applications –Different from multiprocessing, multiprogramming, multitasking Concurrency (multithreading) is useful for: –improving program structure –handling concurrent events (e.g., web requests) –building parallel programs –Resource sharing –Multiprocessor utilization Is multithreading useful even on a single processor?

Cooperative Threads Each thread runs until it decides to give up the CPU main() { tid t1 = CreateThread(fn, arg); … Yield(t1); } fn(int arg) { … Yield(any); }

Cooperative Threads Cooperative threads use non pre-emptive scheduling Advantages: –Simple Scientific apps Disadvantages: –For badly written code Scheduler gets invoked only when Yield is called A thread could yield the processor when it blocks for I/O

Non-Cooperative Threads No explicit control passing among threads Rely on a scheduler to decide which thread to run A thread can be pre-empted at any point Often called pre-emptive threads Most modern thread packages use this approach

Kernel Threads Also called Lightweight Processes (LWP) Kernel threads still suffer from performance problems Operations on kernel threads are slow because: –a thread operation still requires a system call –kernel threads may be overly general to support needs of different users, languages, etc. –the kernel doesn’t trust the user there must be lots of checking on kernel calls

User-Level Threads For speed, implement threads at the user level A user-level thread is managed by the run-time system –user-level code that is linked with your program Each thread is represented simply by: –PC –Registers –Stack –Small control block All thread operations are at the user-level: –Creating a new thread –switching between threads –synchronizing between threads

User-Level Threads User-level threads –the thread scheduler is part of a library, outside the kernel –thread context switching and scheduling is done by the library –Can either use cooperative or pre-emptive threads cooperative threads are implemented by: – CreateThread(), DestroyThread(), Yield(), Suspend(), etc. pre-emptive threads are implemented with a timer (signal) –where the timer handler decides which thread to run next

Example User Thread Interface t = thread_fork(initial context) create a new thread of control thread_stop() stop the calling thread, sometimes called thread_block thread_start(t) start the named thread thread_yield() voluntarily give up the processor thread_exit() terminate the calling thread, sometimes called thread_destroy

Key Data Structures your process address space your program: for i (1, 10, I++) thread_fork(I); …. user-level thread code: proc thread_fork()… proc thread_block()… proc thread_exit()... queue of thread control blocks per-thread stacks your data (shared by all your threads):

Multiplexing User-Level Threads The user-level thread package sees a “virtual” processor(s) –it schedules user-level threads on these virtual processors –each “virtual” processor is implemented by a kernel thread The big picture: –Create as many kernel threads as there are processors –Create as many user-level threads as the application needs –Multiplex user-level threads on top of the kernel-level threads Why not just create as many kernel-level threads as app needs? –Context switching –Resources

User-Level vs. Kernel Threads User-Level Managed by application Kernel not aware of thread Context switching cheap Create as many as needed Must be used with care Kernel-Level Managed by kernel Consumes kernel resources Context switching expensive Number limited by kernel resources Simpler to use Key issue: kernel threads provide virtual processors to user-level threads, but if all of kthreads block, then all user-level threads will block even if the program logic allows them to proceed

Many-to-One Model LWP user-level threads Thread creation, scheduling, synchronization done in user space. Mainly used in language systems, portable libraries Fast - no system calls required Few system dependencies; portable No parallel execution of threads - can’t exploit multiple CPUs All threads block when one uses synchronous I/O

One-to-one Model More concurrency Better multiprocessor performance Each user thread requires creation of kernel thread Each thread requires kernel resources; limits number of total threads LWP user-level threads LWP Thread creation, scheduling, synchronization require system calls Used in Linux Threads, Windows NT, Windows 2000, OS/2

Many-to-Many Model LWP user-level threads LWP If U < L? No benefits of multithreading If U > L, some threads may have to wait for an LWP to run Active thread - executing on an LWP Runnable thread - waiting for an LWP A thread gives up control of LWP under the following: – synchronization, lower priority, yielding, time slicing

Two-level Model LWP user-level threads LWP Combination of one-to-one + “strict” many-to-many models Supports both bound and unbound threads –Bound threads - permanently mapped to a single, dedicated LWP –Unbound threads - may move among LWPs in set Thread creation, scheduling, synchronization done in user space Flexible approach, “best of both worlds” Used in Solaris implementation of Pthreads and several other Unix implementations (IRIX, HP-UX)

Multithreading Issues Semantics of fork() and exec() system calls Thread cancellation –Asynchronous vs. Deferred Cancellation Signal handling –Which thread to deliver it to? Thread pools –Creating new threads, unlimited number of threads Thread specific data Scheduler activations –Maintaining the correct number of scheduler threads

Thread Hazards int a = 1, b = 2, w = 2; main() { CreateThread(fn, 4); while(w) ; } fn() { int v = a + b; w--; }

Concurrency Problems A statement like w-- in C (or C++) is implemented by several machine instructions: ldr4, #w addr4, r4, -1 str4, #w Now, imagine the following sequence, what is the value of w? ldr4, #w ______________ addr4, r4, -1 str4, #w ______________ ldr4, #w addr4, r4, -1 str4, #w