1 / 39

CS 217 Operating Systems

CS 217 Operating Systems. Multithreaded Programming Lecture 08 Somia Razzaq NFC IET Slides adopted from Silbershatz, Galvin and Gagne. Today’s Objectives. To introduce the notion of a thread a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems

Télécharger la présentation

CS 217 Operating Systems

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. CS 217 Operating Systems Multithreaded Programming Lecture 08 SomiaRazzaq NFC IET Slides adopted from Silbershatz, Galvin and Gagne

  2. Today’s Objectives • To introduce the notion of a thread • a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems • To discuss the Java thread libraries

  3. Issues with Processes • The fork() system call is expensive (it requires memory to memory copy of the • executable image of the calling process and allocation of kernel resources to the • child process) • IPC is required to pass information between a parent and its child processes.

  4. Thread Concept • A thread is a “lightweight” process which executes within the address space of a process. • A thread can be scheduled to run on a CPU as an independent unit and terminate. • Multiple threads can run simultaneously.

  5. Thread Concept • Threads have their own • Thread ID • CPU context (PC, SP, register set, etc.) • Stack • Priority • errno

  6. Thread Concept • Threads share • Code and data • Open files (through the PPFDT) • Current working directory • User and group IDs • Signal setups and handlers • PCB

  7. Single and Multithreaded Processes

  8. Threads are Similar to Processes • A thread can be in states similar to a process (new, ready, running, blocked, terminated) • A thread can create another thread

  9. Threads vs. Processes • Processes • Heavyweight - large startup costs • Separate address space • Cooperate with simple read/write streams (pipes) • Synchronization is easy -typically don't have shared address space (i.e. in some sense, fewer opportunities for bugs) • Threads • Lightweight -easy to create/destroy • All in one address space • Can share memory/variables directly • May require more complex synchronization logic to make the shared memory work (potentially hard)

  10. Advantages of Threads • Responsiveness • Multi-threaded servers (e.g., browsers) can allow interaction with user while a thread is formulating response to a previous user query (e.g., rendering a web page) • Keep the GUI responsive by separating the "worker" thread from the GUI thread -this helps an application feel fast and responsive

  11. Advantages of Threads • Resource sharing • Process resources (code, data, etc.) • OS resources (PCB, PPFDT, etc.)

  12. Advantages of Threads • Economy • Take less time to create, schedule, and terminate • Solaris 2: thread creation is 30 times faster than process creation and thread switching is five times faster than process switching

  13. Advantages of Threads • Performance in multi-processor and multi-threaded architectures (e.g., Intel’s P4 HT) • Multiple threads can run simultaneously

  14. Disadvantages of Threads • Resource sharing— synchronization needed between threads • Difficult to write and debug multi-threaded programs

  15. Examples of multithreaded programs • Embedded systems • Elevators, Planes, Medical systems, Wristwatches • Single Program, concurrent operations • Most modern OS kernels • Internally concurrent because have to deal with concurrent requests by multiple users • Database Servers • Access to shared data by many concurrent users • Also background utility processing must be done

  16. Examples of multithreaded programs (con’t) • Network Servers • Concurrent requests from network • Again, single program, multiple concurrent operations • File server, Web server, and airline reservation systems • Parallel Programming (More than one physical CPU) • Split program into multiple threads for parallelism

  17. Continue from here…

  18. Multithreaded Server Architecture

  19. Multicore Programming • Multicore systems putting pressure on programmers • Challenges include • Dividing activities • Balance • Data splitting • Data dependency • Testing and debugging

  20. Concurrent Execution on a Single-core System

  21. Parallel Execution on a Multicore System

  22. Multithreaded Models • Support for threads may be provided at • User level or • Kernel level

  23. User Threads Thread management done by user-level threads library without kernel support Three primary thread libraries: POSIX Pthreads Win32 threads Java threads

  24. Kernel Threads Supported and managed by the OS Kernel Examples of OS supporting kernel thread Windows XP/2000 Solaris Linux Tru64 UNIX Mac OS X

  25. Multithreading Models A relationship must exist between the user and kernel threads Many-to-One One-to-One Many-to-Many

  26. Many-to-One Many user-level threads mapped to single kernel thread Examples: Solaris Green Threads GNU Portable Threads

  27. Many-to-One Model • Thread management done by thread library in user space – efficient • Entire process blocks if a thread makes a blocking system call • Only one thread can access kernel at a time, multiple threads cannot run in parallel on multiprocessors

  28. One-to-One Model Each user-level thread maps to a kernel thread More concurrency than Many-to-One Model Allows other thread to run when one thread makes a blocking system call Multiple threads can access kernel at a time, allows multiple threads to run in parallel on multiprocessors Drawback: creating a user thread requires creating a kernel thread Expensive – most implementation constrain the number of threads supported Examples Windows NT/XP/2000 Linux Solaris 9 and later

  29. Many-to-Many Model Allows many user level threads to be mapped to many kernel threads Allows the operating system to create a sufficient number of kernel threads When a thread performs a blocking system call, the kernel can schedule another thread for execution Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package

  30. Two-level Model Similar to M:M, except that it allows a user thread to be bound to kernel thread Examples IRIX HP-UX Tru64 UNIX Solaris 8 and earlier

  31. Thread Libraries • Thread library provides programmer with API for creating and managing threads • Two primary ways of implementing • Library entirely in user space • Kernel-level library supported by the OS • Three main thread libraries are in use today • POSIX Pthreads (user or kernel-level) • Win32(kernel-level) • Java(directly managed in java programs)

  32. Java Threads • Java threads are managed by the JVM • Java threads may be created by: • Implementing the Runnable interface

  33. Thread creation steps (Using interface) • Step 1 - Implement the Runnable Interface class Worker implements Runnable • Step 2 - Provide an Implementation of run method public void run ( ){ // write thread behavior // code that will execute by thread } • Step 3 - Instantiate Thread object by passing runnable object in constructor Worker w = new Worker(“first”); Thread t = new Thread (w); • Step 4 – Start thread t.start()

  34. Java Threads Example Program

  35. Java Threads Example Program

  36. Java Thread States

  37. Java Threads Producer-Consumer

  38. Java Threads Producer-Consumer

More Related