Operating System Chapter 4 Multithreaded programming


Published on


Published in: Education, Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Operating System Chapter 4 Multithreaded programming

  1. 1. MULTI-THREADED PROGRAMMING Reported by: N ikko D elumen M ary J oy T apar
  2. 2. 4.1 OVERVIEW Thread – basic unit of CPU utilization; it comprises a thread ID, a program counter, a register set, and a stack.
  3. 3. <ul><li>4.1.1 MOTIVATION </li></ul><ul><li>Many software packages that run on modern desktop PCs are multithreaded. </li></ul><ul><li>Example: A word processor may have a thread for displaying graphics, another for responding to keystrokes, and a third is for performing spelling and grammar checking in background. </li></ul>
  4. 4. thread thread Single-threaded process Multi-threaded process
  5. 5. <ul><li>Recall from CHAPTER3 </li></ul><ul><li>“ RPCs allow interprocess communication by providing a communication mechanism similar to ordinary function or procedure calls.” </li></ul><ul><li>Typically, RPC servers are multithreaded. When server receives a message, it services the message using a separate thread. </li></ul>
  6. 6. <ul><li>4.1.2 BENEFITS </li></ul><ul><li>Responsiveness. Multithreading an interactive application may allow a program to continue running even if part of it is blocked or is performing a lengthy operation, thereby increasing responsiveness to the user. </li></ul>
  7. 7. <ul><li>Resource Sharing. By default, threads share the memory and the resources of the process to which they belong. </li></ul><ul><li>Economy. Allocating memory and resources for process creation is costly. Because threads share resources of the process to which they belong, it is more economical to create and context-switch threads. </li></ul>
  8. 8. <ul><li>Utilization of multiprocessor architectures. Threads may be running in parallel on different processors. A single-threaded process can only run on one CPU, no matter how many are available. </li></ul>
  9. 9. <ul><li>4.2 MULTI-THREADING MODELS </li></ul><ul><li>Support for threads may be provided either at the user level, for user threads, or by the kernel, for kernel threads. </li></ul>
  10. 10. 4.2.1 Many-to-One Model User thread Kernel thread
  11. 11. 4.2.2 One-to-One Model User thread Kernel thread
  12. 12. Two-level Model User thread Kernel thread
  13. 13. 4.2.3 Many-to-Many Model User thread Kernel thread
  14. 14. 4.3 THREAD LIBRARIES A Thread Library provides the programmer an API for creating and managing threads.
  15. 15. <ul><li>Two Primary Ways of Implementing a Thread Library </li></ul><ul><li>To provide a library entirely in user with no kernel support. All code and data structures for the library exist in user space. </li></ul><ul><li>To implement a kernel-level library supported directly by the operating system. Code and data structures for the library exist in kernel space. </li></ul>
  16. 16. 4.3.1 PTHREADS Pthreads refers to the POSIX standard (IEEE 1003.1c) defining an API for thread creation and synchronization.
  17. 17. #include <pthread.h> #include <stdio.h> int sum; void *runner(void *param); int main(int argc, char *argv[]) { pthread.t tid; pthread_attr_t attr; if (argc !=2){ fprintf(stderr,”usage: a.out(integer value> ”); return -1; } if (atoi (argv[1]) < 0) { fprintf(stderr,”%d must be >= 0 ”, atoi (argv[1])); return -1; } pthread_attr_init(&attr); pthread_create (&tid, &attr, runner, argv[1]); pthread_join (tid,NULL); printf(“sum=%d ”, sum); } void *runner(void *param) { int i, upper = atoi(param) sum = 0; for (i=1; i<=upper; i++) sum += I; pthread_exit (0); }
  18. 18. 4.3.2 WIN32 THREADS The technique of creating threads using Win32 thread library is similar to the Pthreads technique in several ways.
  19. 19. #include <windows.h> #include <stdio.h> DWORD Sum; DWORD WINAPI Summation(LPVOID Param) { DWORD UPPER = *(DWORD*) Param; for (DWORD I = 0; I <= Upper; i++) Sum += I; return 0; } int main (int argc, char *argv[]) { DWORD ThreadId; HANDLE ThreadHandle; int Param; if (argc !=2){ fprintf(stderr,”An integer parameter is required ”); return – 1; } Param=atoi(argv[1]); if (Param<0){ fprintf(stderr,”An integer >= 0 is required ); return – 1; }
  20. 20. ThreadHandle=CreateThread( NULL, 0, Summation, &Param, 0, &ThreadId); if (ThreadHandle != NULL){ WaitForSingleObject(ThreadHandle, INFINITE); CloseHandle(ThreadHandle); printf(“sum = %d ”,Sum); } }
  21. 21. 4.3.3 JAVA THREADS Threads are fundamental model of program execution in a Java program, and the Java language and it’s API provide a rich set of features for the creation and management of threads.
  22. 22. class Sum { private int sum; public int getSum() { return sum; } public void setSum (int sum) { this.sum = sum; } } class Summation implements Runnable { private int upper; private Sum sumValue; public Summation (int upper, Sum sumValue) { this.upper = upper; this.sumValue = sumValue } public void run() { int sum = 0; for (int i=0; i<= upper; i++) sum+=I; sumValue.setSum(sum); } }
  23. 23. public class Driver { public static void main(String[]args){ if(args.length >0){ if (Integer.parseInt (args[0])<0) System.err.printIn(args[0]+” must be >= 0.”); else{ Sum sumObject = new Sum(); int upper = Integer.parseInt(args[0]); Thread thrd = new Thread(new Summation (upper, sumObject)); thrd.start(); try { thrd.join(); System.out.printIn (“The sum of “+upper+” is “+sumObject.getSum()); } catch (InterruptedException ie) { } } } else System.err.println(“Usage: Summation <integer value>”); } }
  24. 24. 4.4.3 THREADING ISSUES In this section, we discuss some of the issues to consider with multithreaded programs.
  25. 25. <ul><li>4.4.2 CANCELLATION </li></ul><ul><li>Thread cancellation – is the task of terminating a thread before it has completed. </li></ul><ul><li>Example: if multiple threads are concurrently searching through a database and one thread returns the result, the remaining threads might be canceled . </li></ul>
  26. 26. <ul><li>Cancellation points – Pthreads refers to such as cancellation points. </li></ul><ul><li>4.4.3 SIGNAL HANDLING </li></ul><ul><li>Signal – used in UNIX systems to notify </li></ul><ul><li>a process that a particular event has occurred. </li></ul>
  27. 27. <ul><li>All signals, whether synchronous or asynchronous , follow the same pattern: </li></ul><ul><li>A signal is generated by the occurrence of a particular event. </li></ul><ul><li>A generated signal is delivered to a process. </li></ul><ul><li>One delivered, the signal must be handled. </li></ul>
  28. 28. <ul><li>Signal Asynchronously – when a signal is generated by an event external to a running process. </li></ul><ul><li>Example: </li></ul><ul><li>include terminating process with specific </li></ul><ul><li>Keystrokes ( such as <control> <C> ) and have a timer expire. </li></ul><ul><li>Every signal may be handled by one of two possible handlers: </li></ul><ul><li>1. A default signal handler </li></ul><ul><li>2. A user-defined signal handler </li></ul>
  29. 29. <ul><li>Default Signal Handler – run by the kernel </li></ul><ul><li>when handling that signal. </li></ul><ul><li>User-defined signal handler – that is called to handle the signal. </li></ul><ul><li>Asynchronous procedure calls (APCs) – the Windows can be emulated using this. </li></ul>
  30. 30. 4.4.4 THREAD POOLS Limits the number of threads that exist at anyone point. This is particularly important on systems that cannot support a large number of concurrent threads. Example: DWORD WINAPI PoolFunction (AVOID Param) { /** * this function runs as a separate thread. **/ }
  31. 31. <ul><li>QueueUserWorkItem ( ) function , which is passed three parameters: </li></ul><ul><li>LPTHREAD_START_ROUTINE Function – a pointer to the function that is to run as a separate thread. </li></ul><ul><li>PVOID Param – the parameters passed to Function. </li></ul><ul><li>ULONG Flags – flags indicating how the thread pool is to create and manage execution of the thread. </li></ul>
  32. 32. 4.4.5 THREAD-SPECIFIC DATA In a transaction-processing system, we might service each transaction in a separate thread. User thread Lightweight process Kernel thread Figure 4.9 Lightweight process (LWP) LWP k
  33. 33. <ul><li>4.4.6 SCHEDULAR ACTIVATIONS </li></ul><ul><li>One scheme for communication between the user-thread library and the kernel is known as schedular activation . </li></ul><ul><li>upcall – the kernel must inform an application about certain events. </li></ul><ul><li>upcall handler – upcalls are handled by the thread libarary and it must run on a virtual processor. </li></ul>
  34. 34. <ul><li>4.5 OPERATING SYSTEM EXAMPLES </li></ul><ul><li>4.5.1 WINDOW XP THREADS </li></ul><ul><li>it implements the WIN32 API. The WIN32 API is the primary API for the family of Microsoft O.S. </li></ul><ul><li>The general components of a thread include: </li></ul><ul><li>A thread ID unquely identifying the thread </li></ul><ul><li>A register set representing the status of the processor. </li></ul>
  35. 35. <ul><li>A user stack, employed when the thread is running in user mode, and a kernel stack, employed when the thread is running in kernel mode </li></ul><ul><li>A private storage area used by various run-time libraries and dynamic link libraries (DLLs) </li></ul>
  36. 36. <ul><li>The register set, stacks, and private storage area are known as the context of a thread include: </li></ul><ul><li>ETHREAD – executive thread block </li></ul><ul><li>KTHREAD – kernel thread block </li></ul><ul><li>TEB – thread environment block </li></ul>
  37. 37. <ul><li>4.5.2 Linux Threads </li></ul><ul><li>Lynux provides the fork ( ) system call </li></ul><ul><li>it also provides the ability to create threads using the clone( ) system call </li></ul><ul><li>uses the distinguish between processes and threads. Uses the term task – rather process or thread. </li></ul>
  38. 38. Some of these flags are listed below: The set of open file is shared. CLONE_FILES Signal handlers are shared. CLONE_SIGHAND The same memory space is shared. CLONE_VM File-system information is shared. CLONE_FS MEANING FLAG
  39. 39. <ul><li>SUMMARY </li></ul><ul><li>THREAD – is a flow of control within a process. </li></ul><ul><li>MULTITHREADED PROCESS – contains several different flows of control within the same address space. It includes increased responsiveness to the user, resource sharing within process, economy, and the ability to take advantage of multiprocessor architecture. </li></ul>
  40. 40. <ul><li>USER-LEVEL THREADS – are threads that are visible to the programmer and are unknown to the kernel. </li></ul><ul><li>THREE DIFFERENT TYPES OF MODELS RELATE USER AND KERNEL THREADS: </li></ul><ul><li>1. many to one model maps </li></ul><ul><li>2. one-to-one model maps </li></ul><ul><li>3. many-to-many model maps </li></ul>
  41. 41. THE END…