Pemrograman Paralel dengan PThread @2012,Eko Didik Widianto POSIX Thread API PThread Umpan Balik Lisensi Pemrograman Paralel dengan PThread Kuliah#5 TSK205 Sistem Digital - TA 2011/2012 Eko Didik Widianto Teknik Sistem Komputer - Universitas Diponegoro
39
Embed
Pemrograman Paralel @2012,Eko Didik Widianto …didik.blog.undip.ac.id/files/2012/02/TSK617-Kuliah5-Pemrograman_P... · Pemrograman Paralel dengan PThread @2012,Eko Didik Widianto
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX Thread
API PThread
Umpan Balik
LisensiPemrograman Paralel dengan PThreadKuliah#5 TSK205 Sistem Digital - TA 2011/2012
Eko Didik Widianto
Teknik Sistem Komputer - Universitas Diponegoro
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX Thread
API PThread
Umpan Balik
Lisensi
Tentang Kuliah
I Sebelumnya dibahas tentang:I Desain model pemrograman di atas mesin memori bersama
API PThreadAPI PThreadCreating and Terminating ThreadPassing Argumen ke ThreadJoining dan Detaching Thread
Umpan Balik
Lisensi
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX ThreadKonsep Thread
Tentang POSIX Threads
API PThread
Umpan Balik
Lisensi
Bahasan
POSIX ThreadKonsep ThreadTentang POSIX Threads
API PThreadAPI PThreadCreating and Terminating ThreadPassing Argumen ke ThreadJoining dan Detaching Thread
Umpan Balik
Lisensi
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX ThreadKonsep Thread
Tentang POSIX Threads
API PThread
Umpan Balik
Lisensi
Apa itu Thread?
I Secara teknis, sebuah thread didefinisikan sebagai satualiran instruksi yang independen yang dapat dijalankansecara terjadwal oleh OS
I Dapat dibayangkan seperti konsep "procedure" yang dapatberjalan independen dari program utamanya
I Seperti program utama (a.out) yang berisi sejumlahprosedur/fungsi. Kemudian semua prosedur dapat dijadwaluntuk dijalankan secara simultan dan/atau independen olehOS
I Ini menggambarkan program “multi-thread”I Bagaimana menjalankannya secara simultan?
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX ThreadKonsep Thread
Tentang POSIX Threads
API PThread
Umpan Balik
Lisensi
Process
I Sebuah proses dibuat oleh OS dan memerlukanoverhead untuk menyimpan informasi tentang resourcedan state eksekusi program
I Process ID, process group ID, user ID, and group IDI EnvironmentI Working directoryI Program instructionsI RegistersI StackI HeapI File descriptorsI Signal actionsI Shared librariesI Inter-process communication tools (such as message
queues, pipes, semaphores, or shared memory)
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX ThreadKonsep Thread
Tentang POSIX Threads
API PThread
Umpan Balik
Lisensi
UNIX Process
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX ThreadKonsep Thread
Tentang POSIX Threads
API PThread
Umpan Balik
Lisensi
Thread dalam Sebuah Proses UNIX
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX ThreadKonsep Thread
Tentang POSIX Threads
API PThread
Umpan Balik
Lisensi
ThreadI Exists within a process and uses the process resources
I Changes made by one thread to shared system resourceswill be seen by all other threads
I such as closing a fileI Two pointers having the same value point to the same dataI Reading and writing to the same memory locations is
possible, and therefore requires explicit synchronizationby the programmer
I Has its own independent flow of control as long as its parentprocess exists
I Stack pointer, registers, scheduling properties (such aspolicy or priority), set of pending and blocked signals, threadspecific data.
I Duplicates only the essential resources it needs to beindependently schedulable
I May share the process resources with other threads that actequally independently (and dependently)
I Dies if the parent process dies - or something similarI Is "lightweight" because most of the overhead has already been
accomplished through the creation of its process
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX ThreadKonsep Thread
Tentang POSIX Threads
API PThread
Umpan Balik
Lisensi
Bahasan
POSIX ThreadKonsep ThreadTentang POSIX Threads
API PThreadAPI PThreadCreating and Terminating ThreadPassing Argumen ke ThreadJoining dan Detaching Thread
Umpan Balik
Lisensi
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX ThreadKonsep Thread
Tentang POSIX Threads
API PThread
Umpan Balik
Lisensi
POSIX Thread
I POSIX: Portable Operating System Interface for UNIXI IEEE Std 1003.1c-1995 mendefinisikan API untuk membuat
dan memanipulasi threadI System call untuk membuat dan mensinkronkan threadI Implementasi API: FreeBSD, NetBSD, GNU/Linux, Mac OS
X dan SolarisI Windows? pthreads-w32 (third party)
I Support PThreadI Membuat program paralelI SinkronisasiI Memori shared implisit, pointer ke data shared diberikan ke
suatu thread
I Implementasi: pthread.h, libpthreadI Some useful links:
I standards.ieee.org/findstds/standard/1003.1-2008.htmlI www.opengroup.org/austin/papers/posix_faq.htmlI www.unix.org/version3/ieee_std.html
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX ThreadKonsep Thread
Tentang POSIX Threads
API PThread
Umpan Balik
Lisensi
Mengapa PThread?
I The primary motivation is to realize potential programperformance gains
I When compared to the cost of creating and managing aprocess, a thread consumes much less operating systemoverhead
I Managing threads requires fewer system resources thanmanaging processes
I Function:I fork() subroutine: creating a new processI pthread_create() subroutine: creating a new thread
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX ThreadKonsep Thread
Tentang POSIX Threads
API PThread
Umpan Balik
Lisensi
Fork vs ThreadI All threads within a process share the same address
spaceI Inter-thread communication is more efficient and easier to
use than inter-process communication
Timings reflect 50,000 process/thread creations
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX ThreadKonsep Thread
Tentang POSIX Threads
API PThread
Umpan Balik
Lisensi
Aplikasi Single-Thread vs Multi-Thread
I Offers potential performance gains and practicaladvantages
I Overlapping CPU work with I/OI For example, a program may have sections where it is
performing a long I/O operation. While one thread is waitingfor an I/O system call to complete, CPU intensive work can beperformed by other threads
I Priority/real-time scheduling: tasks which are moreimportant can be scheduled to supersede or interrupt lowerpriority tasks
I Asynchronous event handling: tasks which service events ofindeterminate frequency and duration can be interleaved
I For example, a web server can both transfer data fromprevious requests and manage the arrival of new requests
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX ThreadKonsep Thread
Tentang POSIX Threads
API PThread
Umpan Balik
Lisensi
Pthread di Komputer SMP
I To achieve optimum performanceI Performance of an application can be greatly improved by
using Pthread on-node data transfer/communication,instead of using MPI
I MPI libraries usually implement on-node task communicationvia shared memory, which involves at least one memory copyoperation (process to process)
I For Pthreads there is no intermediate memory copy requiredbecause threads share the same address space within asingle processIt becomes more of a cache-to-CPU or memory-to-CPUbandwidth (worst case) situation. These speeds are muchhigher
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX ThreadKonsep Thread
Tentang POSIX Threads
API PThread
Umpan Balik
Lisensi
Perbandingan Pthread vs MPI di KomputerSMP
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX ThreadKonsep Thread
Tentang POSIX Threads
API PThread
Umpan Balik
Lisensi
Desain Program MultithreadI On modern, multi-cpu machines, pthreads are ideally
suited for parallel programmingI whatever applies to parallel programming in general, applies
to parallel pthreads programsI In order for a program to take advantage of Pthreads, it
must be able to be organized into discrete, independenttasks which can execute concurrently
I Routines can be interchanged, interleaved and/oroverlapped in real time
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX ThreadKonsep Thread
Tentang POSIX Threads
API PThread
Umpan Balik
Lisensi
Program yang Akan Memerlukan Pthread
I Programs having the following characteristics may be wellsuited for pthreads:
I Work that can be executed, or data that can be operated on,by multiple tasks simultaneously
I Block for potentially long I/O waitsI Use many CPU cycles in some places but not othersI Must respond to asynchronous eventsI Some work is more important than other work (priority
interrupts)
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX ThreadKonsep Thread
Tentang POSIX Threads
API PThread
Umpan Balik
Lisensi
Model Program Multithread
I Manager/worker: a single thread, the manager assignswork to other threads, the workers
I Typically, the manager handles all input and parcels outwork to the other tasks.
I At least two forms of the manager/worker model arecommon: static worker pool and dynamic worker pool
I Pipeline: a task is broken into a series of suboperations,each of which is handled in series, but concurrently, by adifferent thread. An automobile assembly line bestdescribes this model.
I Peer: similar to the manager/worker model, but after themain thread creates other threads, it participates in thework
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX ThreadKonsep Thread
Tentang POSIX Threads
API PThread
Umpan Balik
Lisensi
Model Memori BersamaI All threads have access to the same global, shared
memoryI Threads also have their own private dataI Programmers are responsible for synchronizing access
(protecting) globally shared data
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX ThreadKonsep Thread
Tentang POSIX Threads
API PThread
Umpan Balik
Lisensi
Thread-safeness
I Thread-safeness: refers an application’s ability to executemultiple threads simultaneously without "clobbering"shared data or creating "race" conditions.
I If the routine does not employ synchronization constructs toprevent data corruption, then it is not thread-safe
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX Thread
API PThreadAPI PThread
Creating and TerminatingThread
Passing Argumen keThread
Joining dan DetachingThread
Umpan Balik
Lisensi
Bahasan
POSIX ThreadKonsep ThreadTentang POSIX Threads
API PThreadAPI PThreadCreating and Terminating ThreadPassing Argumen ke ThreadJoining dan Detaching Thread
Umpan Balik
Lisensi
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX Thread
API PThreadAPI PThread
Creating and TerminatingThread
Passing Argumen keThread
Joining dan DetachingThread
Umpan Balik
Lisensi
API PThreadI Pthreads API was defined in the ANSI/IEEE POSIX 1003.1 -
1995 standardI The subroutines which comprise the Pthreads API can be
informally grouped into:1. Thread management: Routines that work directly on
threads - creating, detaching, joining, etcI Also include functions to set/query thread attributes
(joinable, scheduling etc.)2. Mutexes: Routines that deal with synchronization, called a
"mutex", which is an abbreviation for "mutual exclusion"I Provide for creating, destroying, locking and unlocking
mutexesI Supplemented by mutex attribute functions that set or
modify attributes associated with mutexes3. Condition variables: Routines that address
communications between threads that share a mutex.I Based upon programmer specified conditionsI Includes functions to create, destroy, wait and signal
based upon specified variable values. Functions toset/query condition variable attributes are also included
4. Synchronization: Routines that manage read/write locksand barriers
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX Thread
API PThreadAPI PThread
Creating and TerminatingThread
Passing Argumen keThread
Joining dan DetachingThread
Umpan Balik
Lisensi
API PThread
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX Thread
API PThreadAPI PThread
Creating and TerminatingThread
Passing Argumen keThread
Joining dan DetachingThread
Umpan Balik
Lisensi
Kompilasi dan Linking
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX Thread
API PThreadAPI PThread
Creating and TerminatingThread
Passing Argumen keThread
Joining dan DetachingThread
Umpan Balik
Lisensi
Bahasan
POSIX ThreadKonsep ThreadTentang POSIX Threads
API PThreadAPI PThreadCreating and Terminating ThreadPassing Argumen ke ThreadJoining dan Detaching Thread
Membuat ThreadRutin: pthread_create (thread,attr,start_routine,arg)
I Argument:I thread: An opaque, unique identifier for the new thread
returned by the subroutineI attr: An opaque attribute object that may be used to set
thread attributes. You can specify a thread attributes object,or NULL for the default values
I start_routine: the C routine that the thread will execute onceit is created
I arg: A single argument that may be passed to start_routine.It must be passed by reference as a pointer cast of typevoid. NULL may be used if no argument is to be passed
I Return: thread_id
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX Thread
API PThreadAPI PThread
Creating and TerminatingThread
Passing Argumen keThread
Joining dan DetachingThread
Umpan Balik
Lisensi
Membuat Thread
I Once created, threads are peers, and may create otherthreads
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX Thread
API PThreadAPI PThread
Creating and TerminatingThread
Passing Argumen keThread
Joining dan DetachingThread
Umpan Balik
Lisensi
Atribut Thread
I Can be set when creating a threadI Or initialize thread attribute in main thread:
pthread_attr_init and pthread_attr_destroyI Attributes:
I A thread may be terminated:I The thread returns normally from its starting routine. It’s
work is doneI The thread makes a call to the pthread_exit subroutine -
whether its work is done or notI The thread is canceled by another thread via the
pthread_cancel routineI The entire process is terminated due to making a call to
either the exec() or exit()I If main() finishes first, without calling pthread_exit explicitly
itself
I Optional termination status can be specifiedI Typically returned to threads joining the terminated thread
I Cleanup: the pthread_exit() doesnot close filesI Any files opened inside the thread will remain open after the
thread is terminated
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX Thread
API PThreadAPI PThread
Creating and TerminatingThread
Passing Argumen keThread
Joining dan DetachingThread
Umpan Balik
Lisensi
Contoh Kode: Pembuatan dan TerminasiThread
See code
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX Thread
API PThreadAPI PThread
Creating and TerminatingThread
Passing Argumen keThread
Joining dan DetachingThread
Umpan Balik
Lisensi
Bahasan
POSIX ThreadKonsep ThreadTentang POSIX Threads
API PThreadAPI PThreadCreating and Terminating ThreadPassing Argumen ke ThreadJoining dan Detaching Thread
Umpan Balik
Lisensi
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX Thread
API PThreadAPI PThread
Creating and TerminatingThread
Passing Argumen keThread
Joining dan DetachingThread
Umpan Balik
Lisensi
Passing Argumen ke Thread
See code
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX Thread
API PThreadAPI PThread
Creating and TerminatingThread
Passing Argumen keThread
Joining dan DetachingThread
Umpan Balik
Lisensi
Bahasan
POSIX ThreadKonsep ThreadTentang POSIX Threads
API PThreadAPI PThreadCreating and Terminating ThreadPassing Argumen ke ThreadJoining dan Detaching Thread
Umpan Balik
Lisensi
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX Thread
API PThreadAPI PThread
Creating and TerminatingThread
Passing Argumen keThread
Joining dan DetachingThread
Umpan Balik
Lisensi
Joining dan Detaching Thread
See code
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX Thread
API PThread
Umpan Balik
Lisensi
Umpan Balik
I Yang telah kita pelajari hari ini:I tbd
I Yang akan kita pelajari di pertemuan berikutnya adalah<tbd>
I Pelajari: tbd
Pemrograman Paraleldengan PThread
@2012,Eko DidikWidianto
POSIX Thread
API PThread
Umpan Balik
Lisensi
Lisensi
Creative Common Attribution-ShareAlike 3.0 Unported (CCBY-SA 3.0)
I Anda bebas:I untuk Membagikan — untuk menyalin, mendistribusikan,
dan menyebarkan karya, danI untuk Remix — untuk mengadaptasikan karya
I Di bawah persyaratan berikut:I Atribusi — Anda harus memberikan atribusi karya sesuai
dengan cara-cara yang diminta oleh pembuat karyatersebut atau pihak yang mengeluarkan lisensi.
I Pembagian Serupa — Jika Anda mengubah, menambah,atau membuat karya lain menggunakan karya ini, Andahanya boleh menyebarkan karya tersebut hanya denganlisensi yang sama, serupa, atau kompatibel.
I Lihat: Creative Commons Attribution-ShareAlike 3.0Unported License