Presentation is loading. Please wait.

Presentation is loading. Please wait.

5. Multithreading UNIT 5 Multithreading 1.

Similar presentations


Presentation on theme: "5. Multithreading UNIT 5 Multithreading 1."— Presentation transcript:

1 5. Multithreading UNIT 5 Multithreading 1

2 Points to be covered.. Thread Pthread API for thread management
Thread scheduling and priorities Thread contention scope 2

3 What is a process and a thread?
A process is an instance of a running (or suspended) program. It has its own address space/memory. A thread is a sequence or stream of executable code or stream of instructions within a process. The operating system schedules it for execution on a processor or core. A process may have one or more threads i.e. one or more flows of controls. It is a light weight process with less overhead than a process. Each thread of a program / process executes independently or concurrently. A process with multiple threads is multithreaded process. The threads of same process are called peer threads. 3

4 Properties of threads Threads execute independent concurrent tasks of a program. Multiple threads of a process can be created, managed, maintained easily as compared to a single process. Threads use minimal resources shared in the address space of a single process as compared to an application. Threads can improve the throughput and performance of the application if used correctly. Each thread can be assigned a priority showing importance of the subtask it is executing. 4

5 User level v/s Kernel level Threads
There are three implementation models for threads: User - or application - level threads Kernel - level threads Hybrid of user - and kernel - level threads 5

6 1) User level threads Jhxakxajkx 6

7 1) User level threads In user mode , a process or thread is executing instructions in the program or linked library. They are not making any calls to the operating system kernel. In kernel mode , the process or thread is making system calls such as accessing resources or throwing exceptions. Also, in kernel mode, the process or thread can access objects that are defined in kernel space. User - level threads reside in user space or mode. The library scheduler chooses a thread from the multiple threads of a process, and that thread is associated with the one kernel thread allowed for that process. That kernel thread will be assigned to a processor core by the operating system scheduler. User -level threads are considered a “ many - to - one ” thread mapping. 7

8 8

9 1) Kernel level threads Kernel - level threads reside in kernel space and are kernel objects With kernel threads, each user thread is mapped to or bound to a kernel thread till it gets terminated. This is called a “ one - to - one ” thread mapping. The operating system scheduler manages, schedules, and dispatches these threads. The runtime library requests a kernel - level thread for each of the user – level threads. 9

10 10

11 Hybrid level A hybrid thread implementation is a cross between user and kernel threads and allows both the library and the operating system to manage the threads. User threads are managed by the runtime library scheduler, and the kernel threads are managed by the operating system scheduler. With this implementation, a process has its own pool of kernel threads. The user threads that are runnable are dispatched by the runtime library and are marked as available threads ready for execution. The operating system selects a user thread and maps it to one of the available kernel threads in the pool. More than one user thread may be assigned to the same kernel thread. 11

12 Thread Context Context of process includes:
Process id Pointer to executable the stack Memory for variables Process registers Threads also have a context. Threads of same process share the same address space Threads have their own unique, local information like: Thread id, Time when it is created Process registers (e.g. program counter, stack pointer) State and priority of thread Thread -specific data like private copies of a process’s global data. 12

13 Thread Context A thread ’ s locally declared variables should not be accessed by any of its peer threads. They are placed in the stack of the thread, and when the thread has completed, they are removed from the stack. The TSD is a structure that contains data and information private to a thread. TSD can contain private copies of a process ’ s global data. It can also contain signal masks for a thread. Signal masks are used to identify signals of a specific type that will not be received by the thread when sent to its process. Otherwise, if a process is sent a signal by the operating system, all threads in its address space also receive that signal. The thread receives all signal types that are not masked. 13

14 Thread Context A thread shares text and stack segment with its process. Its instruction pointer points to some location within the process ’ s text segment to the next executable thread instruction, and the stack pointer points to the location in the process stack where the top of the thread ’ s stack begins. Threads can also access any environment variables. All of the resources of the process, such as file descriptors, are shared with its threads.

15 Process and thread context
15

