Docstoc

Threads

Document Sample
Threads Powered By Docstoc
					Threads



Irfan Khan
Myo Thein
           What Are Threads ?
   a light, fine, string like length of material
    made up of two or more fibers or
    strands of spun cotton, flax, silk, etc.
    twisted together and used in sewing

    Webster’s New World Dictionary
         Overview of Process
   An abstraction of a running program

   A process includes the code, current
    value of the program counter,
    registers, and variables

   CPU switches from process to process
                    Process Image
   User program
   User data
   Stack(s)
       For function calls and parameter passing
   Process control block           (execution context, or state)
       Pointers to all of the above
       Attributes needed by the OS to control the process
            Process identification information
            Processor state information
            Process control information
     Process Identification in PCB
   A few numeric identifiers may be used
       Unique process identifier
            Indexes (directly or indirectly) into the primary process
             table
       User identifier
            The user who initiated the process
            Effective user: the user whose permissions the process
             inherits
       Identifier for the process that created this process
            I.E. A pointer to the process’ parent
    Processor State Information in PCB
   Contents of processor registers
        User-visible registers
        Control and status registers
        Stack pointers
   Program status word (PSW)
        Contains status information
        Example: the EFLAGS register on Pentium
         machines
    Process Control Information in PCB
   Scheduling and state information
        Process state (i.E.: Running, ready, blocked...)
        Priority of the process
        Event for which the process is waiting (if blocked)
   Process privileges
        Access to certain memory locations
        OS resources
   Memory management
        Pointers to segment/page tables assigned to this process
   Resource ownership and utilization
        Resource in use: open files, I/O devices...
        History of usage: accounting (of CPU time, I/O...)
                Process Creation
   Assign a unique process identifier
   Allocate space for the process image
   Initialize process control block
       Many default values (example: state is
        new, no I/O devices or files...)
   Set up appropriate linkages
       Example: add new process to linked list
        used for the scheduling queue
                      Context Switching
   A context switch may occur whenever the OS
    is invoked
       System call
            Explicit request by the program, such as open file
            The process will likely be blocked
                  OS will dispatch a new process
       Trap
            An error resulted from the last instruction
                  May cause the process to be moved to the terminate state
       Interrupt
            The cause is external to the execution of the current
             instruction
            Control is transferred to the interrupt handler
   Hence the OS is event driven
        Steps in Context Switching
   Save context of processor including program
    counter and other registers
   Update the PCB of the running process with
    its new state and other associate info
   Move PCB to appropriate queue - ready,
    blocked
   Select another process for execution
   Update PCB of the selected process
   Restore CPU context from the PCB
             Introduction to Threads
   A process can be considered as based on
       Resource grouping
       Execution
       Its own address space
   A thread represents the execution part of a process.
   A thread has a program counter, register states,
    stack pointer.
   All threads of a process share its address space.
   All threads of a process share its resources.
Threads in Process
    Why Use Threads Over Processes
   Both thread and process models provide concurrent
    program execution
   Creating new process can be expensive
      It takes time: calling into the OS kernel is needed

      Can trigger process rescheduling activity: context

       switch
      It takes up memory resource: entire process is

       replicated
   Communication and synchronization is expensive
      Requiring calling into the OS kernel
Why Use Threads Over Processes, contd
   Threads can be created without replicating an entire
    process
   Most of the work of creating a thread is done in user
    space rather than the OS kernel
   Thread can synchronize by monitoring a variable, as
    opposed to processes that require calling into the OS
    kernel
   The benefits of the thread model results from staying
    inside the user address space of the program
   Multithreading – multiple threads in the same
    process
   Multithreading OS
          MS-DOS – single process, single thread
          UNIX – multiple processes, single thread per process
          JVM – single process with multiple threads
          Windows 2000, Linux, OS/2, Solaris – multiple processes
           with multiple threads
   Thread states – running, blocked, or ready.
   Each thread has its own stack.
                  Thread Usage
   Example: word processor
       Displays contents;
       Edits (e.G. Typing);
       Reformats;
       Auto saves;
       Printing etc.
Why Threads Become Popular Now?
   SMPs (symmetric multiprocessors)
       2 to 64 processors sharing
             Buss
            I/O system
            Same memory
       One operating system for all processors
   Examples:
       SGI PowerChallenge (8 MIPS 1000 CPUs) -- CYC807
       Sun ultra enterprise 6000 (8 CPUs) -- CYC807
       ALR SMP server (4 Pentium pro) - CYC414
      Three Types of Thread System
   Kernel-supported threads (mach, OS/2, NT)
   User-level threads; Supported above the
    kernel, via a set of library calls at the user
    level (linux via clone)
   Hybrid approach implements both user-level
    and kernel-supported threads (Solaris 2)
    Kernel-level Versus User-level Threads
   User-level thread
       User-level activities; No kernel involvement
       Basic scheduling unit in OS is process
       Threads of the same process can not run on
        different CPUs in SMP in parallel
   Kernel-level thread
       Each process consists of several threads
       Basic scheduling unit is thread
       Can run on different CPUs in SMP in parallel
        Advantages of Kernel Threads
   Higher application throughput
     If there were no kernel thread support
            Need I/O; It means the process goes into
             waiting state and wait until the I/O is complete
       With multiple kernel threads per task
            Block the I/O requesting thread and continue
             to work on another thread
            Increases the overall throughput of the
             application
    Advantages of User Level Threads
   Threads are cheap
       Can be implemented at user levels, no
        kernel resources
   Threads are fast
       No system calls, switching modes involved
                     Sun Solaris 2
   Mixed approach
       OS schedules light-weight process (LWP)
       User-level library schedules user-level threads
   User threads are cheap, can be thousands
    per task
   Each LWP supports one or more user threads
       LWPs are what we’ve been calling kernel threads
       Solaris has entities called kernel threads; They are
        scheduling artifacts contained in the OS
       User/ Kernel Threads in Sun Solaris 2
                                           User-level thread
Light weight     Task 1         Task 2      Task 3
process (LWP)




Kernel thread

                                   KERNEL


                CPU       CPU        CPU        CPU
Thank You

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:12
posted:1/26/2011
language:English
pages:26