1 / 32

Processes

Processes. Chapter 3. Part I Threads. Processes. Process = a program in execution OS creates an extended machine with multiple virtual CPUs OS keeps track of all processes (process tables) Processes are independent of one another OS protects processes from one another (w H/W support).

semah
Download Presentation

Processes

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. Processes Chapter 3 Part I Threads

  2. Processes • Process = a program in execution • OS creates an extended machine with multiple virtual CPUs • OS keeps track of all processes (process tables) • Processes are independent of one another • OS protects processes from one another (w H/W support)

  3. Overhead of Processes • Processes allow the OS to overlap I/O and computation, creating an efficient system • Overhead: • Process creation • Context switching • Swapping • All require kernel intervention

  4. Kernel Intervention • Context switching as the result of IPC

  5. Threads (1) PC PC PC PC Multi-threaded Process Traditional Process • A set of threads share the same address space • Enhance the process concept

  6. Threads (2) • Multithreaded-programs are easy to map to multiprocessors • It is easier to engineer applications with threads

  7. Threads Versus Processes (1) • Threads (same address space) are not protected from each other • Require care from developers • Context switching is cheaper

  8. Threads Versus Processes (2) • Traditional process executes a system call, it must block • A thread executes a system call, peer threads may still proceed

  9. Thread Implementation - Packages • Threads are provided as a package, including operations to create, destroy, and synchronize them • A package can be implemented as: • User-level threads • Kernel threads

  10. User-Level Threads • Thread library entirely executed in user mode • Cheap to manage threads • Create: setup a stack • Destroy: free up memory • Context switch requires few instructions • Just save CPU registers • Done based on program logic • A blocking system call blocks all peer threads

  11. Kernel-Level Threads • Kernel is aware of and schedules threads • A blocking system call, will not block all peer threads • Expensive to manage threads • Expensive context switch • Kernel Intervention

  12. Light-Weight Processes (LWP) • Support for hybrid (user-level and Kernel) threads • A process contains several LWPs • In addition, the system provides user-level threads • Developer: creates multi-threaded applications • System: Maps threads to LWPs for execution

  13. Thread Implementation – LWP (1) • Combining kernel-level lightweight processes and user-level threads.

  14. Thread Implementation – LWP (2) • Each LWP offers a virtual CPU • LWPs are created by system calls • They all run the scheduler, to schedule a thread • Thread table is kept in user space • Thread table is shard by all LWPs • LWPs switch context between threads

  15. Thread Implementation – LWP (3) • When a thread blocks, LWP schedules another ready thread • Thread context switch is completely done in user-address space • When a thread blocks on a system call, execution mode changes from user to kernel but continues in the context of the current LWP • When current LWP can no longer execute, context is switched to another LWP

  16. LWP Advantages • Cheap thread management • A blocking system call may not suspend the whole process • LWPs are transparent to the application • LWPs can be easily mapped to different CPUs • Managing LWPs is expensive (like kernel threads)

  17. Threads and Distributed Systems • A blocking system call will not block the entire process • Very important to maintain multiple logical connections between many clients and a server

  18. Multi-Threaded Clients • High propagation delay in big networks • WAN: order of seconds for a round trip • To hide this delay, use threads • Initiate communication by a separate thread • Example: browsers • Separate connection for each page component • Better: if server is horizontally distributed

  19. Multi-Threaded Servers (1) • Much more Important than multi-threaded clients • Simplifies server code • Boosts server performance • It is common to have multiple-CPU (Multiprocessor) server machines • Example: File Server

  20. Multi-Threaded Servers (2) • A multithreaded server organized in a dispatcher/worker model.

  21. Multithreaded Servers (2) • Three ways to construct a server.

  22. Java Threads (1) • Implemented by the Thread class, which is part of the java.lang package • Thread is system-dependent: • Actual implementation is provided by OS • Unified interface with the OS

  23. Java Threads (2) • Every thread runs within a specific method in an object • This object is not an instance of the Thread class • It is an instance of a class that extends Thread or implements the Runnable Interface

  24. Creating Threads • Threads execute methods • class MyThread extends Thread { public void run () { System.out.println(“Hello World!”); } • }

  25. Running Threads • To execute a thread: • Instantiate an object (MyThread) • Call start() on the object (which calls run()) • class TestThread { public static void main (String a[]) { new MyThread().start(); } • } • main is executed in a separate thread

  26. Runnable Threads • class MyThread implements Runnable { • public void run () { System.out.println(“Hello World!”); } } • class TestThreads { public static void main (String a[]) { new Thread(new MyThread()).start(); } }

  27. Threads Example • class MyThread extends Thread { • String mySymbol; • public MyThread (String s) { • mySymbol = s; } public void run () { while (1) System.out.println(mySymbol); } } • class TestThreads { public static void main (String a[]) { new MyThread(“A”).start(); new MyThread(“B”).start(); } }

  28. Output • AAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBB …

  29. Preempting Threads • yield() a class method in Thread • When invoked the scheduler suspends the thread invoking it and chooses another ready thread for dispatching • public void run() { • while (1) { • System.out.println(“Hello World!”); • Thread.currentThread().yield(); }}

  30. Output • ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABA…

  31. Thread States suspend() wait() sleep() … New Runnable Not Runnable start() resume() notify() … stop() stop() Scheduler yield() Scheduler Dead Runnable stop() run() exits stop()

  32. Thread Priorities • Priorities in Java are between MIN_PRIORITY (=1) and MAX_PRIORITY (=10) • The main method is assigned NORM_PRIORITY (=5) • Priorities are set by setPriority() • Priorities are extracted by getPriority()

More Related