400 likes | 530 Views
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
E N D
CS 217 Operating Systems Multithreaded Programming Lecture 08 SomiaRazzaq 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 • To discuss the Java thread libraries
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.
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.
Thread Concept • Threads have their own • Thread ID • CPU context (PC, SP, register set, etc.) • Stack • Priority • errno
Thread Concept • Threads share • Code and data • Open files (through the PPFDT) • Current working directory • User and group IDs • Signal setups and handlers • PCB
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
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)
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
Advantages of Threads • Resource sharing • Process resources (code, data, etc.) • OS resources (PCB, PPFDT, etc.)
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
Advantages of Threads • Performance in multi-processor and multi-threaded architectures (e.g., Intel’s P4 HT) • Multiple threads can run simultaneously
Disadvantages of Threads • Resource sharing— synchronization needed between threads • Difficult to write and debug multi-threaded programs
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
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
Multicore Programming • Multicore systems putting pressure on programmers • Challenges include • Dividing activities • Balance • Data splitting • Data dependency • Testing and debugging
Multithreaded Models • Support for threads may be provided at • User level or • Kernel level
User Threads Thread management done by user-level threads library without kernel support Three primary thread libraries: POSIX Pthreads Win32 threads Java threads
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
Multithreading Models A relationship must exist between the user and kernel threads Many-to-One One-to-One Many-to-Many
Many-to-One Many user-level threads mapped to single kernel thread Examples: Solaris Green Threads GNU Portable Threads
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
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
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
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
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)
Java Threads • Java threads are managed by the JVM • Java threads may be created by: • Implementing the Runnable interface
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()