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

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

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

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

shsh sh

foo

T1

Process hierarchyA process

T2T4

T5 T3

shared code, dataand 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 A Thread B Thread 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 ProcessesShared memory access for code/dataSeparate 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 threadsPthreads Java threadsOS-specific threads

–Nachos for class proj.

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

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 <pthread.h>

#include <stdio.h>

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 <pthread.h>

#include <stdio.h>

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 threadterminates)

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: Exampleclass 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

thread2: hello world

thread2: hello world

thread2: hello world

thread2: hello world

thread2: hello world

thread2: hello world

thread2: hello world

thread1: hello world

thread2: hello world

thread1: hello world

thread2: hello world

thread2: hello world

thread1: hello world

thread2: 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 processJohn Kubiatowicz (http://cs162.eecs.Berkeley.edu)