ν°μ€ν 리 λ·°
μ΄μ체μ : νλ‘μΈμ€ λκΈ°ν - μΈλ§ν¬μ΄
dirmathfl 2020. 6. 19. 23:02νλ‘μΈμ€ κ°μ μ 보λ₯Ό μ£Όκ³ λ°κ±°λ, νλμ 곡μ λ μμμ μ¬μ©ν κ²½μ°μλ μ£Όμκ° νμνλ€. μλ₯Ό λ€μ΄ νλ‘μΈμ€ Aλ 1λ²μ§ λ©λͺ¨λ¦¬ 곡κ°μ 10μ΄λΌλ κ°μ μ½μ΄ μ€κ³ μνμμΌλ, νλ‘μΈμ€ Bκ° νλ‘μΈμ€ Aκ° μ½λ μ§μ μ 20μ΄λΌλ κ°μΌλ‘ λ³κ²½ν΄ λ²λ¦¬λ©΄ μνμ§ μλ κ°μ΄ λ°νλλ κ²°κ³Όκ° λ°μνλ€. μ΄λ κ² μνΈμ μΌλ‘ μν₯μ λ―ΈμΉλ νλ‘μΈμ€λ€μ Cooperating ProcessλΌκ³ νλ€. μ΄μ λ°λμΈ κ²½μ° Independent ProcessλΌκ³ νλ€.
κ° νλ‘μΈμ€λ‘ μΈν΄ 곡μ λ°μ΄ν°μ μΌκ΄μ±μ 보μ₯νμ§ λͺ»νλ κ²μ λ§κΈ° μν΄ λκΈ°ν(Synchronization)κ° νμνλ€. μμ λ§ν κ°λ¨ν μμμμ μμΉ μλ κ°μ λ°νλ°μ§ μμΌλ €λ©΄, νλ‘μΈμ€ A → νλ‘μΈμ€ Bμ μμλ₯Ό 보μ₯ν κ²½μ° μ΄μ κ°μ λ¬Έμ λ₯Ό ν΄κ²°ν μ μλ€. μ΄ λΏλ§ μλλΌ, 곡μ λ°μ΄ν°λ₯Ό νλ²μ νλμ νλ‘μΈμ€λ§ μ κ·Όν μ μλ λ°©μμ μ¬μ©νμ¬ λ¬Έμ λ₯Ό ν΄κ²°ν μ λ μλ€.
Bank Account Problem (μν κ³μ’ λ¬Έμ )
λκΈ°ν λ¬Έμ μ€ μ§κ΄μ μΌλ‘ μ΄ν΄ν μ μκ³ , λνμ μΈ λ¬Έμ κ° μν κ³μ’μ κΈμ‘μ΄ μ μΆκΈ λ κ²½μ°μ΄λ€. λ§μ½ μ μΆκΈ κ³Όμ μ μμκ° λ³΄μ₯λμ§ μλλ€λ©΄, μμΉ μλ κ²°κ³Όλ₯Ό μ΄λνκ² λ κ²μ΄λ€.
class BankAccount {
int balance;
void deposit(int amount) {
balance += amount;
}
void withdraw(int amount) {
balance -= amount;
}
int getBalance() {
return balance;
}
}
class Parent extends Thread {
BankAccount b;
int count;
Parent(BankAccount b, int count) {
this.b = b;
this.count = count;
}
public void run() {
for (int i = 0; i < count; i++)
b.deposit(1);
}
}
class Child extends Thread {
BankAccount b;
int count;
Child(BankAccount b, int count) {
this.b = b;
this.count = count;
}
public void run() {
for (int i = 0; i < count; i++)
b.withdraw(1);
}
}
public class Test {
static final int MAX = 100;
public static void main(String[] args) throws InterruptedException {
// μν κ³μ’ κ°μ²΄ μμ±
BankAccount b = new BankAccount();
// μ°λ λ κ°μ²΄ μμ±
Parent p = new Parent(b, MAX);
Child c = new Child(b, MAX);
// μ°λ λ μ€ν
p.start();
c.start();
p.join();
c.join();
// μμ‘ μΆλ ₯
System.out.println("Final balance = " + b.getBalance());
}
}
μμ κ°λ¨ν μλ° μ½λμ κ°μ΄ μ κΈμ νλ κ°μ²΄μΈ Parentμ μΆκΈμ νλ κ°μ²΄μΈ Childκ° BanckAccountλΌλ 곡μ©μμμ μ¬μ©νκ³ μλ€. μμ μ½λλ μ¬ννμ¬ λκ°μ μ°λ λκ° κ³΅μ©μμμ μ¬μ©νλ©° μν©μ λ°λΌ μμΉμλ κ²°κ³Όκ° λμ¬ μ μλ€. μΈμ κ°μ μ΄μ λ μ μΆκΈμ λν μμκ° λ³΄μ₯λμ§ μκΈ° λλ¬Έμ΄λ€. (μ½λλ₯Ό μ€ννλ©΄ 맀 μ€νλ§λ€ μ μΆκΈμ μμκ° λ€λ₯΄κ² μΆλ ₯λλ κ²μ μ μ μλ€.)
μκ³ κ΅¬μ (Critical Section)
μ¬λ¬ νλ‘μΈμ€(μ°λ λ)κ° μ¬μ©νλ 곡μ μμμ λν λ³κ²½μ΄ λ°μνλ μ½λ μμμ μκ³ κ΅¬μμ΄λΌκ³ νλ€. μν κ³μ’ λ¬Έμ μ½λμμλ deposit, withdraw λ©μλκ° μ΄μ ν΄λΉνλ€.
μκ³ κ΅¬μμ μ¬λ¬ νλ‘μΈμ€(μ°λ λ)κ° μ§μ νλλΌλ λ°μ΄ν°μ μΌκ΄μ±μ 보μ₯νκΈ° μν΄ λ€μκ³Ό κ°μ 쑰건μ λ§μ‘±μν¬ νμκ° μλ€.
-
Mutual Exculsion : μκ³ κ΅¬μμ νλμ νλ‘μΈμ€(μ°λ λ)κ° μ§μ ν μνλΌλ©΄ λ€λ₯Έ μ°λ λλ μ§μ ν μ μλ€.
-
Progess : 1μ 쑰건μ μΆ©μ‘±μν€κΈ° μν΄, μκ³ κ΅¬μμ μ κ·ΌνκΈ° μν λ€λ₯Έ νλ‘μΈμ€(μ°λ λ)λ€μ λκΈ°κ° νμν κ²μ΄λ€. λ°λΌμ μκ³ κ΅¬μμ λμμ κ°λ¨νκ³ μ§§μ μκ° λ΄μ μ΄λ£¨μ΄μ ΈμΌ νλ€.
-
Bounded Wating : μκ³ κ΅¬μμ μ§μ νλ νλ‘μΈμ€(μ°λ λ)λ μΌμ μκ° λ΄μ μ§μ ν μ μμ΄μΌ νλ€.
νλ‘μΈμ€(μ°λ λ) λκΈ°ν
μμ λ°μνλ λμμ μκ³κ΅¬μμ μ κ·Όνλ λ¬Έμ λ‘ μΈν΄ λ°μνλ λ¬Έμ λ₯Ό ν΄κ²°νκΈ° μν΄μλ λκΈ°νκ° νμνλ€. λκΈ°νλ₯Ό ν΅ν΄ νλ‘μΈμ€μ μ€ν μμλ₯Ό μ μ΄ ν μ μμΌλ©°, μκ³ κ΅¬μ μ§μ μ μν λΆνμν Watingμ μ κ±°ν μ μλ€.
Semaphore (μΈλ§ν¬μ΄)
μκ³ κ΅¬μμ μ§μ νκΈ° μν΄, μκ³ κ΅¬μμ μ§μ ν μ μλ κΆνμ νλνκ³ κΆνμ νλν κ²½μ°μλ§ μκ³ κ΅¬μμ μ§μ ν μ μλλ‘ νλ λκΈ°ν λ°©μμ λ§νλ€. μλ°μ κ²½μ° μ°λ λ μ¬μ© μ java.util.concurrent.Semaphoreλ₯Ό ν΅ν΄ μΈλ§ν¬μ΄ κ°μ²΄λ₯Ό μμ±νμ¬, μκ³ κ΅¬μμ λ¬Έμ λ₯Ό λ°©μ§ν μ μλ€.
BankAccount Problem (USE Semaphore)
import java.util.concurrent.Semaphore;
class BankAccount {
int balance;
Semaphore s;
BankAccount() {
s = new Semaphore(1);
}
void deposit(int amount) {
try {
s.acquire();
} catch (InterruptedException e) { }
balance += amount;
s.release();
}
void withdraw(int amount) {
try {
s.acquire();
} catch (InterruptedException e) { }
balance -= amount;
s.release();
}
int getBalance() {
return balance;
}
}
λμμ μ¬λ¬ μ°λ λκ° μκ³ κ΅¬μμ μ κ·Όνλ κ²μ λ§κΈ° μν΄ μλ°μμλ μμ κ°μ΄ ꡬνν μ μλ€. κΈ°μ‘΄ μ½λμ λ¬λ¦¬ BackAccount Generatorμμ μΈλͺ¨ν¬μ΄ κ°μ²΄λ₯Ό ν λΉνκ³ , νλμ μ°λ λλ§ μ κ·Όν μ μκ² permitsμ κ°μ 1λ‘ μ€μ νλ€. λν, depositκ³Ό withdraw ν¨μκ° νΈμΆλλ©΄, μΈλͺ¨ν¬μ΄λ₯Ό νλ/ ν΄μ νλ μ½λλ₯Ό ν΅ν΄ μκ³ κ΅¬μμ νλμ μ°λ λλ§ μ κ·Όν μ μλλ‘ ν μ μλ€.
Ordering
λ¨μν μΈλͺ¨ν¬μ΄λ₯Ό μ μ©ν κ²½μ°μ λ¬Έμ μ μ μ¬μ ν μ μΆκΈμ λν μμλ₯Ό 보μ₯νμ§ μλλ€. λ°λΌμ μ€μ μν©μ λΉμ·νκ² μ μΆκΈμ λ°λ₯Έ μμλ₯Ό 보μ₯μν€κΈ° μν΄μλ λ€μκ³Ό κ°μ κ³Όμ μ΄ νμνλ€.
class BankAccount {
int balance;
Semaphore critical;
Semaphore order;
BankAccount() {
critical = new Semaphore(1);
order = new Semaphore(0);
}
void deposit(int amount) {
try {
critical.acquire();
} catch (InterruptedException e) { }
balance += amount;
critical.release();
order.release();
}
void withdraw(int amount) {
try {
order.acquire();
critical.acquire();
} catch (InterruptedException e) { }
balance -= amount;
critical.release();
}
int getBalance() {
return balance;
}
}
μκ³ κ΅¬μμ λν μ κ·Όμ μΈλͺ¨ν¬μ΄ νλ / ν΄μ λ₯Ό ν΅ν΄ 보μ₯νμλ€. μμλ₯Ό 보μ₯νκΈ° μν΄μλ μΈλͺ¨ν¬μ΄λ₯Ό 0μΌλ‘ μ€μ νκ³ , μ κΈν λ ν΄μ νκ³ μΆκΈ ν λ νλνλ λ°©μμ μ¬μ©νλ©΄ μμλ₯Ό 보μ₯ν μ μλ€.
'ποΈββοΈ κΈ°λ° λ€μ§κΈ° > μ΄μ체μ ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
μ΄μ체μ : νλ‘μΈμ€ λκΈ°ν - κ΅μ°©μν (0) | 2020.06.20 |
---|---|
μ΄μ체μ : νλ‘μΈμ€ λκΈ°ν - μμ±μ β μλΉμ (0) | 2020.06.20 |
μ΄μ체μ : νλ‘μΈμ€μ μ°λ λ (0) | 2020.06.19 |
μ΄μ체μ : CPU μ€μΌμ€λ§ (0) | 2020.06.19 |
μ΄μ체μ : νλ‘μΈμ€ κ΄λ¦¬ (0) | 2020.06.18 |