Threads by 2RCa21



                                  Thread model
                                  Thread usage
                                  Implementing threads
                                  Scheduler activations
                                  Making single-threaded code

Fabián E. Bustamante, Fall 2005
The problem with processes
 Many programs need to perform largely independent
 tasks that do not need to be serialized
  – E.g. web server, text editor, database server, some scientific
    applications, …
 If you only have processes
  – A listener process waits for requests &
  – Forks a new process (per each request!)
  – Overhead of fork, IPC, processes (kernel resources), inability
    to share resources among processes (such as network
    connections) …

                            CS 343 Operating Systems
                           EECS, Northwestern University             2
     The thread model
           – 1 process – 1 address space – 1 thread of execution
           – Process = resource grouping + execution stream
         Thread model:
           – Process to group resources
           – Multiple threads for execution

3 traditional heavyweight processes


                                User space                                          User space

                               Kernel space                                        Kernel space

                         (a)                                             (b)
                                                                                   (a) 3 processes w/ 1 thread each
                                                                                   (b) 1 process w/ 3 threads
                                                    CS 343 Operating Systems
                                                   EECS, Northwestern University                                      3
The thread model
 Concurrency & parallelism
  – Concurrency – what‟s possible with infinite processors
       • Provided at the
           – System level: Kernel recognizes multiple threads of control within a
             process & schedules them independently
           – Application level: Through user-level thread library; a good
             structuring tool
  – Parallelism – your actual degree of parallel exec.
 Threads states ~ processes states
 One stack per thread – w/ one frame per procedure
 called but not yet returned from
 Common calls
  –   thread_create()
  –   thread_exit()
  –   thread_wait()
  –   thread_yield() (why would you need this?)

                                 CS 343 Operating Systems
                                EECS, Northwestern University                       4
The thread model
 Share and private items
     Per process                        Per thread
     Address space                      Program counter
     Global variables                   Registers
     Open files                         Stack
     Child processes                    State
     Pending alarms
     Signals and signal handlers
     Accounting information

 No protection bet/ threads
  (Should they be?)

                            CS 343 Operating Systems
                           EECS, Northwestern University   5