16 Hardware and Software threads
Threads can be implemented in H/w as well as in S/W. Cores with multiple hardware threads are called “Simultaneous Multithreaded (SMT)Cores”. SMT – enabled processors execute many software threads or processes concurrently. Due to that, efficiency increases due to less wait time for I/O. Examples: Sun’s UltraSparc T1, IBM’s Cell Broadband Engine CBE, and various Intel multicore processors. They utilize SMT or chip - level multithreading (CMT), having 2 to 8 threads per core. 16

17 Thread Resources Threads share most of their resources like processors, memory, and file descriptors with other threads of the same process. Threads of the same process compete these descriptors. All the resources of peer threads in combination must not exceed the resource limit of the process. A thread should not leave the resources unstable. It should perform some cleanup. 17

18 Some issues: Comparing threads to processes
Context switching: With low processor availability or a single core, concurrently executing processes involve heavy overhead because of the context switching required. But with threads, context switch occurs, when the thread of different process is the next thread to be assigned to processor. 18

19 Increases with multiple threads.
2) Throughput: Increases with multiple threads. Process with one thread can halt entire process, if the thread waits for I/O request. Process with multiple threads does not halt the entire process, as one of the threads waits for I/O request, other threads work. 19

20 Threads don’t require a special mechanism for communication.
Can directly pass and receive data from other peer threads. Can communicate by using memory which is shared within the address space of process. Processes can communicate by shared memory which exists outside the address spaces of the processes. Message queue used to communicate between them. 20

21

22 4) Corrupted Process Data:
Synchronization of threads is needed, otherwise when threads can corrupt the data easily when they share the same address. Access to same piece of data can cause data race. e.g. out of threads A, B, C – threads A and B update a counter and thread C is to read the updates in the counter and use it for further calculation. Thread A and B attempt to write the counter concurrently. But, processes have their own address spaces, which protect the data from corruption. 5) Reuse by other programs: Threads are dependent , and can not be separated from their processes. Processes are more independent. An application is divided into various processes and those can be packaged into modules to reuse. Threads can’t exist outside process, so can’t be reused. 22

23 6) Killing the Entire process:
If a thread tries to access any resource which is not allowed or access is violated, then it may result in entire process termination because it occurs in address space of a process. A process can have an access violation and it causes the process to terminate, but other processes can continue execution.

24 Both have attributes that describe the entity to the OS.
Similarities between Threads and Processes Both have an id, set of registers, state, priority, and scheduling policy. Both have attributes that describe the entity to the OS. Both have an information block. Both share resources with the parent process. Both function as independent entities from the parent process. The creator can exercise some control over the thread or process. Both can change their attributes. Both can create new resources. Neither can access the resources of another process directly. 24

25 Differences between threads and processes
Threads share the address space of the creator; processes have their own address space. Threads have direct access to the data segment of their process; processes have their own copy of the data segment of the parent process. Threads can directly communicate with other threads of their process; processes must use inter-process Communication to communicate with sibling processes. Threads have almost no overhead; processes have considerable overhead. New threads are easily created; new processes require duplication of the parent process. Threads can monitor control over threads of the same process; processes can monitor control only over child processes. Changes to the main thread (cancellation, priority change, and so on) may affect the behaviour of the other threads of the process; changes to the parent process do not affect child processes. 25

26 Thread Attributes Threads have their own attributes.
Those attributes gives the unique identity to each thread. The attributes can reconstruct thread’s environment. Following attributes are there: Contention scope Stack size Stack address Detached state Priority Scheduling policy and parameters POSIX thread library defines thread attribute object to encapsulate the subset of the properties. 26

27 Thread Attribute Object:
It encapsulates the subset of the properties of a thread. It is associated with one or multiple threads. It is a profile that gives behavior of one or group of threads. It should be created and initialized. Once created and initialized, we can reference it repeatedly in threads creation function call. When a thread is created using this object, most attributes can’t be changed while thread is in use. 27

