Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating Systems Theory - Threads1 Operating Systems Theory Threads IFE - Computer Science Alexi Akl.

Similar presentations

Presentation on theme: "Operating Systems Theory - Threads1 Operating Systems Theory Threads IFE - Computer Science Alexi Akl."— Presentation transcript:

1 Operating Systems Theory - Threads1 Operating Systems Theory Threads IFE - Computer Science Alexi Akl

2 Operating Systems Theory - Threads2 Contents Threads and Processes Single And MultiThreaded Processes MultiThreading Models Threading Issues PThreads Windows XP Threads Linux Threads Java Threads

3 Operating Systems Theory - Threads3 Threads And Processes Thread In computer science is short for a thread of execution. Threads are a way for a program to split itself into two or more simultaneously (or pseudo- simultaneously) running tasks. Threads and processes differ from one operating system to another, but in general, the way that a thread is created and shares its resources is different from the way a process does.

4 Operating Systems Theory - Threads4 Threads And Processes A process is the "heaviest" unit of kernel scheduling. Processes own resources allocated by the operating system. Resources include memory, file handles, sockets, device handles, and windows. A thread is the "lightest" unit of kernel scheduling. At least one thread exists within each process. If multiple threads can exist within a process, then they share the same memory and file resources.

5 Operating Systems Theory - Threads5 Single And MultiThreaded Processes Multiple threads can be executed in parallel on many computer systems. This multithreading generally occurs by time slicing, wherein a single processor switches between different threads, in which case the processing is not literally simultaneous, for the single processor is only really doing one thing at a time. Standard PC nowadays has only one processor, if it is equipped with more than one processor, then different threads and processes can rum literally simultaneously on different processors.

6 Operating Systems Theory - Threads6 Single And MultiThreaded Processes

7 Operating Systems Theory - Threads7 MutiThreaded Processes Benefits Responsiveness Resource Sharing Economy Utilization of MultiThreaded Processes architecture

8 Operating Systems Theory - Threads8 User-level and Kernel Threads Three primary User-level thread libraries: POSIX Pthreads Win32 threads Java threads Examples of supported kernel threads: Windows XP Solaris Linux Tru64 UNIX Mac OS X

9 Operating Systems Theory - Threads9 MutiThreading Models - Many to one 1. Many user level threads mapped to single kernel thread Examples: 1. Solaris Green Threads 2. GNU Portable Threads

10 Operating Systems Theory - Threads10 MutiThreading Models - One to one 1. Each user-level thread maps to kernel thread Examples: 1. Windows NT/XP/ Linux 3. Solaris 9 and later

11 Operating Systems Theory - Threads11 MutiThreading Models – Many to many 1. Allows many user level threads to be mapped to many kernel threads 2. Allows the operating system to create a sufficient number of kernel threads Examples: 1. Solaris prior to version 9 2. Windows NT/2000 with the ThreadFiber package

12 Operating Systems Theory - Threads12 MultiThreading Models - Two Level Model Similar to Many to Many, except that it allows a user thread to be bound to kernel thread Examples IRIX HP-UX Tru64 UNIX Solaris 8 and earlier

13 Operating Systems Theory - Threads13 Threading Issues – Thread Cancellation Thread cancellation: Terminating a thread before it has finished, has two approaches: Asynchronous cancellation: terminates the target thread immediately Deferred cancellation: allows the target thread to periodically check if it should be cancelled

14 Operating Systems Theory - Threads14 Threading Issues – Signal Handling Signals are used in UNIX systems to notify a process that a particular event has occurred A signal handler is used to process signals Signal is generated by particular event Signal is delivered to a process Signal is handled Options: Deliver the signal to the thread to which the signal applies Deliver the signal to every thread in the process Deliver the signal to certain threads in the process Assign a specific thread to receive all signals for the process

15 Operating Systems Theory - Threads15 Threading Issues - Thread Pool Create a number of threads in a pool where they await work Advantages: Usually slightly faster to service a request with an existing thread than create a new thread Allows the number of threads in the application(s) to be bound to the size of the pool

16 Operating Systems Theory - Threads16 Threading Issues – Thread specific data Allows each thread to have its own copy of data Useful when you do not have control over the thread creation process (i.e., when using a thread pool)

17 Operating Systems Theory - Threads17 Threading Issues – Scheduler Activations Both M:M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application Scheduler activations provide upcalls-a communication mechanism from the kernel to the thread library This communication allows an application to maintain the correct number kernel threads

18 Operating Systems Theory - Threads18 PThreads Historically, hardware vendors have implemented their own proprietary versions of threads. These implementations differed substantially from each other making it difficult for programmers to develop portable threaded applications. In order to take full advantage of the capabilities provided by threads, a standardized programming interface was required. For UNIX systems, this interface has been specified by the IEEE POSIX c standard (1995). Implementations which adhere to this standard are referred to as POSIX threads, or Pthreads. Pthreads are defined as a set of C language programming types and procedure calls, implemented with a pthread.h header/include file and a thread library. Common in Unix

19 Operating Systems Theory - Threads19 Windows XP Threads Implements the one-to-one mapping Each thread contains A thread id Register set Separate user and kernel stacks Private data storage area The register set, stacks, and private storage area are known as the context of the threads The primary data structures of a thread include: ETHREAD (executive thread block) KTHREAD (kernel thread block) TEB (thread environment block)

20 Operating Systems Theory - Threads20 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)

21 Operating Systems Theory - Threads21 Linux Threads Thread operations include thread creation, termination, synchronization (joins,blocking), scheduling, data management and process interaction. A thread does not maintain a list of created threads, nor does it know the thread that created it. All threads within a process share the same address space. Threads in the same process share: Process instructions Most data open files (descriptors) signals and signal handlers current working directory User and group id Each thread has a unique: Thread ID set of registers, stack pointer stack for local variables, return addresses signal mask priority Return value: errno pthread functions return "0" if OK.

22 Operating Systems Theory - Threads22 Linux Threads #include void *print_message_function( void *ptr ) { char *message; message = (char *) ptr; printf("%s \n", message); } main() { pthread_t thread1, thread2; char *message1 = "Thread 1"; char *message2 = "Thread 2"; int iret1, iret2; iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1); iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2); pthread_join( thread1, NULL); pthread_join( thread2, NULL); printf("Thread 1 returns: %d\n",iret1); printf("Thread 2 returns: %d\n",iret2); exit(0); }

23 Operating Systems Theory - Threads23 Linux Threads Output: Thread 1 Thread 2 Thread 1 returns: 0 Thread 2 returns: 0 Threads terminate by explicitly calling pthread_exit, by letting the function return, or by a call to the function exit which will terminate the process including any threads.

24 Operating Systems Theory - Threads24 Java Threads Java threads are managed by the JVM Java threads may be created by: Extending Thread class Implementing the Runnable interface

25 Operating Systems Theory - Threads25 Java Threads Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon. When a Java Virtual Machine starts up, there is usually a single non- daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs: The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place. All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method. There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread. An instance of the subclass can then be allocated and started.

26 Operating Systems Theory - Threads26 Questions? Thank you for your attention Any questions?

Download ppt "Operating Systems Theory - Threads1 Operating Systems Theory Threads IFE - Computer Science Alexi Akl."

Similar presentations

Ads by Google