Lecture 4: Concurrency and Threads CS 170 T Yang, 2015 Chapter 4 of AD textbook.

Slides:



Advertisements
Similar presentations
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process An operating system executes a variety of programs: Batch system.
Advertisements

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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 6: Threads Chapter 4.
Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)
Day 10 Threads. Threads and Processes  Process is seen as two entities Unit of resource allocation (process or task) Unit of dispatch or scheduling (thread.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 TY, Sept 2011.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
Based on Silberschatz, Galvin and Gagne  2009 Threads Definition and motivation Multithreading Models Threading Issues Examples.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Process Concept An operating system executes a variety of programs
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.
Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place  (address space + resources) –Thread is a program.
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.
國立台灣大學 資訊工程學系 Chapter 4: Threads. 資工系網媒所 NEWS 實驗室 Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the.
Multithreading Allows application to split itself into multiple “threads” of execution (“threads of execution”). OS support for creating threads, terminating.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 T Yang, Sept 2012.
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.
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
B. RAMAMURTHY 10/24/ Realizing Concurrency using the thread model.
CS333 Intro to Operating Systems Jonathan Walpole.
Chapter 4: Threads. 2 Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux 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.
Department of Computer Science and Software Engineering
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
1 OS Review Processes and Threads Chi Zhang
Threads A thread is an alternative model of program execution
Saurav Karmakar. Chapter 4: Threads  Overview  Multithreading Models  Thread Libraries  Threading Issues  Operating System Examples  Windows XP.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Thread S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore.
Concurrency I: Threads Nov 9, 2000 Topics Thread concept Posix threads (Pthreads) interface Linux Pthreads implementation Concurrent execution Sharing.
CISC2200 Threads Fall 09. Process  We learn the concept of process  A program in execution  A process owns some resources  A process executes a program.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Lecture 5. Example for periority The average waiting time : = 41/5= 8.2.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Multithreaded Programming.
Chapter 4: Threads Modified by Dr. Neerja Mhaskar for CS 3SH3.
Day 12 Threads.
CS399 New Beginnings Jonathan Walpole.
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Chapter 4: Threads.
Chapter 5: Threads Overview Multithreading Models Threading Issues
Chapter 5: Threads Overview Multithreading Models Threading Issues
Chapter 4: Multithreaded Programming
Chapter 4 Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 5: Threads Overview Multithreading Models Threading Issues
Concurrent Servers Topics Limitations of iterative servers
Operating Systems Lecture 13.
Chapter 4: Threads.
Modified by H. Schulzrinne 02/15/10 Chapter 4: Threads.
CHAPTER 4:THreads Bashair Al-harthi OPERATING SYSTEM
Threads Chapter 5 2/17/2019 B.Ramamurthy.
Jonathan Walpole Computer Science Portland State University
Threads Chapter 5 2/23/2019 B.Ramamurthy.
Still Chapter 2 (Based on Silberchatz’s text and Nachos Roadmap.)
Chapter 4: Threads & Concurrency
Chapter 5: Threads Overview Multithreading Models Threading Issues
Shared Memory Programming with Pthreads
Presentation transcript:

Lecture 4: Concurrency and Threads CS 170 T Yang, 2015 Chapter 4 of AD textbook

Chapter 4: Threads Overview  thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems Thread Libraries  Pthreads  Nachos threads

Logical View of Threads Threads associated with a process P1 sh foo T1 Process hierarchy A process T2 T4 T5 T3 shared code, data and kernel context

Benefits of multi-threading Responsiveness Resource Sharing  Shared memory Economy Scalability  Explore multi-core CPUs

Motivation for multi-threaded servers

Thread Abstraction Infinite number of processors Threads execute with variable speed  Programs must be designed to work with any schedule

Concurrent Thread Execution Two threads run concurrently (are concurrent) if their logical flows overlap in time Otherwise, they are sequential (we’ll see that processes have a similar rule) Examples:  Concurrent: A & B, A&C  Sequential: B & C Time Thread AThread BThread C

Execution Flow Concurrent execution on a single core system Parallel execution on a multi-core system

Programmer vs. Processor View

Difference between Single and Multithreaded Processes Shared memory access for code/data Separate control flow -> separate stack/registers

Threads vs. Processes How threads and processes are similar  Each has its own logical control flow  Each can run concurrently  Each is context switched How threads and processes are different  Threads share code and data, processes (typically) do not  Threads are somewhat cheaper than processes with less overhead

Thread Libraries Thread library provides programmer with API for creating and managing threads  Pthreads  Java threads  OS-specific threads –Nachos for class proj.

Pthreads provided either as user-level or kernel-level A POSIX standard (IEEE c) API for thread creation and synchronization Common in UNIX OS (Linux, Mac OS X).  In CSIL, compile a c program with gcc -lpthread

Posix Threads (Pthreads) Interface Creating and reaping threads  pthread_create, pthread_join Determining your thread ID  pthread_self Terminating threads  pthread_cancel, pthread_exit  exit [terminates all threads], return [terminates current thread] Synchronizing access to shared variables  pthread_mutex_init, pthread_mutex_[un]lock  pthread_cond_init, pthread_cond_[timed]wait

Example of Pthreads #include void *PrintHello(void * id){ printf(“Thread%d: Hello World!\n", id); } void main (){ pthread_t thread0, thread1; pthread_create(&thread0, NULL, PrintHello, (void *) 0); pthread_create(&thread1, NULL, PrintHello, (void *) 1); }

Example of Pthreads with join #include void *PrintHello(void * id){ printf(“Thread%d: Hello World!\n", id); } void main (){ pthread_t thread0, thread1; pthread_create(&thread0, NULL, PrintHello, (void *) 0); pthread_create(&thread1, NULL, PrintHello, (void *) 1); pthread_join(thread0, NULL); pthread_join(thread1, NULL); }

Execution of Threaded “hello, world” main thread peer thread main thread waits for peer thread to terminate exit() terminates main thread and any peer threads call Pthread_create() call Pthread_join() Pthread_join() returns printf() (peer thread terminates)

Java Threads Java threads are managed by the JVM Java threads may be created by extending Thread class  Thread class –run, start methods –yield, join –sleep Synchronization  synchronized methods & objects  wait/notify/notifyAll  conditions

Java Threads: Example class MyThread extends Thread { private String name; public MyThread(String name) { this.name = name; } public void run() { for (;;) { System.out.println(name + ": hello world"); } public class Main2 { public static void main(String [] args) { MyThread t1 = new MyThread("thread1"); MyThread t2 = new MyThread("thread2"); t1.start(); t2.start(); }

Java Threads: example outpout thread2: hello world thread1: hello world thread2: hello world thread1: hello world thread2: hello world thread1: hello world thread2: hello world

Nachos Threads class Thread{ public: Thread (char *name); ~Thread(); void Fork( void (*Func)( int), int arg); void Yield(); void Finish(); }

Nachos Threads for 170 OS Project C++ methods supported: Thread(char *Name). Create a thread. Fork(VoidFunctionPtr func, int arg). A thread starts to execute a function. Yield(). Suspend the calling thread and the system selects a new one for execution. Sleep(). Suspend the current thread, change its state to BLOCKED, and remove it from the ready list Finish()

main () { Thread *t1 =new Thread("thread1"); Thread *t2= new Thread("thread2"); t1->Fork(SimpleFunc, 1); t2->Fork(SimpleFunc, 2); SimpleFunc(3); } SimpleFunc(int i) { printf(“Hello %d\n”, i); currentThread->Yield(); } Example of Nacho Threads Start to fork and execute a function in each child thread. Function executed by threads Create 2 new thread data structure Parent executes the same function

Thread Lifecycle

Implementing threads Thread_fork(func, args)  Allocate thread control block  Allocate stack  Build stack frame for base of stack (stub)  Put func, args on stack  Put thread on ready list  Will run sometime later (maybe right away!) stub(func, args):  Call (*func)(args)  Call thread_exit() Thread switching during scheduling  Save registers on old stack  Switch to new stack, new thread  Restore registers from new stack

Shared vs. Per-Thread State

Types of Threads: Kernel vs user-level Kernel Threads Recognized and supported by the OS Kernel OS explicitly performs scheduling and context switching of kernel threads Linux, MacOS, Windows

User-level Threads Thread management done by user-level threads library  OS kernel does not know/recognize there are multiple threads running in a user program.  The user program (library) is responsible for scheduling and context switching of its threads. Examples:  Java threads

Summary: Process vs Thread Processes have two parts  Threads (Concurrency)  Address Spaces (Protection) Concurrency accomplished by multiplexing CPU Time:  Unloading current thread (PC, registers)  Loading new thread (PC, registers)  Such context switching may be voluntary ( yield(), I/O operations) or involuntary (timer, other interrupts) Protection accomplished restricting access:  Memory mapping isolates processes from each other  Dual-mode for isolating I/O, other resources Various Textbooks talk about processes  When this concerns concurrency, really talking about thread portion of a process  When this concerns protection, talking about address space portion of a process John Kubiatowicz (