28 Threads compete with other threads within the same process.
Contention Scope: “Scope” describes that which thread out of many, compete with for resources. Two contention scopes: Process scope System scope Process scope: Threads compete with other threads for processor usage. Threads compete with other threads within the same process. System scope: Threads compete for resources with the threads of other processes allocated across the system. Thread with system scope is prioritized with respect to all other system wide threads. 28

29 Each thread’s stack size and location is set when it is created.
If not specified, then default stack size and location is assigned by system which is system dependent. Thread’s stack size must be large enough for any function calls, for any external code like library code, local variable storage, etc. A process should have stack segment large enough for all thread’s stack. The location of a thread does not overlap other peer thread’s stacks. 29

30 These threads are detached from their creator.
3) Detached state: These threads are detached from their creator. They are not synchronized with other peer threads or the primary thread. When a thread terminates, the id and the status of the terminated thread are saved by the system, the creator is notified. These resources are immediately available for reuse by the system. 30

31 4) Scheduling policies and parameters:
The threads inherit scheduling attributes from the process. The thread with the highest priority is executed before threads with lower priority. Executing threads are preempted if a thread of higher priority is available. FIFO, round robin (RR), and other scheduling policies are available. 31

32 Architecture of a thread
32

33 Threads states: Runnable Running Stopped Sleeping(blocked)
There are several transitions: Preempt Signaled Dispatch Timer runout The state of the primary thread is that same as the state of the process, if it ’ s the only thread. If the primary thread is sleeping, the process is sleeping. If the primary thread is running, the process is running. For a process that has multiple threads, all threads of the process have to be in a sleeping or stopped state in order for the whole process to be considered sleeping or stopped. On the other hand, if one thread is active (runnable or running), then the process is considered active. 33

34 Scheduling and thread contention scope
There are two types of contention scopes for threads: Process contention : Threads with process contention scope contend with threads of the same process. These are hybrid threads (user - and kernel - level threads), whereby the system creates a pool of kernel - level threads, and user – level threads are mapped to them. These kernel - level threads are unboundand can be mapped to one thread or mapped to many threads. The kernel then schedules the kernel threads onto processors according to their scheduling attributes. System contention: Threads with system contention scope contend with threads of processes systemwide. This model consists of one user - level thread per kernel - level thread. The user thread is bound to a kernel - level thread throughout the lifetime of the thread. The kernel threads are solely responsible for scheduling thread execution on one or more processors. This model schedules all threads against all other threads in the system, using the scheduling attributes of the thread. 34

35 Scheduling and thread contention scope
35

36 Scheduling Policy and Priority
The scheduling policy and priority of the process belong to the primary thread. Each thread can have its own scheduling policy and priority separate from the primary thread. The priority value is an integer that has a maximum and minimum value. Preemptive and non-preemptive schedule of the thread and priority. Threads with same contention scope are scheduled as per priorities. Threads with process level contention scope never compete with threads with system level contention scope. 1) A round - robin scheduling policy: It considers all threads to be of equal priority, and each thread is given the processor for only a time slice. 2) FIFO scheduling: A thread with high priority assigned to a processor dominates the processor until it completes execution. This scheduling policy can be used for applications where a set of threads needs to complete as soon as possible. 3) The “ other ” scheduling policy can be a customization of a scheduling policy. 36

37 POSIX threads(Portable Operating System Interface)
It is a standard for Unix-like operating systems—for example, Linux, Mac OS X, Solaris, HPUX. It specifies an application programming interface (API) for multithreaded programming. like MPI, Pthreads specifies a library that can be linked with C programs. 37

38 A simple threaded program
using namspace std; #include < iostream > #include < pthread.h > void *task1(void *X) //define task to be executed by ThreadA { cout < < “Thread A complete” < < endl; return (NULL); } void *task2(void *X) //define task to be executed by ThreadB cout < < “Thread B complete” < < endl; 38

