1 / 24

Threads

Threads. Overview Multithreading Models Threading Issues User threads vs. Kernel threads Pthreads. Single and Multithreaded Processes. Benefits.

Download Presentation

Threads

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Threads • Overview • Multithreading Models • Threading Issues • User threads vs. Kernel threads • Pthreads Operating System Concepts

  2. Single and Multithreaded Processes Operating System Concepts

  3. Benefits • Responsiveness: multi threading an interactive application may allow a programmer to continue even if part of it is blocked or is performing a lengthy operation. • Resource sharing: • Economy : It is more economical to create and context switch threads. • Utilization of Multi Processor Architectures : Each thread may be running parallel on a different processor. Operating System Concepts

  4. User Threads • Thread management done by user-level threads library • Examples : - POSIX Pthreads, Mach C-threads, Solaris threads • Advantages: fast to create and destroy • Disadvantages: If kernel is single threaded then any user level thread performing a blocking system call will cause the entire process to block even if other threads are available to run within the application Operating System Concepts

  5. Kernel Threads • The thread creation, scheduling and management is done by the Kernel • Slower to create and manage. • If a thread performs a blocking system call, the kernel can schedule another thread in the application for execution. • Examples - Windows 95/98/NT/2000 - Solaris - Tru64 UNIX - BeOS - Linux Operating System Concepts

  6. Multithreading Models • Many-to-One • One-to-One • Many-to-Many Operating System Concepts

  7. Many-to-One • Many user-level threads mapped to single kernel thread. • Thread management is done in user space. • Used on systems that do not support kernel threads. • Since only one thread can access kernel at a time, multiple threads are unable to run in parallel on multiprocessors. • Green threads a thread library available for solaris 2 Operating System Concepts

  8. Many-to-One Model Operating System Concepts

  9. One-to-One • Each user-level thread maps to kernel thread. • More concurrency. • Every user thread requires creating a corresponding kernel thread. • Examples - Windows 95/98/NT/2000 - Linux Operating System Concepts

  10. One-to-one Model Operating System Concepts

  11. Many-to-Many Model • Allows many user level threads to be mapped to many kernel threads. • The number of kernel threads may be specific to a particular application or particular machine. (More kernel threads on a multiprocessor m/c) • Allows the operating system to create a sufficient number of kernel threads. • Solaris 2 , IRIX, HP-UX Operating System Concepts

  12. Many-to-Many Model Operating System Concepts

  13. Threading Issues • Semantics of fork( )system call: if one thread calls fork( ), does the new process duplicate all threads or it is single threaded. Some unix version have two version of fork, one that duplicates all threads and another that duplicates only the thread that invoked the fork system call. • Exec system call: The program specified in the parameter to exec will replace the entire process including all threads and LWPs. • Thread cancellation : terminating a thread before it is completed. Ex. Many threads searching a database in parallel and search of one thread is successful. • 1) Asynchronous cancellation :One thread immediately terminates target thread. • 2) Deferred cancellation (target thread check periodically if it should terminate) Operating System Concepts

  14. Signal Handling • Signal is used in UNIX systems to notify a process that a particular event has occurred. A signal is generated by the occurrence of an event, delivered to a process where it must be handled either by a default signal handler (run by the kernel) or a user defined signal handler. • In a multi threaded program where a signal should be delivered? (i) Deliver the signal to the thread to which the signal applies. (ii) Deliver signal to every thread. (iii) deliver signal to certain threads (iv) Assign a specific thread to receive all signals (Solaris2) Although Windows 200 does not support signals, they can be emulated using asynchronous procedure calls. Operating System Concepts

  15. Thread pools • Consider the example of a multithreaded web server creating one thread for each client request: (a) time required to create the thread prior to servicing request. (b) thread discarded after servicing the request (c) No limit on the maximum number of threads. Thread pool: The idea is to create a number of threads at process start up and place them into a pool, where they sit and wait for work. Thread pool solves above mentioned three problems. Number of threads in the pool can be set heuristically based upon factors such as the number of CPUs in the system, the amount of physical memory and the expected number of concurrent client requests. Operating System Concepts

  16. Pthreads • a POSIX standard (IEEE 1003.1c) API for thread creation and synchronization. • API specifies behavior of the thread library, implementation is up to development of the library. • Common in UNIX operating systems. Operating System Concepts

  17. Pthreads example #include </usr/include/pthread.h> #include <stdio.h> int sum; /* this data is shared by the thread(s) */ void *runner(void *param); /* the thread */ main(int argc, char *argv[]) { pthread_t tid; /* the thread identifier */ pthread_attr_t attr; /* set of thread attributes */ if (argc != 2) { fprintf(stderr, "usage: a.out <integer value>\n"); return -1; } if (atoi (argv [1]) < 0) { fprintf(stderr,"%d must be >= O\n",atoi(argv[1])); return -1; } /* get the default attributes */ pthread_attr_init (&attr); /* create the thread */ pthread_create(&tid,&attr,runner,argv[1]) ; /* wait for the thread to exit */ pthread_join(tid,NULL) ; printf (" sum = %d\n", sum) ; } Operating System Concepts

  18. Pthreads example … /* The thread will begin control in this function */ void *runner(void *param) { int i, upper = atoi(param); sum = 0; for (i = 1; i <= upper; i++) sum += i; pthread_exit ( 0); } Operating System Concepts

  19. Solaris 2 Threads Operating System Concepts

  20. Solaris Process Operating System Concepts

  21. Windows 2000 Threads • Implements the one-to-one mapping. • Each thread contains - a thread id - register set - separate user and kernel stacks - private data storage area Operating System Concepts

  22. Linux Threads • Linux refers to them as tasks rather than threads. • Thread creation is done through clone() system call. • Clone() allows a child task to share the address space of the parent task (process) Operating System Concepts

  23. Java Threads • Java threads may be created by: • Extending Thread class • Implementing the Runnable interface • Java threads are managed by the JVM. Operating System Concepts

  24. Java Thread States Operating System Concepts

More Related