13
Threads Chapter 4

Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place (address space + resources) –Thread is a program

  • View
    225

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place  (address space + resources) –Thread is a program

Threads

Chapter 4

Page 2: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place  (address space + resources) –Thread is a program

Modern Process & Thread

– Process is an infrastructure in which execution takes place (address space + resources)

– Thread is a program in execution within a process context – each thread has its own execution stack

DataData

ProcessProcess

Stac

k

ProgramProgram

Operating SystemOperating System

Thread

Thread

Thread

Stac

k

Stac

k

Page 3: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place  (address space + resources) –Thread is a program

MS-DOS

Some variants of UNIX

Multithreaded Approaches

Windows, Solaris, modern versions of UNIX

Single-threaded approaches

JAVA run-time environment

Page 4: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place  (address space + resources) –Thread is a program

Thread (Lightweight Process) Has an execution state (running, ready, etc.) State is saved when not running Has an execution stack Some per-thread static storage for local variables Access to the memory and resources of its process

all threads of a process share this

Page 5: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place  (address space + resources) –Thread is a program

Benefits of Threads

Takes less time to create a new thread than a process

Less time to terminate a thread than a process

Less time to switch between two threads within the same process

Since threads within the same process share memory and files, they can communicate with each other without invoking the kernel

Shared Address Space All threads in a process implicitly use that process’s address

space

Threads can share a program and data• If you want sharing, encode your work as threads in a process• Threads in separate processes can not facilitate sharing

Page 6: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place  (address space + resources) –Thread is a program

Thread ExamplesForeground to background work

(spreadsheet: one thread reading user input, another executing the commands and updating the spreadsheet; yet another making periodic backups)

Asynchronous processing(ex: a thread performing periodic backups against power failures in a word-processor)

Fast execution(on a Multiprocessor system, multiple threads can execute in parallel)

Modular program structure(different tasks/activities in a program may be implemented using different threads)

Client/Server computing

Page 7: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place  (address space + resources) –Thread is a program

User-Level Threads (ULT) All thread management is done by the application

The kernel is not aware of the existence of threads and schedules the process as a unit and assigns a single execution state (Ready, Running, Blocked, etc.)

Any application can be programmed to be multithreaded by using a threads library which contains code for creating and destroying threads, for passing messages and data between threads, for scheduling thread execution, and for saving and restoring thread contexts.

Disadvantages: when a ULT executes a blocking system call, all of the

threads within the same process are blocked can not take advantage of multiprocessing

Page 8: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place  (address space + resources) –Thread is a program

Kernel-Level Threads (KLT)

W2K, Linux, and OS/2 are examples of this approach

All of the work of thread management is done by the kernel. i.e. no thread management code in the program.

Kernel maintains context information for the process as a whole and for individual threads within the process

Scheduling is done by the kernel on a thread basis

Advantages: Kernel can simultaneously schedule multiple threads from

the same process on multiple processors If one thread in a process is blocked, the kernel can

schedule another thread of the same process

Disadvantage: transfer of control from one thread to the next in the same process requires a mode switch to the kernel

Page 9: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place  (address space + resources) –Thread is a program

Combined Approaches

Example: SUN Solaris

Thread creation, destruction, bulk of scheduling and synchronization are done in the user space.

multiple threads within the same application can run in parallel on multiple processors

if one is blocked, another thread within the same application need not block

Page 10: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place  (address space + resources) –Thread is a program

POSIX Thread Library (Linux)

http://web.mst.edu/~ercal/284/ThreadExamples/thread_handout.doc

Thread creation pthread_create()

Synchronization pthread_mutex_lock(), pthread_mutex_unlock(), pthread_mutex_trylock()

Suspension pthread_cond_wait(), pthread_cond_signal(), pthread_join()

Yielding pthread_yield()

Termination pthread_exit()

Page 11: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place  (address space + resources) –Thread is a program
Page 12: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place  (address space + resources) –Thread is a program

Symmetric Multiprocessing Kernel can execute on any processor

Typically each processor does self-scheduling from the pool of available processes or threads

Page 13: Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place  (address space + resources) –Thread is a program

Multiprocessor Operating System Design Considerations

Concurrent processes or threads

Synchronization

Scheduling

Memory Management

Reliability and Fault Tolerance