39 int main(int argc, char *argv[ ]) {
pthread_t ThreadA, ThreadB; // declare threads pthread_create( & ThreadA, NULL, task1, NULL); // create threads pthread_create( & ThreadB, NULL, task2, NULL); // additional processing pthread_join(ThreadA, NULL); // wait for threads pthread_join(ThreadB, NULL); return (0); } 39

40 How to compile and execute ?
g++ -o a.out test_thread.cpp –lpthread Or g++ test_thread.cpp –lpthread Run: ./a.out 2 Output: Thread A complete Thread B complete 40

41 const pthread_attr_t* attr_p, void* (*start_routine)(void*),
1) int pthread_create( pthread_t* thread_p, const pthread_attr_t* attr_p, void* (*start_routine)(void*), void* arg_p); First argument is pointer to pthread_t object i.e. each thread. Second arg. we are not using so NULL. Third arg. is a function that the thread is to run. Last arg. is pointer to the argument that is passed to function the thread is running. 2) void * thread_function(void * arg_p); void*can point to any pointer type in C. arg_p can point to a list containing one or more values. Similarly, return type of function can point to a list containing one or more values. 41

42 42

43 the “master” thread terminating a thread Is called joining
starting a thread Is called forking 43

44 int pthread_join (pthread_t thread, void** ret_val_p);
The function fails if the target thread is not a joinable thread or detached. If target thread returns successfully, its exit status stored in ret_val_p. Each thread executes return statement and eventually, main thread will call pthread_join(). 44

45 Passing arguments to main()
45

46 int main( int argc, char* argv[ ])
46

47 Simple program 47

48 48

49 output For example, to run the program with one thread, we type
$ ./pth hello 1 and the output will look something like this: Hello from the main thread Hello from thread 0 of 1 To run the program with four threads, we type $ ./pth hello 4 Hello from thread 0 of 4 Hello from thread 1 of 4 Hello from thread 2 of 4 Hello from thread 3 of 4 49

50 #include < pthread.h > pthread_t pthread_self(void);
Getting thread ids: Syntax: #include < pthread.h > pthread_t pthread_self(void); When a thread is created, the thread id is returned to the calling thread e.g. pthread_t ThreadId; ThreadId = pthread_self(); 50

51 #include < pthread.h >
comparing thread ids: Syntax: #include < pthread.h > int pthread_equal(pthread_t tid1, pthread_t tid2); Function returns a nonzero value, if two thread ids reference the same thread, Else it returns zero value. 51

52 Using pthread attribute object
“pthread_attr_t” object can be used to set following thread attributes: Size of the thread ’ s stack Location of the thread ’ s stack Scheduling inheritance, policy, and parameters Whether the thread is detached or joinable Scope of the thread 52

53 53

54 First function initializes a thread attribute object with default values for all attributes.
Second function deletes any hidden storage associated with the thread attribute object. 54

55 Creating detached threads
When thread exits, system stores thread’s completion status and id , when it joins other thread. But, when an exiting thread is not joined with other thread, the exiting thread is called a detached thread. “detachstate” parameter can have two values: PTHREAD_CRTEATE_DETACHED PTHREAD_CRTEATE_JOINABLE 55

56 ThreadA is created as a detached thread using an attribute object
ThreadA is created as a detached thread using an attribute object. ThreadB is detached after it has been created. 56

57 Thread terminates either
Managing threads Managing operation consists of : How to control any thread behaviour How they compete for resources How to set and manage scheduling policy and priority Terminating thread: Thread terminates either Comes at the end of instruction in the routine Terminated by another thread prematurely Effect of thread termination: pthread library reclaims system resources. Stores it’s exit status 57

58 Thread Termination Three types of thread termination: Self termination
Terminating peer threads Cancelled by another process. 58

59 Self termination: #include < pthread.h >
Synrax: #include < pthread.h > int pthread_exit(void *value_ptr); //self termination When a joinable thread function has completed executing, it returns to the thread calling pthread_join() for which it was the target thread. When the terminating thread calls pthread_exit() , it is passed the exit status in value_ptr . The exit status is returned to pthread_join() . Cancellation cleanup handler tasks that have not executed execute along with the destructors for any thread - specific data. 59