A simple example
void do_one_thing(int *ptimes)               void do_wrap_up(int one, int
{                                               another)
  int i, j, k;                               {
                                               int total;
  for (i = 0; i < 4; i++) {                    total = one + another;
    printf(“doing one\n”);                     printf(“wrap up: one %d, another
    for (j = 0; j < 1000; j++)                  %d and total %d\n”, one,
                                                another, total);
      x = x + i;                             }
} /* do_one_thing! */                        int main (int argc, char *argv[])
void do_another_thing(int *ptimes)             do_one_thing(&r1);
{                                              do_another_thing(&r2);
  int i, j, k;                                 do_wrap_up(r1,r2);
  for (i = 0; i < 4; i++) {                  } /* main! */
    printf(“doing another\n”);
    for (j = 0; j < 1000; j++)
      x = x + i;
} /* do_another_thing! */

                                  CS 343 Operating Systems
                                 EECS, Northwestern University                    6
Layout in memory & threading
                                Virtual Address Space
             SP                                            Lowest address
             PC                                            Stack
             GP1                 do_another_thing()
             …                   i, j, k
Thread 2
             SP                  do_one_thing()
 Registers                                                 Stack
             PC                  i, j, k
             GP0                 _______________________
Thread 1
 Identity                          --
             GID                 do_one_thing()
             …                     --

 Resources   Open Files          r1
             Locks               r2

                                                           Highest address

                           CS 343 Operating Systems
                          EECS, Northwestern University                      7
Using threads
 A word processor with three threads
  – Input, formatting and backup
 A web server
  – Single-threaded: no parallelism,
    blocking system calls
  – Event-driven: parallelism,
                                                                            Web server
    nonblocking system calls,                                               process

    interrupts                                             Dispatcher
  – Multithreaded: parallelism,
    blocking system calls
                                                                                         User space

                                                                                    Kernel space

                            CS 343 Operating Systems
                           EECS, Northwestern University                                              8
Implementing threads in user space
 Kernel unaware of threads – no modification required
 (many-to-one model)
 Run-system: a collection of procedures
 Each process needs its own thread table
  –   Thread switch is very fast
  –   No need for kernel support
  –   Customized scheduler
  –   Each process ~ virtual processor
 Cons - „real world‟ factors
  – Multiprogramming, I/O, Page faults
  – Blocking system calls?

                              CS 343 Operating Systems
                             EECS, Northwestern University   9
Implementing threads in the kernel
 One-to-one model
 No need for runtime system
 No wrapper for system calls
 Creating threads is more expensive – recycle
 System calls are expensive

                       CS 343 Operating Systems
                      EECS, Northwestern University   10
Hybrid thread implementations
 Trying to get the best of both worlds
 Multiplexing user-level threads onto kernel- level
 threads (many-to-many model)
 One popular variation – two-level model (you can
 bound a user-level thread to a kernel one)

                        CS 343 Operating Systems
                       EECS, Northwestern University   11
Costs of threads

                      User threads        LWP/Kernel-           Processes
                                          level threads

  Creation time            52                      350             1700

  Synchronization          66                      390              200
  with semaphores

      On SPARCstation 2 – Time in μseconds
      (from Uresh Vahalia‟s “UNIX Internals”, PH 1996

                                 CS 343 Operating Systems
                                EECS, Northwestern University               12
        Scheduler activations*
               – Functionality of kernel threads & performance of user-level threads
             Problem – needed control & scheduling information distributed
             bet/ kernel & each app‟s address space
             Idea – closer integration between user thread & kernel
               – Kernel responsible for processor alloc. & thread library for sched.
               – Library informs kernel of events that affect processor allocation
             Two new abstractions
               – Scheduler activation – execution context used to ran a user thread
               – Upcalls – call made by the kernel to the thread library
             When kernel makes an upcall
               – It passes an activation to the library
               – Library use it to process the event, run a new thread or invoke a
                 system call
*Anderson et al., “Scheduler Activations: effective
Kernel Support for the User-level Management of
Parallelism,” Proc. of 13th SOSP, Oct. 1991.
                                                       CS 343 Operating Systems
                                                      EECS, Northwestern University    13
Scheduler activations
 Kernel does not timeslice activations on a processor
  – At any time – 1 process – 1 activation per processor assigned
 Handling blocking operations
  – When user thread blocks in kernel
     •   Kernel creates activation & upcalls library
     •   Library saves state of old thread
     •   .. informs kernel old activation can be reused
     •   .. schedules another user thread on new activation
  – When blocking operation completes
     • Kernel makes upcall to notify library (new activation)
     • … may assign new processor to run new activation or preempt
       one of the current activations of process
           – (2) Upcall must also notify of preempted thread
     • Library puts threads in queue & schedules whichever
 Pros – fast & smart
 Cons – upcalls violate layering approach

                                 CS 343 Operating Systems
                                EECS, Northwestern University        14
Thread libraries
 Pthreads – POSIX standard (IEEE 1003.1c) API for
 thread creation & synchronization
  – API specifies behavior of the thread library, implementation is
    up to development of the library
  – Common in UNIX operating systems (Solaris, Linux, Mac OS
 Win32 threads – slightly different (more complex API)
 Java threads –
  – Managed by the JVM
  – May be created by
      • Extending Thread class
      • Implementing the Runnable interface
  – Implementation model depends on OS (1-to-1 in Windows but
    many-to-many in early Solaris)

                             CS 343 Operating Systems
                            EECS, Northwestern University             15
Multithreaded C/POSIX
int sum;                         /*         int main (int argc, char *argv[])
   shared by the thread(s) */               {
                                              pthread_t tid;   /* thread id */
/* runner: the thread */
void *runner(void *param)                       /* set of thread attrs */
{                                               pthread_attr_t attr;
  int i, upper = atoi(param);   sum
   = 0;                                         if (argc != 2 || atoi(argv[1]) <
                                                  0) {
  for (i = 1; i < upper; i++)                      fprintf (stderr, "usage: %s
    sum += 1;                                     <integer>\n", argv[0]);
  pthread_exit(0);                                 exit(1);
} /* runner! */                                 }

                                                /* get default attrs */
                                                pthread_create(&tid, &attr,
                                                 runner, argv[1]);

                                              /* wait to exit */
                                              pthread_join(tid, NULL);
                                              printf("sum = %d\n", sum);
                                            } /* main! */

                                 CS 343 Operating Systems
                                EECS, Northwestern University                      16
Complications with threads
 Semantics of fork() and exec() system calls
  – fork() would normally create an almost exact copy of parent
      • Chance for deadlock?
 Other system calls (closing a file, lseek, cwd, …?)
 Signal handling, handlers and masking
  1. Send signal to each thread – too expensive
  2. Appoint a master thread per process – asymmetric threads
  3. Send signal to an arbitrary thread
  4. Use heuristics to pick thread (SIGSEGV & SIGILL – caused
     by thread, SIGTSTP & SIGINT – caused by external events)
  5. Create a new thread to handle each signal – situation specific
 Thread visibility
  – should a LWP be visible to another process?
 Stack growth?

                            CS 343 Operating Systems
                           EECS, Northwestern University              17
Single-threaded to multithreaded
 Threads and global variables
  – Prohibit global variables?
  – Assign each thread its own global variables
     • Allocate a chunk of memory and pass it around
     • Create new library calls to create/set/destroy global

                            CS 343 Operating Systems
                           EECS, Northwestern University       18
Single-threaded to multithreaded
 Many library procedures are not reentrant
  – Rewrite library?
  – Wrappers for each call?
 Signal handling
 Stack management

                       CS 343 Operating Systems
                      EECS, Northwestern University   19
OS: Windows XP Threads
 Implements the one-to-one mapping, but it also supports fibers
 (providing many-to-many model)
 Each thread contains:
  –   Thread id
  –   Register set
  –   Separate user & kernel stacks
  –   Private data storage area
 Register set, stacks, and private storage area – context of the
 Primary data structures of a thread include:      ETHREAD
  – ETHREAD (executive thread block)                                            KTHREAD

       • Pointer to process and where thread start                                        TEB

  – KTHREAD (kernel thread block)
       • Scheduling & synchronization info, kernel stack
  – TEB (thread environment block)
       • Thread id, user mode stack, thread specific data        Kernel space             User space

                                  CS 343 Operating Systems
                                 EECS, Northwestern University                                         20
OS: Linux threads
 Refers to as tasks rather than threads
 No distinction between processes/threads
 Thread creation is done through clone()
 clone() allows a child task to share the address space
 of the parent task (process)
 Some clone() flags:
  –   CLONE_FS – Share FS info
  –   CLONE_VM – Share memory
  –   CLONE_SIGHAND – Share handlers
  –   CLONE_FILES – Shared set of open files
 clone() called with all flags ~ pthread_create()
 clone() without any ~ fork()

                              CS 343 Operating Systems
                             EECS, Northwestern University   21
Next time … CPU Scheduling
 The basis of multiprogrammed OSs
 Introducing CPU scheduling
 …, various algorithms for it
 … and tell you a little on how to evaluate them

                     CS 343 Operating Systems
                    EECS, Northwestern University   22

To top