CS 2200 - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

CS 2200

Description:

What about a single users application? Is there a way to make it more efficient ... while(forever) { pthread_mutex_lock(padlock); while (empty) ... – PowerPoint PPT presentation

Number of Views:101
Avg rating:3.0/5.0
Slides: 43
Provided by: BillL161
Category:
Tags: forever

less

Transcript and Presenter's Notes

Title: CS 2200


1
CS 2200
  • Threads

2
Recall
  • Process
  • Program Counter
  • Registers
  • Stack
  • Code (Text)
  • Data
  • Page Table
  • etc.
  • Processes must be protected from one another.

Memory
3
Recall
  • Context Switching
  • Requires considerable work
  • What about a single users application?
  • Is there a way to make it more efficient
  • In effect, allow the user to have multiple
    processes executing in the same space?
  • Yes, solution Threads or Multithreading

4
What is Multithreading?
  • Allowing program to do multiple tasks
  • E.g. spell check while user types on
  • Is it a new technique?
  • has existed since the 70s (concurrent Pascal,
    Ada tasks, etc.)
  • Why now?
  • Hyperthreading, Dual-core processors
  • ALL computers parallel within a few years

5
What is a Thread?
  • Basic unit of CPU utilization
  • A lightweight process (LWP)
  • Consists of
  • Program Counter
  • Register Set
  • Stack Space
  • Shares with peer threads
  • Code
  • Data
  • OS Resources
  • Open files
  • Signals

6
Threads
  • Can be context switched more easily
  • Registers and PC
  • Not memory management
  • Can use processors concurrently
  • Share CPU if only one in the system
  • May (Will) require concurrency control
    programming like mutex locks.

7
Threads in a Uniprocessor?
Process
active
  • Allows concurrency between I/O and user
    processing even in a uniprocessor box

8
Threads and OS
  • Unix - memory layout

User
Kernel code and data
Kernel
  • Protection between user and kernel?

9
Threads and OS
  • Traditionally, single threaded programs
  • One PC per program (process), one stack, one set
    of CPU registers
  • If a process blocks (say disk I/O, network
    communication, etc.) then no progress for the
    program as a whole

10
MultiThreaded Operating Systems
  • How widespread is support for threads in OS?
  • Digital Unix, Sun Solaris, Win9x, Win NT, Win2k,
    Linux, Free BSD, etc
  • Process vs. Thread?
  • In a single threaded program, the state of the
    executing program is contained in a process
  • In a multithreaded program, the state of the
    executing program is contained in several
    concurrent threads

11
Process Vs. Thread
P1
P2
user
PCB
PCB
kernel
Kernel code and data
  • Computational state (PC, regs, ) for each thread
  • How different from process state?

12
Memory Layout
  • Multithreaded program has a per-thread stack
  • Heap, static, and code are common to all threads

13
Threads
  • Threaded code different from non-threaded?
  • Protection for data shared among threads
  • Mutex
  • Synchronization among threads
  • Way for threads to talk (signal) to one another
  • Thread-safe libraries

NOTES strtok is unsafe for multi-thread
applications. strtok_r is MT-Safe and should be
used instead.
14
Typical Operation
  • Main programs creates (or spawns) threads
  • Threads may
  • perform one task and die
  • last for duration of program
  • Threads must
  • be able to synchronize activity
  • communicate with one another

15
Programming Support for Threads
  • Creation
  • pthread_create(top-level procedure, args)
  • Termination
  • return to top-level procedure
  • explicit cancel
  • Rendezvous
  • creator can wait for children
  • pthread_join(child_tid)
  • Synchronization
  • pthread_mutex_...
  • condition variables (pthread_cond_...)

16
Programming with Threads
  • Synchronization
  • For coordination of the threads
  • Communication
  • For inter-thread sharing of data
  • Threads can be in different processors
  • How to achieve sharing in SMP?
  • Software accomplished by keeping all threads in
    the same address space by the OS
  • Hardware accomplished by hardware shared memory
    and coherent caches

17
Synchronization Primitives
  • lock and unlock
  • mutual exclusion among threads
  • busy-waiting vs. blocking
  • pthread_mutex_trylock no blocking (rare)
  • pthread_mutex_lock blocking
  • pthread_mutex_unlock
  • condition variables
  • pthread_cond_wait block for a signal
  • pthread_cond_signal signal one waiting thread
  • pthread_cond_broadcast signal all waiting threads

18
Example
  • lock(mutex)
  • while (resource_state BUSY)
  • //spin
  • resource_state BUSY
  • unlock(mutex)
  • use resource
  • lock(mutex)
  • resource_state FREE
  • unlock(mutex)

Thread 1
19
Example
  • lock(mutex)
  • while (resource_state BUSY)
  • //spin
  • resource_state BUSY
  • unlock(mutex)
  • use resource
  • lock(mutex)
  • resource_state FREE
  • unlock(mutex)

Thread 2
Thread 1
20
Example
  • lock(mutex)
  • while (resource_state BUSY)
  • //spin
  • resource_state BUSY
  • unlock(mutex)
  • use resource
  • lock(mutex)
  • resource_state FREE
  • unlock(mutex)

Thread 2
Thread 1
21
Example with cond-var
  • lock(mutex)
  • while(resource_state BUSY)
  • wait(cond_var) / implicitly give up mutex /
  • / implicitly re-acquire mutex
    /
  • resource_state BUSY
  • unlock(mutex)
  • / use resource /
  • lock(mutex)
  • resource_state FREE
  • unlock(mutex)
  • signal(cond_var)

22
Example with cond-var
  • lock(mutex)
  • while(resource_state BUSY)
  • wait(cond_var) / implicitly give up mutex /
  • / implicitly re-acquire mutex
    /
  • resource_state BUSY
  • unlock(mutex)
  • / use resource /
  • lock(mutex)
  • resource_state FREE
  • unlock(mutex)
  • signal(cond_var)

T1
23
Example with cond-var
  • lock(mutex)
  • while(resource_state BUSY)
  • wait(cond_var) / implicitly give up mutex /
  • / implicitly re-acquire mutex
    /
  • resource_state BUSY
  • unlock(mutex)
  • / use resource /
  • lock(mutex)
  • resource_state FREE
  • unlock(mutex)
  • signal(cond_var)

T2
T1
24
pthreads
  • Mutex
  • Must create mutex variables
  • pthread_mutex_t padlock
  • Must initialize mutex variable
  • pthread_mutex_init(padlock, NULL)
  • Condition Variable (used for signaling)
  • Must create condition variables
  • pthread_cond_t non_full
  • Must initialize condition variables
  • pthread_cond_init(non_full, NULL)

25
Classic CS ProblemProducer Consumer
  • Producer
  • If (! full)
  • Add item to buffer
  • empty FALSE
  • if(buffer_is_full)
  • full TRUE
  • Consumer
  • If (! empty)
  • Remove item from buffer
  • full FALSE
  • if(buffer_is_empty)
  • empty TRUE

buffer
...
26
Example Producer Threads Program
  • while(forever)
  • // produce item
  • pthread_mutex_lock(padlock)
  • while (full)
  • pthread_cond_wait(non_full, padlock)
  • // add item to buffer
  • buffercount
  • if (buffercount BUFFERSIZE)
  • full TRUE
  • empty FALSE
  • pthread_mutex_unlock(padlock)
  • pthread_cond_signal(non_empty)

27
Example Consumer Threads Program
  • while(forever)
  • pthread_mutex_lock(padlock)
  • while (empty)
  • pthread_cond_wait (non_empty, padlock)
  • // remove item from buffer
  • buffercount--
  • full false
  • if (buffercount 0)
  • empty true
  • pthread_mutex_unlock(padlock)
  • pthread_cond_signal(non_full)
  • // consume_item

28
Using Threads
  • Servers
  • dispatcher model

dispatcher
Workers
Mailbox
29
  • Team model

Mailbox
  • Pipelined model

Mailbox
30
Threads Implementation
  • User level threads
  • OS independent
  • Scheduler is part of the runtime system
  • Thread switch is cheap (save PC, SP, regs)
  • Scheduling customizable, i.e., more application
    control
  • Blocking call by thread blocks process

31
  • Solution to blocking problem in user level
    threads
  • Non-blocking version of all system calls
  • Switching among user level threads
  • Yield voluntarily
  • How to make preemptive?
  • Timer interrupt from kernel to switch

32
  • Kernel Level
  • Expensive thread switch
  • Makes sense for blocking calls by threads
  • Kernel becomes complicated process vs. threads
    scheduling
  • Thread packages become non-portable
  • Problems common to user and kernel level threads
  • Libraries
  • Solution is to have thread-safe wrappers to such
    library calls

33
Questions?
34
Solaris Threads
  • Three kinds
  • user, lwp, kernel
  • User Any number can be created and attached to
    lwps
  • One to one mapping between lwp and kernel threads
  • Kernel threads known to the OS scheduler
  • If a kernel thread blocks, associated lwp, and
    user level threads block as well

35
Solaris Terminology
36
More Conventional Terminology
Processes
P1
P2
P3
Thread
kernel thread (user-level view)
(Inside the kernel)
37
Questions?
  • Does kernel see user threads?
  • Are all threads from P3 on same CPU?
  • Are all threads in kernel attached to lwps?
  • If the left-most thread of P3 issues a blocking
    system call does P3 block?
  • Who schedules lwps?
  • Who schedules threads?

38
Kernel Threads vs. User Threads
  • Advantages of kernel threads
  • Can be scheduled on multiple CPUs
  • Can be preempted by CPU
  • Kernel scheduler knows their relative priorities
  • Advantages of user threads
  • (Unknown to kernel)
  • Extremely lightweight No system call to needed
    to change threads.

39
Questions?
40
Things to know?
  • 1. The reason threads are around?
  • 2. Benefits of increased concurrency?
  • 3. Why do we need software controlled "locks"
    (mutexes) of shared data?
  • 4. How can we avoid potential deadlocks/race
    conditions.
  • 5. What is meant by producer/consumer thread
    synchronization/communication using pthreads?
  • 6. Why use a "while" loop around a
    pthread_cond_wait() call?

41
Things to know?
  • 7. Why should we minimize lock scope (minimize
    the extent of code within a lock/unlock block)?
  • 8. Do you have any control over thread
    scheduling?

42
Questions?
Write a Comment
User Comments (0)
About PowerShow.com