60 Terminating peer threads:
#include < pthread.h > int pthread_cancel(pthread_t thread); // “thread” parameter is the thread to be cancelled Called by performance monitoring thread for poorly performing thread. A call to pthread_cancel() is a request to cancel a peer thread. The request can be granted immediately, granted at a later time, or even ignored. The target thread may terminate immediately or defer termination until a logical point in its execution. The thread may have to perform some cleanup tasks before it terminates. The thread also has the option to refuse termination. 60

61 Cancelled by another process:
- It is a request to cancel a thread. #include < pthread.h > int pthread_setcancelstate(int state, int *oldstate); int pthread_setcanceltype(int type, int *oldtype); The values for state and oldstate for setting the cancel state of a thread are: PTHREAD_CANCEL_DISABLE PTHREAD_CANCEL_ENABLE PTHREAD_CANCEL_DISABLE causes the thread to ignore a cancel request. PTHREAD_CANCEL_ENABLE causes the thread to concede to a cancel request. This is the default state of any newly created thread. If successful, the function returns If not successful, the function returns an error number. The pthread_setcancelstate() may fail if not passed a valid state value. 61

62 pthread_setcanceltype() sets the calling thread to the
cancelability type specified by type and returns the previous state in oldtype . The values for type and oldtype are: PTHREAD_CANCEL_DEFFERED PTHREAD_CANCEL_ASYNCHRONOUS causes the thread to put off termination until it reaches its cancellation point. This is the default cancelability type for any newly created threads. PTHREAD_CANCEL_ASYNCHRONOUS causes the thread to t erminate immediately. If successful, the function returns 0

63 Managing the Thread ’ s Stack
Setting the Size of the Stack #include < pthread.h > int pthread_attr_getstacksize(const pthread_attr_t *restrict attr, size_t *restrict stacksize); int pthread_attr_setstacksize(pthread_attr_t *attr, size_t *stacksize); Setting the Location of the Thread ’ s Stack int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr); int pthread_attr_getstackaddr(const pthread_attr_t *restrict attr, void **restrict stackaddr); Setting Stack Size and Location with One Function int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize); int pthread_attr_getstack(const pthread_attr_t *restrict attr, void **restrict stackaddr, Setting Thread Scheduling and Priorities Setting Thread Scheduling and Priorities

64 Setting Thread Scheduling and Priorities
#include < pthread.h > #include < sched.h > int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched); void pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy); int pthread_attr_setschedparam(pthread_attr_t *restrict attr, const struct sched_param *restrict param); pthread_attr_setinheritsched()is used to determine how the thread ’ s scheduling attributes are set, by inheriting the scheduling attributes either from the creator thread or from an attribute object. inheritschedcan have one of these values: PTHREAD_INHERIT_SCHED: Thread scheduling attributes are inherited from the creator thread, and any scheduling attributes of the attr are ignored. PTHREAD_EXPLICIT_SCHED:Thread scheduling attributes are set to the scheduling attributes of the attribute object attr.

65 Setting Thread Scheduling and Priorities
If inheritschedvalue is PTHREAD_EXPLICIT_SCHED, then pthread_attr_setschedpolicy() is used to set the scheduling policy and pthread_attr_setschedparam()is used to set the priority. The pthread_attr_setschedpolicy()sets the scheduling policy of the thread attribute object attr . policyvalues can be one of the following defined in the < sched.h > header: SCHED_FIFO: First - In, First - Out scheduling policy whereby the executing thread runs to completion. SCHED_RR: Round robin scheduling policy whereby each thread is assigned to processor only for a time slice. SCHED_OTHER: Another scheduling policy (implementation - defined). By default, this is the scheduling policy of any newly created thread.

66 #include < sched.h >
int sched_get_priority_max(int policy); int sched_get_priority_min(int policy); Use pthread_attr_setschedparam()to set the scheduling parameters of the attribute object attr used by the scheduling policy. param is a structure that contains the parameters. The sched_param structure has at least this data member defined: struct sched_param { int sched_priority; //... };

67

68 Thank you!! 68


Download ppt "5. Multithreading UNIT 5 Multithreading 1."

Similar presentations


Ads by Google