/
Posix  Threads (Pthreads) Posix  Threads (Pthreads)

Posix Threads (Pthreads) - PowerPoint Presentation

cheryl-pisano
cheryl-pisano . @cheryl-pisano
Follow
342 views
Uploaded On 2019-11-19

Posix Threads (Pthreads) - PPT Presentation

Posix Threads Pthreads Concurrency Chapter 26 Abstraction Threads Here we introduce a new abstraction for a single running process that of a thread The state of a single thread is thus very similar to that of a process It has a program counter PC that tracks where the program is fetching ID: 765513

thread threads concurrency data threads thread data concurrency pthreads parallelism work shared cpu program tasks pthread process single memory

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Posix Threads (Pthreads)" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Posix Threads (Pthreads) Concurrency Chapter 26

Abstraction: Threads Here we introduce a new abstraction for a single running process: that of a thread. The state of a single thread is thus very similar to that of a process. It has a program counter (PC) that tracks where the program is fetching instructions from Each thread has its own private set of registers it uses for computation. Suppose there are two threads that are running on a single processor, when switching from running one (T1) to running the other (T2), a context switch must take place. Instead of PCBs, we now have TCBs. When we perform a context switch the address space remains the same .

Thread Illustration Instead of a single stack in the address space, there wil lbe one per thread. Let’s say we have a multi-threaded process that has two threads in it; the resulting address space looks different (Figure 26.1, right).

Concurrency and Parallelism Concurrency is the ability of two or more threads to execute in overlapping time periods. Parallelism is the ability to execute two or more threads simultaneously. Concurrency can occur without parallelism. Parallelism is a specific form of concurrency requiring multiple processors.

Pthreads Overview They are light weight… When compared to the cost of creating and managing a process, a thread can be created with much less operating system overhead. Managing threads requires fewer system resources than managing processes.

Efficient Communications/Data Exchange For Pthreads there is no intermediate memory copy required because threads share the same address space within a single process. There is no data transfer, per se. It can be as efficient as simply passing a pointer. . In the worst case scenario, Pthread communications become more of a cache-to-CPU or memory-to-CPU bandwidth issue.

More performance facts Threaded applications offer potential performance gains and practical advantages over non-threaded applications in several other ways: Overlapping CPU work with I/O: For example, a program may have sections where it is performing a long I/O operation. While one thread is waiting for an I/O system call to complete, CPU intensive work can be performed by other threads. Priority/real-time scheduling: tasks which are more important can be scheduled to supersede or interrupt lower priority tasks. Asynchronous event handling: tasks which service events of indeterminate frequency and duration can be interleaved. For example, a web server can both transfer data from previous requests and manage the arrival of new requests.

Pthread Parallelism In order for a program to take advantage of Pthreads, it must be able to be organized into discrete, independent tasks which can execute concurrently.

Types of programs well-suited for Pthreads Programs having the following characteristics may be well suited for pthreads: Work that can be executed, or data that can be operated on, by multiple tasks simultaneously: Block for potentially long I/O waits Use many CPU cycles in some places but not others Must respond to asynchronous events Some work is more important than other work (priority interrupts).

Shared Memory Model: All threads have access to the same global, shared memory Threads also have their own private data Programmers are responsible for synchronizing access (protecting) globally shared data

Race Conditions (Thread Safeness) A race condition is a situation in which the unsynchronized access of shared resource by two or more threads leads to erroneous program behavior. The shared resource can be anything: hardware, kernel resource, data in memory. The window in which a race can occur is called a critical section (region) .

Pthread Examples… Sample pthread code can be found in the course web site.

Reading material Chapter 26 “Three Easy Pieces” Concurrency and Parallelism (Web Site) Pthread tutorial LLNL