Docsity

Operating Systems Lecture: Threading Systems and Inter-Thread Communication, Study notes of Computer Science

Partial preview of the text

Download Operating Systems Lecture: Threading Systems and Inter-Thread Communication and more Study notes Computer Science in PDF only on Docsity!

Operating SystemsOperating Systems

CMPSC 473 CMPSC 473

Operating Systems Structure Operating Systems Structure

February 7, 2008 - Lecture February 7, 2008 - Lecture 77

Instructor: Trent Jaeger Instructor: Trent Jaeger

  • Last class:
    • Thread Background
  • Today:
    • Thread Systems

What kind of problems would

you solve with threads?

  • Imagine you are building a web server
    • You could allocate a pool of threads, one for each client
      • Thread would wait for a request, get content file, return it
    • How would the different thread models impact this?
  • Imagine you are building a web browser
    • You could allocate a pool of threads
      • Individual threads would retrieve web content from the myriad of sites
      • What happens if the user decided to stop the request?
        • Mouse click on the stop button
    • How would the different thread models impact this?

Linux Threads

  • Linux uses a one-to-one thread model
    • Threads are calls tasks
  • Linux views threads are “contexts of execution”
    • Threads are defined separately from processes
    • I.e., a thread is assigned an address space

Linux Threads

  • Linux system call
    • clone( int (*fn)(), void *stack, int flags, int argc, … /args */ )
    • Create a new thread (Linux task)
  • Clone( start , stack , 0, 1)
    • What are start and stack for?
  • So, clone provides significant flexibility over fork
    • Compare to fork variants in Solaris
  • Example: http://tldp.org/FAQ/Threads-FAQ/clone.c

POSIX Threads

  • POSIX Threads or Pthreads is a thread API

specification

  • Not an implementation
  • Could be mapped to libraries or system calls
  • Supported by Solaris and Linux

POSIX Threads

  • pthread_self()
    • return thread ID
  • pthread_equal()
    • for comparisons of thread ID's
  • pthread_exit()
    • or just return from the start function
  • pthread_join()
    • wait for another thread to terminate retrieve value from pthread_exit()
  • pthread_cancel()
    • terminate a thread, by TID
  • pthread_detach()
    • thread is immune to join or cancel runs independently until it terminates
  • pthread_attr_init()
    • thread attribute modifiers

POSIX Threads

http://www.cse.psu.edu/~dheller/cse411/programs/thread_sample.c

Inter-Thread Communication

  • Can you use shared memory?
    • Already have it
    • Just need to allocate memory in the address space
      • No need for shm
      • Programming to pipes provides abstraction
  • Can you use message passing?
    • Sure
    • Would have to build infrastructure
  • An advantage of using kernel threads (e.g., Linux) is that system IPC can be used between threads - Would want optimized paths for common address space

Threading Issues

Thread Cancellation

  • So, you want to stop a thread from executing
    • Don’t need it anymore
      • Remember the browser ‘stop’ example
  • Two choices
    • Synchronous cancellation
      • Wait for the thread to reach a point where cancellation is permitted
      • No such operation in Pthreads, but can create your own
    • Asynchronous cancellation
      • Terminate it now
      • pthread_cancel (thread_id)

Signal Handling

  • What’s a signal?
    • A form of IPC
    • Send a particular signal to another process
  • The receiver’s signal handler processes the signal on receipt
  • Example
    • Tell the Internet daemon ( inetd ) to reread its config file
    • Send signal to inetd : kill -SIGHUP
    • inetd ’s signal handler for the SIGHUP signal re-reads the config file
  • Note: some signals cannot be handled by the receiving process, so they cause default action (kill the process)

Signal Handling and Threads

  • So, you send a signal to a process
    • Which thread should it be delivered to?
  • Choices
    • Thread to which the signal applies
    • Every thread in the process
    • Certain threads in the process
    • A specific signal receiving thread

• It depends…

Signal Handling and Threads

  • Synchronous vs. Asynchronous Cases
  • Synchronous
    • Signal is delivered to the same process that caused the signal
    • Which thread(s) would you deliver the signal to?
  • Asynchonous
    • Signal generated by another process
    • Which thread(s) in this case?