Process Synchronization and Memory Management

21 2567 n.w
1 / 35
Embed
Share

Explore the concepts of process synchronization, buffer management, producer-consumer problem, race conditions, critical sections, cache coherence, memory barriers, and solutions like Peterson's algorithm. Learn about managing memory in a multithreaded environment efficiently.

  • Multithreading
  • Synchronization
  • Memory Management
  • Concurrency
  • Algorithms

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. 21 2567 Process Synchronization Buffer Producer Consumer buffer(memory) / Example Producer Consumer Linux Command 1 tar gzip tar cvf - . | gzip > target.tar.gz 2 cat grep cat dictionary | grep board memory memory

  2. Circular Queue A D in 1 out 2 C 0 3 4 7 6 5 count = 3 BUFFER_SIZE = 8

  3. Producer & Consumer problem Producer: while ( produce ) { while (count == BUFFER_SIZE) { } buffer[in] = nextProduced in = (in + 1) % BUFFER_SIZE count++ } Consumer: while ( consume ) { while (count == 0) nextConsumed = buffer[out] out = (out + 1) % BUFFER_SIZE count -- }

  4. Race Condition count++ count-- instr 1: register1 = count instr 2: register1 = register1 + 1 instr 3: count = register1 instr 1: register2 = count instr 2: register2 = register2 1 instr 3: count = register2 Let count = 5 Executing (single core) 1 2 1 2 3 3 results in count = 4

  5. Critical Section A solution to the critical-section problem must satisfy: entry section code critical section critical section 1. Mutual exclusion process/thread critical section 1 2. Progress process/thread critical section deadlock 3. Bounded waiting critical section exit section remainder section

  6. https://www.geeksforgeeks.org/cache-coherence/

  7. Memory Barriers For most purposes, the C# lock statement, the Visual Basic SyncLock statement, or the Monitor class provide easier ways to synchronize data.

  8. reorder 2 thread1 print 0 print 100 MemoryBarrier() ? Thread 1 (print) 100

  9. Petersons Solution int bool turn; flag[2]; // flag[i] = true, process i is ready to enter its CS. Process 0 // turn = 0, 1 process that is allowed to execute in its CS. Process 1 do { do { turn cs P0 CS flag[0] = TRUE; turn = 1; while (flag[1] && turn == 1); flag[1] = TRUE; turn = 0; while (flag[0] && turn == 0); P1 critical section critical section flag[0] = FALSE flag[1] = FALSE } while (TRUE); remainder section } while (TRUE); remainder section Mutual exclusion, progress, bounded waiting are satisfied. 2 process

  10. P0 P1 CS turn = 1 turn = 0

  11. Sync. HW: TestAndSet bool TestAndSet(bool *target) { bool rv = *target; *target = TRUE; return rv; } // atomic instruction // rv return value (object) pass by reference Shared variable lock = FALSE; do { do { while (TestAndSet(&lock)); while (TestAndSet(&lock)); // critical section // critical section lock = FALSE; lock = FALSE; // remainder section // remainder section } while (TRUE); } while (TRUE); Not satisfy bounded-waiting requirement!

  12. textbook Bounded-waiting mutual exclusion process waiting[i] = false lock = false do { waiting[i] = TRUE; key = TRUE; while (waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; CS process i CS process CS // critical section process CS ( ) j = (i + 1) % n; while ((j != i) && !waiting[j]) j = (j + 1) % n; process CS } while (TRUE); if (j == i) else lock = FALSE; process j CS unlock CS waiting[j] = FALSE; CS process // remainder section

  13. textbook Sync. HW: Swap void Swap(bool *a, bool *b) { bool tmp = *a; *a = *b; *b = tmp; } lock = FALSE; // atomic instruction Shared variable do { do { key = TRUE; while (key == TRUE) Swap(&lock, &key); key = TRUE; while (key == TRUE) Swap(&lock, &key); // critical section // critical section lock = FALSE; lock = FALSE; // remainder section // remainder section } while (TRUE); } while (TRUE); Not satisfy bounded-waiting requirement

  14. lock = 0

  15. TestAndSet() Intel compare_and_swap

  16. library - acquire() - release() C# lock (object) { // critical section }

  17. On multiprocessor systems, ensuring atomicity exists is a little harder. It is still possible to use a lock (e.g. a spinlock) the same as on single processor systems, but merely using a single instruction or disabling interrupts will not guarantee atomic access. You must also ensure that no other processor or core in the system attempts to access the data you are working with. The easiest way to achieve this is to ensure that the instructions you are using assert the 'LOCK' signal on the bus, which prevents any other processor in the system from accessing the memory at the same time. On x86 processors, some instructions automatically lock the bus (e.g. 'XCHG') while others require you to specify a 'LOCK' prefix to the instruction to achieve this (e.g. 'CMPXCHG', which you should write as 'LOCK CMPXCHG op1, op2 ). OS non-preemptive CPU atomic instruction critical section CPU iPad, iOS < 4 ( kernel ) https://stackoverflow.com/questions/49346612/atomic-operation-definition-and-multiprocessor

  18. Lock Contention - lock (account) global lock - lock reader(s) writer readers-writers problem

  19. Semaphores critical section resource S mutual exclusive wait(S) { } signal(S) { S++; // 1 } while (S <= 0); // S--; // 1 S 1 process 1 CS S process(es) S is a shared variable between process Semaphore system call concurrent threads S++ increment() compare-and-swap S-- wait(S) Critical section signal(S) wait(S) Critical section signal(S)

  20. Semaphores: spinlock do { // n = #resources CS time slice / time quantum wait(n); CS 2 1. Spinlock time slice 2. No spinlock context-switch // critical section signal(n); // remainder section CS Spin while loop CS } while (TRUE); user process CS user process Spinlock Kernel (scheduler) user process another process No spinlock Semaphore (spinlock / no spinlock) bounded waiting !!!

  21. Semaphores: no-spinlock typedef struct { int value; struct process *list; } semaphore; value + #process CS #process CS <= 0 value++ -1, -2, -3 0, -1, -2 wait(semaphore *S) { S -> value--; if (S -> value < 0) { append this process to S -> list; sleep(); // context switch } } signal(semaphore *S) { S -> value++; if (S -> value <= 0) { remove a process P from S -> list; wakeup(P); } } FIFO No spinlock Ensure bounded waiting by FIFO queue

  22. Spinlock: A case study Error diffusion on multi-core processors spinlock context switch loop semaphore

  23. Race Condition in Web Application n = 0 n = n + 1 id year + n CS id n 1,000 CS Web Application Browser Browser Browser

  24. History's Worst Software Bugs July 28, 1962 -- Mariner I space probe 1982 -- Soviet gas pipeline 1985-1987 -- Therac-25 medical accelerator (race condition) At least five patients die; others are seriously injured. 1988 -- Buffer overflow in Berkeley Unix finger daemon 1985-1987 -- Therac-25 medical accelerator. A radiation therapy device malfunctions and delivers lethal radiation doses at several medical facilities. Based upon a previous design, the Therac-25 was an "improved" therapy system that could deliver two different kinds of radiation: either a low-power electron beam (beta particles) or X-rays. The Therac-25's X-rays were generated by smashing high- power electrons into a metal target positioned between the electron gun and the patient. A second "improvement" was the replacement of the older Therac-20's electromechanical safety interlocks with software control, a decision made because software was perceived to be more reliable. What engineers didn't know was that both the 20 and the 25 were built upon an operating system that had been kludged together by a programmer with no formal training. Because of a subtle bug called a "race condition," a quick-fingered typist could accidentally configure the Therac-25 so the electron beam would fire in high-power mode but with the metal X-ray target out of position. At least five patients die; others are seriously injured.

  25. lock Statement (C# Reference) static global lock ( lock account) lock contention lock bug ? https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/lock-statement

  26. Synchronization Example

  27. buffer (buffer size) thread buffer producer consumer

  28. producer process/thread producer buffer buffer 1 thread consumer 1 consumer process/thread consumer buffer data structure 1 thread producer 1 library data structure thread-safe CS

  29. the firstreaders-writers problem

  30. reader(s) writer db (readers 1) thread read_count rw_mutex reader process read read writer( ) reader signal writer ( ) first problem reader writer reader read reader writer starvation ( second problem starvation)

  31. - thread 1,000 thread 1% writer 99% reader - reader writer 1 ( delay ) - synchronization 1,000 x 1% x 1 = 10 writer reader ( thread 1,000 ) - thread reader print "reading" 1 sleep 1 - thread writer print "writing" 1 sleep 1 C# semaphore https://docs.microsoft.com/en-us/dotnet/api/system.threading.semaphore

  32. 0 4 0 4 1 1 3 3 2 2

  33. deadlock

  34. Note, however, that any satisfactorysolution to the dining-philosophers problem must guard against the possibilitythat one of the philosophers will starve to death. A deadlock-free solution does not necessarily eliminate the possibility of starvation.

Related


More Related Content