1 / 88

Seminar Shared memory Programming

Seminar Shared memory Programming. Week 7 _ 2. Nguyễn Thị Xuân Mai 50501620 Phạm Thị Trúc Linh 50501491 Vũ Thị Mai Phương 50502194 Nguyễn Minh Quân 50502771 Phí Văn Tuấn 50503347. Programming with shared memory. Creating Concurrent Processes. 2. Threads. 1.

Télécharger la présentation

Seminar Shared memory Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SeminarShared memory Programming Week 7 _ 2 Nguyễn Thị Xuân Mai 50501620 Phạm Thị Trúc Linh 50501491 Vũ Thị Mai Phương 50502194 Nguyễn Minh Quân 50502771 Phí Văn Tuấn 50503347

  2. Programming with shared memory Creating Concurrent Processes 2 Threads 1 Shared memory multiprocessor 2 Constructs for specifying parallelism

  3. Programming with shared memory Creating Shared Data Accessing Shared Data Language Constructs for Parallelism Dependency Analysis 4 Shared Data in system with caches 3 Sharing data Locks, Deadlock, Semaphores, Monitor, Condition Variables

  4. Shared Memory Multiprocessors • In a shared memory system, any memory location can be accessible by any of the processors. • A single address space exists, meaning that each memory location is given a unique address within a single range of addresses • Shared-memory behavior is determined by both program order and memory access order Shared memory multiprocessor

  5. Shared Memory Multiprocessors • For a small number of processors, a common architecture is the single bus architecture, in which all processors and memory modules attach to the same set of wires(the bus)‏ Shared memory multiprocessor

  6. Shared Memory Multiprocessors (a) A uniprocessor system (b) A multiprocessor system Shared memory multiprocessor

  7. Creating Concurrent Processes 1 2 Threads Constructs for Specifying Parallelism Constructs for specifying Parallelism

  8. Creating Concurrent Proceses • A structure to specifying concurrent processes is the FORK – JOIN group statements • A FORK statement generates one new path for a concurrent process and the concurrent process use JOIN statements at their ends • When JOIN statements have been reached, processing continues in a sequential fashion Constructs for specifying Parallelism

  9. Creating Concurrent Proceses(cont)‏ Constructs for specifying Parallelism

  10. UNIX Heavyweight Processes • Operating systems such as UNIX are based upon the notion of a process. • On a single processor system, the processor has to be time shared between processes, switching from one process to another • Time sharing also offer the opportunity to deschedule processes that are blocked from proceeding for some reason, such as waiting for an I/O operation to complete • On a multiprocessors, there is an opportunity to execute process truly concurrently Constructs for specifying Parallelism

  11. UNIX Heavyweight Processes • The UNIX system call fork() creates a new process. The new process (child process) is an exact copy of the calling process except that it has a unique process ID • On success, fork() returns 0 to the child process ang returns the process ID of the child process to the parent process • Process are “joined” with the system calls wait() and exit() defined as wait(statusp);/*delays caller until signal received or one of its*/ …child process terminates or stop… exit(status);/*terminates a process*/ Constructs for specifying Parallelism

  12. UNIX Heavyweight Processes • Hence, a single child process can be created by pid = fork() /* fork */ … Code to be excuted by both child and parent… if (pid == 0) exit(0); else wait(0); /* join */ Constructs for specifying Parallelism

  13. UNIX Heavyweight Processes • If the child is to execute different code, we could use pid = fork() ; if (pid == 0) { … code to be executed by slave … } else { … code to be executed by parent … } if (pid == 0) exit (0); else wait (0); Constructs for specifying Parallelism

  14. UNIX Heavyweight Processes • All variables in the original program are duplicated in each process, becoming local variables for the process. They are assigned the same values as the original variables initially • The parent will wait for the slave to finish if it reaches the “join” point first, if the slave reaches the “join” point first, it will terminate Constructs for specifying Parallelism

  15. Threads • Thread: • a thread of execution is a fork of a computer program into two or more concurrently running tasks. • Thread mechanism • Allow to share the same memory space & global variables. Constructs for specifying Parallelism

  16. Heap Code Heap Code Stack IP IP Stack Interupt Routines Stack IP Interupt Routines File File Processes & Threads Dependence • processes are typically independent, while threads exist as subsets of a process • processes carry considerable state information, where multiple threads within a process share state as well as memory and other resources. State Infomation Address Space • processes have separate address spaces, where threads share their address space. • processes interact only through system-provided inter-process communication mechanisms, while threads interact through shared variable or by message passing. Interaction Context Switching • Context switching between threads in the same process is typically faster than context switching between processes . Process Thread Constructs for specifying Parallelism

  17. Multithreaded Processor Model • Analyze performance of system • Latency L: communication latency experienced with remote memory access. network delay, cache-miss penalty, delays caused by contentions in split transactions • Number of threads N: Number of thread that can be interleaved in a processor. Context of a thread ={PC,register set, required context status word …} • Context switch overhead C: time lost in performing context switch in a processor. Switch mechanism, number of processor states needed to maintain active threads • Interval between context switches: run length (cycles between context switch triggered by remote reference)‏ www.themegallery.com Company Logo

  18. Computation ??? Variable ??? Thread of Parallel Computation Initial Scheduling overhead Thread Synchronization overhead Multithreaded Computation The concept of multithreading in MPP system • Processor efficiency • Busy: do useful work • Context switch: suspend current context & switch to another • Idle: when all availble context suspended (blocked) Efficient = Busy/ (busy + switching + idle)‏

  19. 1 Thread / context Register Files PC PSW PC N Contexts PSW PC PSW ALU Local memory reference Remote memory reference Abtract Processor Model Multiple-context processor model with one thread per context www.themegallery.com Company Logo

  20. Context-switching policies • Switch on cache miss: when encoutering a cache miss • Switch on every load: switching on every load operation, independent of whether it will cause a miss or not. • Switch on every instruction: switching on every instruction, insependent of whether or not it is a load. • Switch on block of instruction: will improve the cache-hit ratio, due to preservation of some locality & also benefit singe-context performance. www.themegallery.com Company Logo

  21. Pthread Thread • History: SUN, Solaris, Window NT … are examples the multithreaded operating systems, allow users to employ threads in their programs,but each system is different. IEEE POSIX 1003.1c standard (1995). Standard Constructs for specifying Parallelism

  22. Executing a Pthread Thread • Pthread_t thread; • Hanndle of specia Pthread datatype. • int pthread_create(pthread_t * thread, pthread_attr_t * attr, • void * (*start_routine)(void *), void * arg); • Return value of function: • 􀁺success, create a new thread: 0, arg contain thread ID • 􀁺fail: <> 0 (error signature is contain in the variable of errno)‏ • Arguments: • thread: a pointer of type pthread_t, contain ID of new thread. • 􀁺attr: contain initial attr for thread, if attr = NULL Attr are init by default value. • 􀁺start_routine: la reference to a function defined by user. This function contain execute code for new thread. • 􀁺arg: a single argument is passed for start_routine.

  23. Executing a Pthread Thread(cont)‏ • int pthread_join(pthread_t th, void **thread_return); • pthread_join() force call thread suspend its execution & wait until thread having thread ID terminates. Thread_return contain the return value (value of return statement or pthread_exit(…) statement)‏ • pthread_exit(void *status); • Terminate & destroy a thread. • pthread_cancel(); • Thread is destroyed by another process.

  24. Main program Pthread_create(); Thread Pthread_create(); Thread Pthread_create(); Thread Termination Termination Termination Detached Thread • There are cases in which threads can be terminated without needed of pthread_join Detached Thread • When Detached Thread teminate, they are destroyed & their resource released => More efficient Constructs for specifying Parallelism

  25. Thread Pools • A master thread could control a collection of slave threads. A work pool of threads could be formed. Threads can communicate through shared location or, as we shall see =, using signals. Constructs for specifying Parallelism

  26. Thread – safe routines • System calls or library routines are called thread safe if they can be called from multiple threads simultaneously and always produce correct results. • Thread – safeness: an application's ability to execute multiple threads simultaneously without "clobbering" shared data or creating "race" conditions. Constructs for specifying Parallelism

  27. Thread – safe routines (cont)‏ • Suppose that your application creates several threads, each of which makes a call to the same library routine: • This library routine accesses/modifies a global structure or location in memory. • As each thread calls this routine it is possible that they may try to modify this global structure/memory location at the same time. • If the routine does not employ some sort of synchronization constructs to prevent data corruption, then it is not thread-safe. • fe Constructs for specifying Parallelism

  28. Creating Shared Data 1 Accessing Shared Data 2 3 Language Constructs for Parallelism Dependency Analysis 4 5 Shared Data in system with caches Sharing Data Locks Deadlock Semaphores Deadlock Condition Variables

  29. Conditions for Deadlock 1. Mutual exclusion • Resource can not be shared • Requests are delayed until resource is released 2. Hold-and-wait • Thread holds one resource while waits for another 3 No preemption • Resources are released voluntarily after completion 4. Circular wait • Circular dependencies exist in “waits-for” or “resource- allocation” graphs ALL four conditions MUST hold Text in here Text in here Sharing Data

  30. Handing Deadlock • Deadlock prevention • Deadlock avoidance • Deadlock detection and recovery • Ignore Text Text Text Txt www.themegallery.com Company Logo

  31. Deadlock Example R1,R2,…,Rn là tài nguyên P1,P2,…,Pn là quá trình 8.8b : n-processes deadlock 8.8a : 2 processes dealock www.themegallery.com Company Logo

  32. Semaphore

  33. sEMaPHoRE • A positive integer operated upon by 2 operations: P & V. • The value is the number of the units of the resource which are free. • A binary semaphore has value 0 or 1. • A general semaphore can take on positive values other than 0 and 1. www.themegallery.com Company Logo

  34. sEMaPHoRE • P & V operations are performed indivisibly. • P(s): waits until s is greater than 0 then, decrements s by 1, allows the process to continue. • V(s): increments s by 1 to release one of the waiting processes (if any). www.themegallery.com Company Logo

  35. sEMaPHoRE • The first process reach its P(s) operation , or to be accepted will set the semaphore to 0. • Processes delayed by P(s) are kept in abeyance until released by a V(s) on the same semaphore. www.themegallery.com Company Logo

  36. sEMaPHoRE • When the process reaches its V(s) operation, it sets the semaphore s to 1 and one of the processes waiting is allowed to proceed into the critical section. www.themegallery.com Company Logo

  37. Monitor Disadvange of Semaphore: • Although semaphore provide a convenient and effective mechanism for process synchronization, using them incorrectly can result in timing error that are difficult to detect (deadlock), since these errors happen only if some particular execution sequences take place and these sequences do not always occur • Using incorrectly may be caused by an honest programming error or an uncooperative programmer Shared memory multiprocessor

  38. Monitor Example wrong Semaphore Wrong code … Signal(mutex); //critical code Wait(mutex);… Đoạn mã sai này gây ra vi phạm điều kiện loại trừ lẫn nhau Right code … Wait(mutex); //critical section Signal(mutex); … www.themegallery.com Company Logo

  39. Monitor Example wrong Semaphore Wrong code … wait(mutex); //critical code Wait(mutex); … Đoạn mã sai này gây ra bế tắc Right code … Wait(mutex); //critical section Signal(mutex); … www.themegallery.com Company Logo

  40. Monitor • If programmer omits the wait() or the signal() in critical section, or both, either mutual exclusion is violated or a deadlock will occur • Both processes are simultaneously active will cause a deadlock Process P1 … Wait(S); Wait(Q); //critical section Signal(S); Signal(Q); Process P2 … Wait(S); Wait(Q); //critical section Signal(S); Signal(Q); Khoa Khoa Học & Kĩ thuật máy tính - Đại học Bách Khoa TpHCM

  41. Monitor • To deal with such errors, researches have developed high-level language constructs, one fundamental high-level synchronization construct – the monitor type • Monitor is an approach to synchronize operations on computer when use shared source. Monitor includes: • A suit of procedure that provides the only method to access a shared resource • Key eliminate together • Variables correlative shared source • Some unchanged assume to avoid events conflict Khoa Khoa Học & Kĩ thuật máy tính - Đại học Bách Khoa TpHCM

  42. Monitor • A type, or abstract data type, encapsulates private data with public method to operate on that data • Monitor type presents a set of programmer-defined operations that are provided mutual exclusion within monitor • The monitor type also contains the declaration of variables whose values define the state of an instance of that type, along with the bodies of procedures or function that operate on those variables Khoa Khoa Học & Kĩ thuật máy tính - Đại học Bách Khoa TpHCM

  43. Monitor • A structure of monitor type: monitor monitor_name { // shared variable declaration procedure P1(...) { ... } procedure P2(...) { ... } ... procedure Pn(...) { ... } initialization_code (..) { ... } } Khoa Khoa Học & Kĩ thuật máy tính - Đại học Bách Khoa TpHCM

  44. Structure Monitor Khoa Khoa Học & Kĩ thuật máy tính - Đại học Bách Khoa TpHCM

  45. Usage Monitor • The monitor construct ensure that only one process at a time can be active within the monitor. Consequently, the programmer does not need to code this synchronization constraint explicitly • Monitor, as defined so far, is not sufficiently powerful for modeling some synchronization scheme. Need to define additional some machanisms “tailor mode” about synchronization • Some addiontional synchronization “tailor mode”: use conditional construct Khoa Khoa Học & Kĩ thuật máy tính - Đại học Bách Khoa TpHCM

  46. Conditional type • Declare: conditional x,y; • Only operations that can be invoke on a conditional variable are wait() and signal()‏ x.wait(): the process invoking this operation is suspended until another process invokes x.signal():the process invoking this operation resumes exatly one suspended process • if no process is suspended, x.signal() has no effect Khoa Khoa Học & Kĩ thuật máy tính - Đại học Bách Khoa TpHCM

  47. Structure Monitor conditional type Khoa Khoa Học & Kĩ thuật máy tính - Đại học Bách Khoa TpHCM

  48. Condition variables • Condition variables • Allow threads to synchronize based upon the actual value of data. • Without condition variables, the programmer would need to have threads continually polling (possibly in a critical section), to check if the condition is met. This can be very time consuming & unproductive execise since the thread would be continuously busy in this activity. • Always used in conjunction with a mutex lock. Sharing Data

  49. Pthread Condition Variables • Pthread_cond_t cond ; • Declare condition variable. • int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr); • initialises the condition variable referenced by cond with attributes referenced by attr. If attr is NULL, the default condition variable attributes are used; the effect is the same as passing the address of a default condition variable attributes object. Upon successful initialisation, the state of the condition variable becomes initialised. • int pthread_cond_signal(pthread_cond_t *cond); • call unblocks at least one of the threads that are blocked on the specified condition variable cond (if any threads are blocked on cond). Sharing Data

  50. Pthread Condition Variables(cont)‏ • int pthread_cond_broadcast(pthread_cond_t *cond); • call unblocks all threads currently blocked on the specified condition variable cond. • int pthread_cond_destroy(pthread_cond_t *cond); • destroys the given condition variable specified by cond; the object becomes, in effect, uninitialised. An implementation may cause pthread_cond_destroy() to set the object referenced by cond to an invalid value. A destroyed condition variable object can be re-initialised using pthread_cond_init(); the results of otherwise referencing the object after it has been destroyed are undefined. • int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); • int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime); • block on a condition variable, the second one alow to appoint timeout Sharing Data

More Related