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 Thread Disadvantages First let’s look at threads in memory space

8 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

9

10 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

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

12 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

13 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

14 Thread Lifecycle

15 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.

16 Two threads call yield

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

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

19 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.

20 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()

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

22 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.

23 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.

24 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.

25 Create Threads example

26 To see thread information ps -m

27 ps m –o pid,tid,command


Download ppt "Thread Programming."

Similar presentations


Ads by Google