Thread Programming Topics Thread Thread NPTL thread library
Thread Programming
Topics • Thread – Thread NPTL thread library commands – Thread Safety – Thread libraries – Threading issues – Linux Threads
4 Reasons for threads • Express logically concurrent tasks • Run work in the background • Exploit multiple processors • Manage I/O devices
Process (fork) vs Thread creation Process • Fork() is relatively expensive to launch • Difficult to share information between processes • Faster creation (10 x or better. . • Sharing information is easy. . Just copy information into a shared heap. -> That requires synchronization between threads
Thread Advantages • Sharing data between threads is easier • Thread Creation is faster • Used extensively in Operating Systems
Thread Usage (1) Figure 2 -7. A word processor with three threads. Tanenbaum & Bo, Modern Operating Systems: 4 th ed. , (c) 2013 Prentice-Hall, Inc. All rights reserved.
Thread Usage (2) Figure 2 -8. A multithreaded Web server. Tanenbaum & Bo, Modern Operating Systems: 4 th ed. , (c) 2013 Prentice-Hall, Inc. All rights reserved.
Thread Usage (3) Figure 2 -9. A rough outline of the code for Fig. 2 -8. (a) Dispatcher thread. (b) Worker thread. Tanenbaum & Bo, Modern Operating Systems: 4 th ed. , (c) 2013 Prentice-Hall, Inc. All rights reserved.
Thread Disadvantages First let’s look at threads in memory space
Stack and Stack Frames square (int x) { return x*x } STACK Frames for C run-time start up functions do. Calc(int val) { printf (“square is %dn”, square(val) } main (int x, int y) { key = 9999 do. Calc (key) } Frame for do. Calc Frame for square Stack for main
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 Code is re-entrant Shared libraries, shared memory Heap Uninitialized Data (BSS) Initialized Data Thread 3 executing here Main thread executing here Thread 1 executing here Thread 2 executing here Text (program code)
Threads share • • • Process ID and parent process ID Controlling terminal Credentials Open files and locks Resource limits CPU times consumed.
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
Thread resource Shared State Per-Thread State Heap Thread Control Block Stack Information Global Variables Code Saved Registers Thread Metadata Thread Stack Per-Thread State Thread Control Block Stack Information Saved Registers Thread Metadata Thread Stack
The Classical Thread Model Figure 2 -12. 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: 4 th ed. , (c) 2013 Prentice-Hall, Inc. All rights reserved.
The Classical Thread Model (1) Figure 2 -11. (a) Three processes each with one thread. (b) One process with three threads. Tanenbaum & Bo, Modern Operating Systems: 4 th ed. , (c) 2013 Prentice-Hall, Inc. All rights reserved.
The Classical Thread Model (3) Figure 2 -13. Each thread has its own stack. Tanenbaum & Bo, Modern Operating Systems: 4 th ed. , (c) 2013 Prentice-Hall, Inc. All rights reserved.
POSIX Threads (1) Figure 2 -14. Some of the Pthreads function calls. Tanenbaum & Bo, Modern Operating Systems: 4 th ed. , (c) 2013 Prentice-Hall, Inc. All rights reserved.
Implementing Threads in User Space or Kernel Space (a) (b) Figure 2 -16. (a) A user-level threads package. (b) A threads package managed by the kernel. Tanenbaum & Bo, Modern Operating Systems: 4 th ed. , (c) 2013 Prentice-Hall, Inc. All rights reserved.
Pop-Up Threads Figure 2 -18. Creation of a new thread when a message arrives. (a) Before the message arrives. (b) After the message arrives. Tanenbaum & Bo, Modern Operating Systems: 4 th ed. , (c) 2013 Prentice-Hall, Inc. All rights reserved.
Thread Lifecycle
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.
Two threads call yield
Thread Disadvantages • Must ensure thread safety • Bug in one thread can exhibit itself in ALL threads • Threads are competing for finite virtual address space.
Compiling Thread Programs On Linux compile with the -pthread option. The effects include • _REENTRANT preprocessor macro is defined. • Links with libpthread lib
Creating Threads #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.
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()
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)
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.
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.
Detaching a Thread #include <pthread. h> int pthread_detach (pthread_ t thread); • When we don’t care to wait for a thread to finish.
Create Threads example • https: //computing. llnl. gov/tutorials/pthreads/ • http: //www. tutorialspoint. com/cplus/cpp _multithreading. htm
To see thread information ps -m
ps m –o pid, tid, command
- Slides: 36