Presentation is loading. Please wait.

Presentation is loading. Please wait.

Department of Computer Science and Software Engineering

Similar presentations


Presentation on theme: "Department of Computer Science and Software Engineering"— Presentation transcript:

1 Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-2 Threads
Department of Computer Science and Software Engineering University of Wisconsin-Platteville

2 Outlines Process Aspects process vs. Threads Multithreading Process
User and Kernel Threads Thread Pool

3 Process Aspects Process can be viewed two ways
unit of resource ownership address space, containing program code and data open files, may be using an I/O device unit of scheduling execution path that may be interleaved with other processes These two aspects are treated independently by the operating system process = unit of resource ownership thread = unit of scheduling Operating system supports multiple threads of execution within a single process

4 Process vs. Thread Process Thread A process is a multithread where
address space, program code, global variables, heap, OS resources : files, I/O devices Thread is a single sequential execution stream within a process A process is a multithread where Each thread has its own (other threads can access but shouldn’t) registers, program counter (PC) , stack, stack pointer (SP) All threads shares process resources Threads executes concurrently Each thread can block, fork, terminate, synchronize

5 Process Types

6 Multithreading Process: When
Programs require multithreading programs with multiple independent tasks to provide quick user-response while carrying out main task server which needs to process multiple independent requests simultaneously OS kernel repetitive numerical tasks — break large problem down into small problems and assign each one to a separate thread programs difficult to multithread: programs that don’t have multiple concurrent tasks multi-task programs that require protection between tasks

7 Benefits of Threads Takes less time to create a new thread than a process Less time to terminate a thread than a process Less time to switch between two threads within the same process Since threads within the same process share memory and files, they can communicate with each other without invoking the kernel

8 Thread Termination Suspending a process involves suspending all threads of the process all process threads share the same address space Termination of a process, terminates all threads within the process

9 Thread States States associated with a change in thread state Spawn
Spawn another thread Block Unblock Finish De-allocate register context and stacks

10 Thread States Example:
Remote Procedure Call (RPC) Using Threads

11 Thread Types User-Level Threads Kernel-Level Threads

12 User-Level Threads provide a library of functions to allow user processes to manage (create, delete, schedule) their own threads OS is not aware of threads advantages: doesn’t require modification to the OS simple representation — each thread is represented simply by a PC, registers, stack, and a small control block, all stored in the user process’ address space simple management — creating a new thread, switching between threads, and synchronization between threads can all be done without intervention of the kernel fast — thread switching is not much more expensive than a procedure call flexible — CPU scheduling (among those threads) can be customized to suit the needs of the algorithm

13 User-Level Threads disadvantages:
lack of coordination between threads and OS kernel process as a whole gets one time slice same time slice, whether process has 1 thread or 1000 threads also — up to each thread to relinquish control to other threads in that process requires non-blocking system calls otherwise, entire process will blocked in the kernel, even if there are runnable threads left in the process

14 Kernel-Level Threads kernel provides system calls to create and manage threads advantages kernel has full knowledge of all threads - scheduler may choose to give a process with 10 threads more time than process with only 1 thread good for applications that frequently block (e.g., server processes with frequent interprocess communication) disadvantages: slower — thread operations are significantly slower than for user-level threads significant overhead and increased kernel complexity — kernel must manage and schedule threads as well as processes - requires a full thread (task) control block (TCB) for each thread

15 Multithreading Models
Many-to-One (Solaris’ green threads) – all user threads are mapped to one kernel thread: same problems as with user threads One-to-One (Windows XP, OS/2) – one user thread to one kernel thread programmer has better control of concurrency does not block the process on one thread blocking may potentially waste resources Many-to-Many – multiplexes many user-level threads to a smaller or equal number of kernel-level threads allocation is specific to a particular machine (more k. threads may be allocated on a multiprocessor)

16 Multithreading Models

17 Thread Pools threads may not be created infinitely – overwhelm computer resources consider web-server on-demand thread creation may not be fast enough thread pool create a number of threads in a pool where they remain blocked until activated activate when request arrives if more requests than threads – extra requests have to wait performance controllable process size fast response until pool exhausted

18 Thread APIs POSIX threads (pthreads) Win32 threads Java threads
A POSIX standard (IEEE c) API for thread creation and synchronization Common in UNIX operating systems (Solaris, Linux, MacOS X) implemented on top of “native” OS threads or as a user-level package Win32 threads one-to-one mapping implemented on Windows OSes Java threads specified as part of Java maintained by JVM implemented on native threads or as a user-level package


Download ppt "Department of Computer Science and Software Engineering"

Similar presentations


Ads by Google