Process Synchronization Buffer Producer Consumer buffer Example Producer
Process Synchronization Buffer Producer Consumer ตองม buffer ถา อตราการผลต /อตราการบรโภค ไมเทากน Example Producer Consumer 1 tar gzip tar cvf -. | gzip > target. tar. gz 2 cat grep cat dictionary | grep board ไมตองเขยนลงฮารดดสก Linux Command เขยนลง memory แทน และไมเปลอง memory มาก
Circular Queue A D 1 out C in 2 0 3 4 7 6 5 count = 3 BUFFER_SIZE = 8
Producer & Consumer problem Producer: while (ยงตอง produce อย ) { while (count == BUFFER_SIZE) buffer[in] = next. Produced in = (in + 1) % BUFFER_SIZE count++ } Consumer: while (ยงตอง consume อย ) { while (count == 0) next. Consumed = buffer[out] out = (out + 1) % BUFFER_SIZE count -}
Machine Language count++ instr 1: register 1 = count instr 2: register 1 = register 1 + 1 instr 3: count = register 1 count-instr 1: register 2 = count instr 2: register 2 = register 2 – 1 instr 3: count = register 2 Let count = 5 Executing (single core) 1 2 3 3 results in “count = 4”
Critical Section entry section เพม code เขาไปปองกน critical section exit section remainder section A solution to the critical-section problem must satisfy: 1. Mutual exclusion process/thread ไดเขาใช critical section ทละ 1 เทานน 2. Progress เลอก process/thread เขามาใน critical section ไดเสมอ ไมตดตาย deadlock 3. Bounded waiting รบประกนวารอนานไมเกนเวลาท กำหนด จะตองไดเขา critical section
Obsolete! Peterson’s Solution int bool turn; // turn = 0, 1 process that is allowed to execute in its CS. flag[2]; // flag[i] = true, process i is ready to enter its CS. Process 0 do { Process 1 do { flag[0] = TRUE; turn = 1; while (flag[1] && turn == 1); critical section flag[0] = FALSE remainder section } while (TRUE); flag[1] = TRUE; turn = 0; while (flag[0] && turn == 0); critical section flag[1] = FALSE remainder section } while (TRUE); This software solution assumes atomic instructions. Mutual exclusion, progress, bounded waiting are satisfied. แตทำไดแค 2 process เทานน OS สมยกอนเปนแบบ non-preemtive เพราะ CPU ยงไมม atomic instructions ดงนนตองทำ critical section ใหเสรจกอนปลอย CPU เชน i. Pad, i. OS < 4 (อาจจะเพราะพฒนา
Sync. HW: Test. And. Set bool Test. And. Set(bool *target) { bool rv = *target; (object) *target = TRUE; return rv; } do { เรมตน lock = FALSE; while (Test. And. Set(&lock)); (object) // critical section lock = FALSE; Shared variable do { while (Test. And. Set(&lock)); (object) // critical section lock = FALSE; // remainder section } while (TRUE); // atomically by hardware // rv นาจะหมายถง return value // remainder section } while (TRUE); Not satisfy bounded-waiting requirement
Sync. HW: Swap void Swap(bool *a, bool *b) { bool tmp = *a; *a = *b; *b = tmp; } // atomic instruction (HW) Shared variable lock = FALSE; do { key = TRUE; while (key == TRUE) Swap(&lock, &key); // critical section lock = FALSE; // remainder section } while (TRUE); do { // remainder section } while (TRUE); Not satisfy bounded-waiting requirement
Bounded-waiting mutual exclusion ไดเขาใช ตองจดควให process do { ทรออย รอใช waiting[i] = TRUE; เรมตน waiting[i] = false; CS key = TRUE; lock = false; while (waiting[i] && key) key = Test. And. Set(&lock); CS แลว waiting[i] = FALSE; // critical section คนหา process ทรอใช CS (หาไปทางขวา) j = (i + 1) % n; while ((j != i) && !waiting[j]) j = (j + 1) % n; if (j == i) ใมม process CS lock อนทรอใช = FALSE; Unlock ปลอย CS else ม process j รอใช CS waiting[j] = FALSE; สงมอบ CS ให process // remainder section } while (TRUE); ตวแรกทอยถดไปทางขวา
Semaphores ใน critical section ม resource อย exclusive wait(S) { } S คอ จำนวนทรพยากร ทม ถา 1 process ใชทรพยากร กจะเขาใชไดพรอมกนไมเกน 1 ชน S is a shared variable between process wait(S) Critical section เปน mutual signal(S) { S++ } while (S <= 0); S-- S = 10 S ชน S process Critical section signal(S) Semaphore is a system call. No processes can execute wait() or signal() at the same time by using the previously described technique.
Semaphores: spinlock do { // n = #resources หยดรอจนกวาจะเขา CS ได หรอ หมด time slice / time quantum wait(n); // critical section signal(n); การหยดรอ CS ม 2 options คอ 1. Spinlock ใช time quantum ตอไป 2. No spinlock เรยก context-switch // remainder section เขา CS } while (TRUE); Spinlock user process No spinlock user process Spin กคอ while loop รอ CS user process ����� CS Kernel (scheduler) Semaphore (spinlock / no spinlock) กยงไมรบประกน another process bounded waiting !!!
Semaphores: no-spinlock typedef struct { int value; struct process *list; } semaphore; – S #processes waiting + #processes allowed wait(semaphore *S) { signal(semaphore *S) { S -> value--; S -> value++; if (S -> value < 0) { if (S -> value <= 0) { // FIFO append this process to S -> list; remove a process P from S -> list; block(); // context switch wakeup(P); No spinlock } } Ensure bounded waiting by FIFO queue
Spinlock: A case study Error diffusion on multi-core processors Use spinlock, do not allow context-switch !!!
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. Therac-25's X-rays were generated by smashing highpower 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. http: //www. wired. com/software/coolapps/news/2005/11/69355? current. Page=all
lock Statement (C# Reference) เรยนมาถงตรงนแลว ดออกหรอยงวาโปรแกรมน ยงไง ? https: //docs. microsoft. com/en-us/dotnet/csharp/language-reference/keywords/lock-statement bug
- Slides: 16