Presentation is loading. Please wait.

Presentation is loading. Please wait.

Thread Programming.

Similar presentations


Presentation on theme: "Thread Programming."— Presentation transcript:

1 Thread Programming

2 Topics Thread Thread NPTL thread library commands Thread Safety
Thread libraries Threading issues Linux Threads

3 4 Reasons for threads Express logically concurrent tasks
Run work in the background Exploit multiple processors Manage I/O devices

4

5 Process (fork) vs Thread creation
Fork() is relatively expensive to launch Difficult to share information between processes Faster creation (10x or better.. Sharing information is easy.. Just copy information into a shared heap. -> That requires synchronization between threads

6 Thread Advantages Sharing data between threads is easier
Thread Creation is faster Used extensively in Operating Systems

7 Figure 2-7. A word processor with three threads.
Thread Usage (1) Figure 2-7. A word processor with three threads. Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

8 Figure 2-8. A multithreaded Web server.
Thread Usage (2) Figure 2-8. A multithreaded Web server. Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

9 Thread Usage (3) Figure 2-9. A rough outline of the code for Fig (a) Dispatcher thread. (b) Worker thread. Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

10 Thread Disadvantages First let’s look at threads in memory space

11 Stack and Stack Frames square (int x) STACK doCalc(int val)
{ return x*x } doCalc(int val) { printf (“square is %d\n”, square(val) } main (int x, int y) { key = 9999 doCalc (key) } STACK Frames for C run-time start up functions Frame for doCalc Frame for square Stack for main

12

13 4 Threads executing in process in linux
argv, environ Stack for main thread Stack for thread 3 Stack for thread 2 Stack for thread 1 Shared libraries, shared memory Code is re-entrant Heap Uninitialized Data (BSS) Initialized Data Thread 3 executing here Text (program code) Main thread executing here Thread 1 executing here Thread 2 executing here

14 Threads share Process ID and parent process ID Controlling terminal
Credentials Open files and locks Resource limits CPU times consumed.

15 Attributes distinct for Threads
Thread ID Thread-specific data Real-time scheduling policy CPU affinity Stack (local variables and function call linkage) Processor registers in switc

16 Thread resource Shared State Per-Thread State Per-Thread State
Thread Control Block Heap Thread Control Block Stack Information Stack Information Saved Registers Saved Registers Global Variables Thread Metadata Thread Metadata Code Thread Stack Thread Stack

17 The Classical Thread Model
Figure The first column lists some items shared by all threads in a process. The second one lists some items private to each thread. Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

18 The Classical Thread Model (1)
Figure (a) Three processes each with one thread. (b) One process with three threads. Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

19 The Classical Thread Model (3)
Figure Each thread has its own stack. Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

20 Figure 2-14. Some of the Pthreads function calls.
POSIX Threads (1) Figure Some of the Pthreads function calls. Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

21 Implementing Threads in User Space or Kernel Space
(b) Figure (a) A user-level threads package. (b) A threads package managed by the kernel. Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

22 Pop-Up Threads Figure Creation of a new thread when a message arrives. (a) Before the message arrives. (b) After the message arrives. Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

23 Thread Lifecycle

24 Thread Safety A function is said to be thread-safe if it can be safely invoked by multiple threads at the same time. Conversely, if a function is not thread-safe, then we can’t call it from one thread while it is being executed in another thread.

25 Two threads call yield

26 Thread Disadvantages Must ensure thread safety
Bug in one thread can exhibit itself in ALL threads Threads are competing for finite virtual address space.

27 Compiling Thread Programs
On Linux compile with the -pthread option. The effects include _REENTRANT preprocessor macro is defined. Links with libpthread lib

28 Creating Threads Thread points to buffer for unique thread ID
#include <pthread.h> int pthread_create( pthread_t *thread. const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)); Thread points to buffer for unique thread ID If attr is NULL, the default attributes are used. Upon its creation, the thread executes start_routine, with arg as its sole argument.

29 Thread ID #include <pthread.h> pthread_t pthread_self(void)); Each thread within a process is uniquely identified by the Thread Id This is returned on pthread_create()

30 Thread Termination #include <pthread.h> void pthread_exit(void *retval)); The thread’s start function returns with return value Thread calls pthread_exit() Thread is cancelled by external program Main thread performs a return (causes all threads to terminate immediately)

31 Thread Cancellation #include <pthread.h> void pthread_cancel(pthread_t thread)); Threads can protect themselves from cancellation by setting its flag. When cancellation is enabled, it is done only when the thread reaches its next cancellation point.

32 Joining with a Terminated Thread
#include <pthread.h> int pthread_join(pthread_ t thread, void **retval); Returns 0 on success or a positive error number on error Waits for the thread to terminate.

33 Detaching a Thread When we don’t care to wait for a thread to finish.
#include <pthread.h> int pthread_detach (pthread_ t thread); When we don’t care to wait for a thread to finish.

34 Create Threads example

35 To see thread information ps -m

36 ps m –o pid,tid,command


Download ppt "Thread Programming."

Similar presentations


Ads by Google