CS 2200 Presentation 19 Threads. Test Preview A misguided java programmer is looking at the specs for a computer. It's a byte addressed 32-bit model equipped.

Slides:



Advertisements
Similar presentations
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Advertisements

Chapter 4 Threads Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William.
Multiprocessors and Multithreading – classroom slides.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Chapter 4 Threads, SMP, and Microkernels Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design.
Chapter 5 Threads os5.
Computer Systems/Operating Systems - Class 8
Operating Systems Parallel Systems (Now basic OS knowledge)
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,
Lecture 4: Concurrency and Threads CS 170 T Yang, 2015 Chapter 4 of AD textbook.
Threads. Announcements Cooperating Processes Last time we discussed how processes can be independent or work cooperatively Cooperating processes can.
Operating Systems Parallel Systems and Threads (Soon to be basic OS knowledge)
CS238 Lecture 5 Threads Dr. Alan R. Davis. Threads Definitions Benefits User and Kernel Threads Multithreading Models Solaris 2 Threads Java 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
Threads CSCI 444/544 Operating Systems Fall 2008.
1 Chapter 4 Threads Threads: Resource ownership and execution.
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
CS 3013 & CS 502 Summer 2006 Threads1 CS-3013 & CS-502 Summer 2006.
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.
Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place  (address space + resources) –Thread is a program.
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.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Chapter 4 Threads, SMP, and Microkernels Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E.
9/13/20151 Threads ICS 240: Operating Systems –William Albritton Information and Computer Sciences Department at Leeward Community College –Original slides.
Operating Systems Lecture 09: Threads (Chapter 4)
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Multithreading Allows application to split itself into multiple “threads” of execution (“threads of execution”). OS support for creating threads, terminating.
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.
Chapter 4 Threads, SMP, and Microkernels Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design.
Processes and Threads Processes have two characteristics: – Resource ownership - process includes a virtual address space to hold the process image – Scheduling/execution.
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
1 Threads, SMP, and Microkernels Chapter 4. 2 Process Resource ownership: process includes a virtual address space to hold the process image (fig 3.16)
CS333 Intro to Operating Systems Jonathan Walpole.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Chapter 4 – Threads (Pgs 153 – 174). Threads  A "Basic Unit of CPU Utilization"  A technique that assists in performing parallel computation by setting.
1 Threads, SMP, and Microkernels Chapter Multithreading Operating system supports multiple threads of execution within a single process MS-DOS.
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.
Department of Computer Science and Software Engineering
Module 2.0: Threads.
Processes & Threads Introduction to Operating Systems: Module 5.
1 OS Review Processes and Threads Chi Zhang
Threads-Process Interaction. CONTENTS  Threads  Process interaction.
Threads. Readings r Silberschatz et al : Chapter 4.
1 Threads, SMP, and Microkernels Chapter 4. 2 Process Resource ownership - process includes a virtual address space to hold the process image Scheduling/execution-
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.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
CS 6560: Operating Systems Design
Processes and Threads Processes and their scheduling
CS399 New Beginnings Jonathan Walpole.
Chapter 4 Threads.
Threads & multithreading
Threads, SMP, and Microkernels
Lecture 10: Threads Implementation
Threads Chapter 4.
Threads Chapter 4.
Jonathan Walpole Computer Science Portland State University
Lecture 10: Threads Implementation
CS510 Operating System Foundations
CS703 – Advanced Operating Systems
Presentation transcript:

CS 2200 Presentation 19 Threads

Test Preview A misguided java programmer is looking at the specs for a computer. It's a byte addressed 32-bit model equipped with a cache. The memory reference is divided like this: tag 19, index 7, offset 6. How many lines are in the cache? /2 Meg 4.None of the above

Questions?

Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy

Recall Process –Program Counter –Registers –Stack –Code (Text) –Data –Page Table –etc. Processes must be protected from one another. Memory

Recall Context Switching –Requires considerable work What about a single users application? –Is there a way to make it more efficient –In effect, allow the user to have multiple processes executing in the same space? –Yes, solution: Threads or Multithreading

What is Multithreading? Technique allowing program to do multiple tasks –Example: Java GUI's

