b. ramamurthy 10/24/2015 1 realizing concurrency using the thread model

23
B. RAMAMURTHY 03/27/22 1 Realizing Concurrency using the thread model

Upload: moris-powell

Post on 03-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: B. RAMAMURTHY 10/24/2015 1 Realizing Concurrency using the thread model

B. RAMAMURTHY

04/20/23

1

Realizing Concurrency using the thread model

Page 2: B. RAMAMURTHY 10/24/2015 1 Realizing Concurrency using the thread model

Models of concurrency

There are two prevalent models of concurrency in most systems

The process model (heavy weight: provides complete isolation; separate address space/process)

The thread model (light weight: many operate in the same address space)

We will discuss the thread model as defined by Posix thread now.

We will discuss the process model later.

04/20/23

2

Page 3: B. RAMAMURTHY 10/24/2015 1 Realizing Concurrency using the thread model

Introduction

04/20/23

3

A thread refers to a thread of control flow: an independent sequence of execution of program code.

Threads are powerful. As with most powerful tools, if they are not used appropriately thread programming may be inefficient.

Thread programming has become viable solution for many problems with the advent of multi-core processors

Typically these problems are expected to handle many requests simultaneously. Example: multi-media, games, automotive embedded systems

Especially relevant to embedded system with the proliferation of multi-core processors

Page 4: B. RAMAMURTHY 10/24/2015 1 Realizing Concurrency using the thread model

Topics to be Covered

04/20/23

4

ObjectivesWhat are Threads?Thread implementation modelsPOSIX threadsCreating threadsUsing threadsSummary

Page 5: B. RAMAMURTHY 10/24/2015 1 Realizing Concurrency using the thread model

Objectives

04/20/23

5

To understand the thread model for realizing concurrency

To study POSIX standard for threads called the Pthreads.

To study thread control primitives for creation, termination, join, synchronization, concurrency, and scheduling.

To learn to design multi-threaded applications.

Page 6: B. RAMAMURTHY 10/24/2015 1 Realizing Concurrency using the thread model

Per process vs per thread items

04/20/23

6

Items shared by all threads in a processItems private to each thread

Page 7: B. RAMAMURTHY 10/24/2015 1 Realizing Concurrency using the thread model

Thread as a unit of work

04/20/23

7

A thread is a unit of work to a CPU. It is strand of control flow.

A traditional UNIX process has a single thread that has sole possession of the process’s memory and resources.

Threads within a process are scheduled and execute independently.

Many threads may share the same address space.Each thread has its own private attributes: stack,

program counter and register context.

Page 8: B. RAMAMURTHY 10/24/2015 1 Realizing Concurrency using the thread model

Pthread Library

04/20/23

8

Many thread models emerged: Solaris threads, win-32 threads

A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization.

API specifies behavior of the thread library, implementation is up to development of the library.

Simply a collection of C functions.

Page 9: B. RAMAMURTHY 10/24/2015 1 Realizing Concurrency using the thread model

Posix Library Implementationin F. Mueller’s Paper

04/20/23

9

Language Application

Language Interface C Language Application

Posix thread library

Unix Kernel

Unix librariesUser Level

Kernel Level

Page 10: B. RAMAMURTHY 10/24/2015 1 Realizing Concurrency using the thread model

Creating threads

04/20/23

10

Always include pthread library: #include <pthread.h> int pthread_create (pthread_t *tp, const pthread_attr_t * attr,

void *(* start_routine)(void *), void *arg); This creates a new thread of control that calls the function

start_routine. It returns a zero if the creation is successful, and thread id in

tp (first parameter). attr is to modify the attributes of the new thread. If it is

NULL default attributes are used. The arg is passing arguments to the thread function.

Page 11: B. RAMAMURTHY 10/24/2015 1 Realizing Concurrency using the thread model

Using threads

04/20/23

11

1. Declare a variable of type pthread_t2. Define a function to be executed by the thread.3. Create the thread using pthread_createMake sure creation is successful by checking the

return value.4. Pass any arguments need through’ arg (packing

and unpacking arg list necessary.)5. #include <pthread.h> at the top of your header.6. Compile: g++ -o executable file.cc -lpthread

Page 12: B. RAMAMURTHY 10/24/2015 1 Realizing Concurrency using the thread model

Thread’s local data

04/20/23

12

Variables declared within a thread (function) are called local data.

Local (automatic) data associated with a thread are allocated on the stack. So these may be deallocated when a thread returns.

So don’t plan on using locally declared variables for returning arguments. Plan to pass the arguments thru argument list passed from the caller or initiator of the thread.

Page 13: B. RAMAMURTHY 10/24/2015 1 Realizing Concurrency using the thread model

Thread termination (destruction)

04/20/23

13

Implicit : Simply returning from the function executed by the thread terminates the thread. In this case thread’s completion status is set to the return value.

Explicit : Use thread_exit. Prototype: void thread_exit(void *status);The single pointer value in status is available

to the threads waiting for this thread.

Page 14: B. RAMAMURTHY 10/24/2015 1 Realizing Concurrency using the thread model

Waiting for thread exit

04/20/23

14

int pthread_join (pthread_t tid, void * *statusp);

A call to this function makes a thread wait for another thread whose thread id is specified by tid in the above prototype.

When the thread specified by tid exits its completion status is stored and returned in statusp.

Page 15: B. RAMAMURTHY 10/24/2015 1 Realizing Concurrency using the thread model

The Thread Model

04/20/23

15

(a) Three processes each with one thread(b) One process with three threads

Page 16: B. RAMAMURTHY 10/24/2015 1 Realizing Concurrency using the thread model

Implementing Threads in User Space

04/20/23

16

A user-level threads package

Page 17: B. RAMAMURTHY 10/24/2015 1 Realizing Concurrency using the thread model

Implementing Threads in the Kernel

04/20/23

17

A threads package managed by the kernel

Page 18: B. RAMAMURTHY 10/24/2015 1 Realizing Concurrency using the thread model

Hybrid Implementations

04/20/23

18

Multiplexing user-level threads onto kernel- level threads

Page 19: B. RAMAMURTHY 10/24/2015 1 Realizing Concurrency using the thread model

Scheduler Activations

04/20/23

19

Goal – mimic functionality of kernel threads gain performance of user space threads

Avoids unnecessary user/kernel transitionsKernel assigns virtual processors to each process

lets runtime system allocate threads to processorsProblem:

Fundamental reliance on kernel (lower layer) calling procedures in user space (higher layer)

Page 20: B. RAMAMURTHY 10/24/2015 1 Realizing Concurrency using the thread model

Pop-Up Threads

04/20/23

20

Creation of a new thread when message arrives(a) before message arrives(b) after message arrives

Thread pools

Page 21: B. RAMAMURTHY 10/24/2015 1 Realizing Concurrency using the thread model

Thread Scheduling (1)

04/20/23

21

Possible scheduling of user-level threads 50-msec process quantum threads run 5 msec/CPU burst

B1, B2, B3

Page 22: B. RAMAMURTHY 10/24/2015 1 Realizing Concurrency using the thread model

Thread Scheduling (2)

04/20/23

22

Possible scheduling of kernel-level threads 50-msec process quantum threads run 5 msec/CPU burst

B1, B2, B3

Page 23: B. RAMAMURTHY 10/24/2015 1 Realizing Concurrency using the thread model

Summary

04/20/23

23

We looked at Implementation of threads. thread-based concurrency. Pthread programming

We will look at a pthread programming demoSee https://computing.llnl.gov/tutorials/pthreads/