Presentation is loading. Please wait.

Presentation is loading. Please wait.

Threads G.Anuradha (Reference : William Stallings)

Similar presentations


Presentation on theme: "Threads G.Anuradha (Reference : William Stallings)"— Presentation transcript:

1 Threads G.Anuradha (Reference : William Stallings)

2 Characteristics of a Process Resource Ownership: Process image is in a virtual address space and time to time it has been allocated resources. OS provides protection to the process (Process or Task) Scheduling/execution: Process has an execution state and priority and OS does the scheduling and execution (Thread or Light Weight Process)

3 Multithreading Ability of and OS to support multiple threads of execution within a single process Single thread of execution per process is called as single-threaded approach Process in a multithreaded environment is the unit of resource allocation and protection – Virtual address space that holds the process image – Protected access to processor(s),resources

4 Threads and Processes MS-DOS UNIX Java Windows 2000 Solaris Linux

5 Thread Within a process there may be thread(s) – Has an execution state – Saved thread context when not running – An execution stack – Some per-thread static storage for local variables – Access to memory and resources of its process shared with all other threads in that process

6 Single threaded and multithreaded process models Share the state and resources of the process and reside in the same address space and access the same data

7 Benefits of threads 1.Takes Less time to create a thread than a process 2.Takes less time to terminate a thread than a process 3.Takes less time to switch between threads within the same process 4.Enhance efficiency in communication between different executing programs.

8 Examples of users of threads in single –user multiprocessing system Foreground and background work Asynchronous processing Speed execution Modular program structure Note:- 1. Suspension of a process suspends all the threads in that process as well 2. Termination of a process terminates all the threads in that process as well

9 Thread Functionality Thread States Thread Synchronization User level threads Kernel level threads

10 Thread states Running Ready Blocked Note:- There is no suspended state becos its only in the process level. If a process is suspended all threads associated with it are also suspended

11 Operations associated with threads Associated with change in thread state we have four basic thread operations Spawn: When a new process is spawned a thread for that process is also spawned. Block: When a thread need to wait for an event it will block. The processor may now turn to the execution of another ready thread. Unblock: After the event for a blocked thread occurs its moved into the ready queue Finish: After a thread completes, its register context and stacks are deallocated.

12 Performance benefits of threads that do not block and entire process

13 Multithreading example on a uniprocessor

14 Thread Synchronization All threads of a process share the same address space and other resources of a process Any alternation in the resources by one thread affects the environment of other threads in the same process Activities of various threads need to be properly synchronised

15 Types of threads User-level threads Kernel-level threads

16 User-level threads Application does the work of thread management Thread libraries are used for this purpose Kernel is unaware of the thread creations and schedules the process as a unit assigning a single execution state Thread library Data structure for the new thread Passes control to one of the threads within this process that is in the ready state

17 Relationship between ULT states and Process states

18 In figures b and c kernel switches control back to process B so execution resumes in thread 2

19 Advantages of ULT 1.Thread switching requires only user mode privileges. So its saves the overhead of two mode switches 2.Scheduling is application specific without disturbing the OS scheduler 3.ULTs run on any operating system. Thread library utilities are shared by all applications

20 Disadvantage of ULT Many system calls are blocking system calls. This not only blocks that thread but all threads in a process Multithreaded application cannot take advantage of multiprocessing. One single thread within a process can execute at a time.

21 How to overcome the disadvantages? Write an application as multiple processes rather than multiple threads. This eliminates the advantage of threads. Overhead increases Jacketing:- convert a blocking system call into nonblocking system call. In this case instead of calling a I/O routine a I/o jacket routine is called which checks whether the I/O is buzy or not. If I/O is buzy it enters the Ready state and passes control to another thread.

22 Kernel level threads No thread management code in the application area Just an API All threads are supported by a single process. Scheduling by kernel is done by thread basis

23 Advantages and disadvantages of KLT Advantages – Kernel can simultaneously schedule multiple threads from the same process on multiple processors – If one thread is blocked the kernel can schedule another thread Disadvantage – Transfer of control is mode switch

24 Combined Approaches Thread creation, scheduling, synchronization of threads is done in the user space. Multiple ULTs can be mapped onto some number of KLTs Multiple threads within the same application can run in parallel on multiple processors Blocking system call need not block the entire process

25 Relationship Between Threads and Processes

26 TRIX Follows a many-to-many relationship between threads and processes There is a concept of domain and thread – Domain is a static entity consisting of an address space and ports through which messages may be send and received – Thread is a single execution path with an execution stack, processor state and scheduling information Multiple threads may execute in a single domain or a single user activity can be performed in multiple domains as well

27 Implementation of TRIX 1.Single process. Requires sufficient main memory but I/O subprogram requires a small address space 2.Main program and I/O can be implemented as two separate processes. 3.Main program and I/O subprogram as a single activity implemented as a single thread. But one domain to be created for the main program and one for I/O subprogram. Thread can be moved between two address spaces

28 One-To-Many Relationship Used in distributed operating systems. Eg Cloud operating systems Activity CASE STUDIES ON trix CASE STUDIES ON CLOUD OS

29 UNIX SVR4 Process Management In UNIX most of the OS executes within the environment of a user process. So it has user and kernel mode of execution

30 Categories of process in unix System processes – Run in kernel mode – Executes OS code to perform administrative and housekeeping function User Processes – Run in user mode – Executes user programs and utilities in kernel mode to execute instructions which belong to the kernel – Enters the kernel mode by issuing a system call when an exception is generated or when an interrupt occurs

31 UNIX process state transition Diagram

32 Process states in Unix

33 Processes which are unique in UNIX Process 0 is a special process that is created when the system boots. (Swapper process) Process 0 spawns process 1 (init process) All processes in UNIX has process 1 as its ancestor

34 Process Description Process Image User –level context Register ContextSystem level context

35 User-level context

36 Register context

37 System level context

38 Process Control When a parent process is forked(kernel mode) – A slot is allocated in the process table for the new process – An unique process ID is assigned to child process – Copy of process image of the parent is made – Increments counters for any files owned by the parent – Assigns the child process to a ready to run state – Returns the ID number of the child to the parent process and 0 to child process

39 Dispatcher routine Stay in parent process. Control returns to user mode at the point of fork call of parent Transfer control to child process Transfer control to another process. Both parent and child are left in Ready to Run state.

40 Linux process and thread mgt Task- task struct Task-struct – State:Execution state of a process – Scheduling information – Identifiers- process has UID and group has GID – Interprocess communicaiton – Links – Times and timers – File system – Virtual memory – Processor-specific context

41 Linux process/Thread model


Download ppt "Threads G.Anuradha (Reference : William Stallings)"

Similar presentations


Ads by Google