CS241 System Programming Threads - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

CS241 System Programming Threads

Description:

CS241 MP1, MP2, MP3, MP4, MP5. Each is different. Share. Textbook. Personnel ... What is thread? Why do we need threads? Difference between process and thread ... – PowerPoint PPT presentation

Number of Views:82
Avg rating:3.0/5.0
Slides: 31
Provided by: yuanyu1
Category:

less

Transcript and Presenter's Notes

Title: CS241 System Programming Threads


1
CS241 System ProgrammingThreads
  • Klara Nahrstedt
  • Lecture 5
  • 1/30/2006

2
Overview
  • What is a thread?
  • Examples of using threads
  • Thread implementations
  • Summary

3
Administrative
  • Read T Chapter 2.1
  • Read RR 12

4
Process Review
  • So What Is A Process?
  • Its one executing instance of a program
  • Its separate from other instances
  • It can start (launch) other processes
  • Whats in a process?
  • Code (text), data, stack, heap
  • Process control block (PCB)
  • Process state, priority, accounting
  • Program counter, register variables, stack
    pointers, etc
  • Open files and devices

5
Threads
  • Processes do not share resources very well,
    therefore context switching cost is very high.
  • This leads to threads which share some of the
    resources
  • Thread is a light-weighted process and it is the
    basic unit of CPU utilization.
  • Thread comprises
  • Thread ID
  • Program counter
  • Register set
  • Stack space
  • Thread shares
  • Code section
  • Data section
  • OS resources such as open files, signals
    belonging to the task

6
Threads
  • System
  • Light-weighted threads switch between threads but
    not between virtual memories
  • Threads allow user programs to continue after
    starting I/O
  • Threads allow parallel processing
  • User
  • Threads may reduce context switching times by
    eliminating kernel overhead
  • Thread management allows user scheduling of
    threads

7
Threads Lightweight Processes
execution
Environment (resource)
  • (a) Three processes each with one thread
  • (b) One process with three threads

8
Thread Model
  • Threads in the same process share resources
  • Each thread execute separately

9
Thread Model Stack
10
Thread Model Context Switch
  • Extensive sharing makes CPU switching among peer
    threads and creation of threads inexpensive
    compared to processes
  • Thread context switch still requires
  • Register set switch
  • But no memory management related work!!!

11
Thread Model State
  • Threads states are
  • Ready
  • Blocked
  • Running
  • Terminated
  • Threads share CPU and on single processor machine
    only one thread can run at a time
  • Thread management can create child threads which
    can block waiting for a system call to be
    completed
  • No protection among threads!!

12
Example program
  • include ltphtread.hgt
  • include ltthread.hgt
  • include ltstdio.hgt
  • void threadex(void )
  • int main()
  • pthread_t tid / stores the new thread ID /
  • pthread_create(tid, NULL, threadex, NULL)
    /create a new thread/
  • pthread_join(tid, NULL) /main thread waits for
    other thread to terminate /
  • return 0 / main thread exits /
  • void threadex(void arg) /thread routine/
  • int i
  • for (i0 ilt5 i)
  • fprintf(stderr, Hello, world! \n'')
  • return NULL

13
Questions
  • What are the similarities between processes and
    threads?
  • What are the differences between processes and
    threads?

14
Real Life Example
  • Process
  • CS241 MP
  • Different from CS 225s MP
  • Thread
  • CS241 MP1, MP2, MP3, MP4, MP5
  • Each is different
  • Share
  • Textbook
  • Personnel (TAs, instructors)
  • Affect each other

15
Thread Usage word processor
  • What if it is single-threaded?

16
Thread Usage Web Server
17
Web Server
  • Rough outline of code for previous slide
  • (a) Dispatcher thread
  • (b) Worker thread

18
Tradeoffs
  • Three ways to construct a server

19
Benefits of Threads
  • Responsiveness
  • Multi-threading allows applications to run even
    if part of it is blocked
  • Resource sharing
  • Sharing of memory, files and other resources of
    the process to which the threads belong
  • Economy
  • Much more costly and time consuming to create and
    manage processes than threads
  • Utilization of multiprocessor architectures
  • Each thread can run in parallel on a different
    processor

20
Implementing Threads in User Space (old Linux)
  • A user-level threads package

21
User-level Threads
  • Advantages
  • Fast Context Switching
  • User level threads are implemented using user
    level thread libraries, rather than system calls,
    hence no call to OS and no interrupts to kernel
  • One key difference with processes when a thread
    is finished running for the moment, it can call
    thread_yield. This instruction (a) saves the
    thread information in the thread table itself,
    and (b) calls the thread scheduler to pick
    another thread to run.
  • The procedure that saves the local thread state
    and the scheduler are local procedures, hence no
    trap to kernel, no context switch, no memory
    switch, and this makes the thread scheduling very
    fast.
  • Customized Scheduling

22
User level Threads
  • Disadvantages
  • Blocking
  • If kernel is single threaded, then any user-level
    thread can block the entire task executing a
    single system call
  • No Protection
  • There is no protection between threads, since the
    threads share memory space

23
Implementing Threads in the Kernel (Windows
2000/XP)
  • A threads package managed by the kernel

24
Hybrid Implementations (Solaris)
  • Multiplexing user-level threads onto kernel-
    level threads

25
Kernel Threads (Linux)
  • Kernel threads differ from regular processes
  • Each kernel thread executes a single specific
    kernel C function
  • Regular process executes kernel function only
    through system calls
  • Kernel threads run only in Kernel Mode
  • Regular processes run alternatively in kernel
    mode and user mode
  • Kernel threads use smaller linear address space
    than regular processes

26
Multi-threading Models
  • Many-to-One Model many user threads are mapped
    to one kernel thread
  • Advantage
  • thread management is done in user space, so it is
    efficient
  • Disadvantage
  • Entire process will block if a thread makes a
    blocking call to the kernel
  • Because only one thread can access kernel at a
    time, no parallelism on multiprocessors is
    possible
  • One-to-One Model one user thread maps to kernel
    thread
  • Advantage
  • more concurrency than in many-to-one model
  • Multiple threads can run in parallel on
    multi-processors
  • Disadvantage
  • Creating a user thread requires creating the
    corresponding kernel thread. There is an overhead
    related with creating kernel thread which can be
    burden on the performance.

27
Multi-threading Models
  • Many-to-Many Model many user threads are
    multiplexed onto a smaller or equal set of
    kernel threads.
  • Advantage
  • Application can create as many user threads as
    wanted
  • Kernel threads run in parallel on multiprocessors
  • When a thread blocks, another thread can still
    run

28
A Challenge Making Single-Threaded Code
Multithreaded
  • Conflicts between threads over the use of a
    global variable

29
A solution Private Global Variables
30
Summary
  • What is thread? Why do we need threads?
  • Difference between process and thread
  • Thread Implementations and their tradeoffs
  • User-level
  • Kernel-level
  • Hybrid
  • Pthreads will be handled in discussion sections
    during the week of February 6
Write a Comment
User Comments (0)
About PowerShow.com