Download presentation
Presentation is loading. Please wait.
Published byTodd Warren Modified over 9 years ago
1
Lecture 4: Concurrency and Threads CS 170 T Yang, 2015 Chapter 4 of AD textbook
2
Chapter 4: Threads Overview thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems Thread Libraries Pthreads Nachos threads
3
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
4
Benefits of multi-threading Responsiveness Resource Sharing Shared memory Economy Scalability Explore multi-core CPUs
5
Motivation for multi-threaded servers
6
Thread Abstraction Infinite number of processors Threads execute with variable speed Programs must be designed to work with any schedule
7
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
8
Execution Flow Concurrent execution on a single core system Parallel execution on a multi-core system
9
Programmer vs. Processor View
10
Difference between Single and Multithreaded Processes Shared memory access for code/data Separate control flow -> separate stack/registers
11
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
12
Thread Libraries Thread library provides programmer with API for creating and managing threads Pthreads Java threads OS-specific threads –Nachos for class proj.
13
Pthreads provided either as user-level or kernel-level A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization Common in UNIX OS (Linux, Mac OS X). In CSIL, compile a c program with gcc -lpthread
14
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
15
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); }
16
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); }
17
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)
18
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
19
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(); }
20
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
21
Nachos Threads class Thread{ public: Thread (char *name); ~Thread(); void Fork( void (*Func)( int), int arg); void Yield(); void Finish(); }
22
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()
23
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
24
Thread Lifecycle
25
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
26
Shared vs. Per-Thread State
27
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
28
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
29
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 (http://cs162.eecs.Berkeley.edu)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.