Java Event Handling import java.awt.*; public class HelloGUI { public static void main (String[ ] arg) { System.out.println (“About to make GUI”); Frame f = new Frame (“Hello GUIs”); f.setSize( 200, 200 ); f.show(); System.out.println (“Finished making GUI”); }// main }// class HelloGUI callback Our event handling code The code trapping this event appears in the graphics thread

What is Multithreading? Technique allowing program to do multiple tasks –Example: Java GUI's Is it a new technique? –has existed since the 70’s (concurrent Pascal, Ada tasks, etc.) Why now? –“Time has come for this technology” –Emergence of SMP’s in particular

SMP? What is an SMP? –Multiple CPUs in a single box sharing all the resources such as memory and I/O Is a dual-processor SMP more cost effective than two uniprocessor boxes? –Yes, (roughly 20% more for a dual processor SMP compared to a uniprocessor). –Modest speedup for a program on a dual- processor SMP over a uniprocessor will make it worthwhile.

What is a Thread? Basic unit of CPU utilization A lightweight process (LWP) Consists of –Program Counter –Register Set –Stack Space Shares with peer threads –Code –Data –OS Resources Open files Signals

Threads Can be context switched more easily –Registers and PC –Not memory management Can run on different processors concurrently in an SMP Share CPU in a uniprocessor May (Will) require concurrency control programming like mutex locks.

active Process Threads in a Uniprocessor? Allows concurrency between I/O and user processing even in a uniprocessor box

Threads and OS Traditional OS DOS –Memory layout user kernel Data –Protection between user and kernel? Data Program DOS Code

User Kernel Kernel code and data –Protection between user and kernel? Process code and data PCB P1 Process code and data PCB P2 Threads and OS Unix - memory layout

Threads and OS Programs in a traditional OS are single threaded –One PC per program (process), one stack, one set of CPU registers –If a process blocks (say disk I/O, network communication, etc.) then no progress for the program as a whole

MultiThreaded Operating Systems How widespread is support for threads in OS? –Digital Unix, Sun Solaris, Win9x, Win NT, Win2k, Linux, Free BSD, etc Process vs. Thread? –In a single threaded program, the state of the executing program is contained in a process –In a multithreaded program, the state of the executing program is contained in several ‘concurrent’ threads

What is this? 1.Crack in slide 2.Hair stuck on lens 3.Symbol for thread 4.San Andreas Fault

Process Vs. Thread user kernel Kernel code and data t1 –Computational state (PC, regs, …) for each thread –How different from process state? codedatacodedata t2t3t1 PCB P1P2

Memory Layout Multithreaded program has a per-thread stack Heap, static, and code are common to all threads MT program stk1 stk2 stk3... Heap Static Code ST program stack1 Heap Static Code

Threads Threaded code different from non- threaded? –Protection for data shared among threads Mutex –Synchronization among threads Way for threads to talk (signal) to one another –Thread-safe libraries NOTES strtok is unsafe for multi-thread applications. strtok_r is MT-Safe and should be used instead.

Typical Operation Main programs creates (or spawns) threads Threads may –perform one task and die –last for duration of program Threads must –be able to synchronize activity –communicate with one another

Programming Support for Threads Creation –pthread_create(top-level procedure, args) Termination –return to top-level procedure –explicit cancel Rendezvous –creator can wait for children pthread_join(child_tid) Synchronization –pthread_mutex_... –condition variables (pthread_cond_...)

Programming with Threads Synchronization –For coordination of the threads Communication –For inter-thread sharing of data –Threads can be in different processors –How to achieve sharing in SMP? Software: accomplished by keeping all threads in the same address space by the OS Hardware: accomplished by hardware shared memory and coherent caches

Synchronization Primitives lock and unlock –mutual exclusion among threads –busy-waiting vs. blocking –pthread_mutex_trylock: no blocking (rare) –pthread_mutex_lock: blocking –pthread_mutex_unlock condition variables –pthread_cond_wait: block for a signal –pthread_cond_signal: signal one waiting thread –pthread_cond_broadcast: signal all waiting threads

Example lock(mutex); while (resource_state == BUSY) //spin; resource_state = BUSY; unlock(mutex); use resource; lock(mutex); resource_state = FREE; unlock(mutex); Initially mutex is unlocked resource_state is FREE Will this work? 1 - Yes 2 - No 3 - Maybe

Example lock(mutex); while (resource_state == BUSY) //spin; resource_state = BUSY; unlock(mutex); use resource; lock(mutex); resource_state = FREE; unlock(mutex); Thread 1

Example lock(mutex); while (resource_state == BUSY) //spin; resource_state = BUSY; unlock(mutex); use resource; lock(mutex); resource_state = FREE; unlock(mutex); Thread 1 Thread 2

Example lock(mutex); while (resource_state == BUSY) //spin; resource_state = BUSY; unlock(mutex); use resource; lock(mutex); resource_state = FREE; unlock(mutex); Thread 1 Thread 2

Example with cond-var lock(mutex) while(resource_state == BUSY) wait(cond_var); /* implicitly give up mutex */ /* implicitly re-acquire mutex */ resource_state = BUSY unlock(mutex) /* use resource */ lock(mutex) resource_state = FREE; unlock(mutex) signal(cond_var);

Example with cond-var lock(mutex) while(resource_state == BUSY) wait(cond_var); /* implicitly give up mutex */ /* implicitly re-acquire mutex */ resource_state = BUSY unlock(mutex) /* use resource */ lock(mutex) resource_state = FREE; unlock(mutex) signal(cond_var); T1

Example with cond-var lock(mutex) while(resource_state == BUSY) wait(cond_var); /* implicitly give up mutex */ /* implicitly re-acquire mutex */ resource_state = BUSY unlock(mutex) /* use resource */ lock(mutex) resource_state = FREE; unlock(mutex) signal(cond_var); T1 T2

pthreads Mutex –Must create mutex variables pthread_mutex_t padlock; –Must initialize mutex variable pthread_mutex_init(&padlock, NULL); Condition Variable (used for signaling) –Must create condition variables pthread_cond_t non_full; –Must initialize condition variables pthread_cond_init(&non_full, NULL);

Classic CS Problem Producer Consumer Producer If (! full) Add item to buffer empty = FALSE if(buffer_is_full) full = TRUE Consumer If (! empty) Remove item from buffer full = FALSE if(buffer_is_empty) empty = TRUE full empty buffer...

Example Producer Threads Program while(forever){ // produce item pthread_mutex_lock(padlock); while (full) pthread_cond_wait(non_full, padlock); // add item to buffer; buffercount++; if (buffercount == BUFFERSIZE) full = TRUE; empty = FALSE; pthread_mutex_unlock(padlock); pthread_cond_signal(non_empty); }

Example Consumer Threads Program while(forever) { pthread_mutex_lock(padlock); while (empty) pthread_cond_wait (non_empty, padlock); // remove item from buffer; buffercount--; full = false; if (buffercount == 0) empty = true; pthread_mutex_unlock(padlock); pthread_cond_signal(non_full); // consume_item; }

// Producer while(forever){ // produce item pthread_mutex_lock(padlock); while (full) pthread_cond_wait(non_full, padlock); // add item to buffer; buffercount++; if (buffercount == BUFFERSIZE) full = TRUE; empty = FALSE; pthread_mutex_unlock(padlock); pthread_cond_signal(non_empty); } // Consumer while(forever) { pthread_mutex_lock(padlock); while (empty) pthread_cond_wait (non_empty, padlock); // remove item from buffer; buffercount--; full = false; if (buffercount == 0) empty = true; pthread_mutex_unlock(padlock); pthread_cond_signal(non_full); // consume_item; }

Using Threads Servers dispatcher model Mailbox dispatcher Workers

Team model Pipelined model Mailbox

Threads Implementation User level threads –OS independent –Scheduler is part of the runtime system –Thread switch is cheap (save PC, SP, regs) –Scheduling customizable, i.e., more application control –Blocking call by thread blocks process

Solution to blocking problem in user level threads –Non-blocking version of all system calls Switching among user level threads –Yield voluntarily –How to make preemptive? Timer interrupt from kernel to switch

Kernel Level –Expensive thread switch –Makes sense for blocking calls by threads –Kernel becomes complicated: process vs. threads scheduling –Thread packages become non-portable Problems common to user and kernel level threads –Libraries –Solution is to have thread-safe wrappers to such library calls

Questions?

Solaris Threads Three kinds –user, lwp, kernel User: Any number can be created and attached to lwp’s One to one mapping between lwp and kernel threads Kernel threads known to the OS scheduler If a kernel thread blocks, associated lwp, and user level threads block as well

Solaris Terminology

More Conventional Terminology Thread kernel thread (user-level view) (Inside the kernel) Processes P1P2P3

Questions? Does kernel see user threads? Are all threads from P3 on same CPU? Are all threads in kernel attached to lwp’s? If the left-most thread of P3 issues a blocking system call does P3 block? Who schedules lwp’s? Who schedules threads?

Kernel Threads vs. User Threads Advantages of kernel threads –Can be scheduled on multiple CPU’s –Can be preempted by CPU –Kernel scheduler knows their relative priorities Advantages of user threads –(Unknown to kernel) –Extremely lightweight: No system call to needed to change threads.

Questions?

Things to know? 1. The reason threads are around? 2. Benefits of increased concurrency? 3. Why do we need software controlled "locks" (mutexes) of shared data? 4. How can we avoid potential deadlocks/race conditions. 5. What is meant by producer/consumer thread synchronization/communication using pthreads? 6. Why use a "while" loop around a pthread_cond_wait() call?

Things to know? 7. Why should we minimize lock scope (minimize the extent of code within a lock/unlock block)? 8. Do you have any control over thread scheduling?

Questions?