ν°μ€ν 리 λ·°
μ΄μ체μ : νλ‘μΈμ€ λκΈ°ν - μμ±μ ↔ μλΉμ
dirmathfl 2020. 6. 20. 00:04Producer - Consumer Problem
νΉμ νλ‘μΈμ€λ λ°μ΄ν°λ₯Ό μμ°νκ³ , νΉμ νλ‘μΈμ€λ μμ°λ λ°μ΄ν°λ₯Ό ν΅ν΄ μ²λ¦¬νλ μμ κ³Ό κ°μ΄ μμ±μ-μλΉμκ° μλ νλ‘κ·Έλ¨μμ λ°μν μ μλ λ¬Έμ μ΄λ€. λ§μ½ μμ°λμ§ μμ κ²½μ°μ λ°μ΄ν°λ₯Ό μ¬μ©νλ μλΉμκ° λ°μ΄ν°λ₯Ό κ°μ Έκ°κ² λλ€λ©΄ μμΉ μλ λ°μ΄ν° (μ°λ κΈ° κ°)μ κ°μ Έκ°κ² λ κ°λ₯μ±μ΄ ν¬λ€. λ°λΌμ κ°κ°μ νλ‘μΈμ€(μ°λ λ)λ‘ λΆν° μμ±μ - μλΉμκ° μ‘΄μ¬νλ€λ©΄ μμ±, μλΉ μμΌλ‘ μ§νλ μ μλλ‘ λκΈ°νκ° νμνλ€.
import java.util.concurrent.Semaphore;
class Buffer {
int[] buf;
int size;
int count;
int in;
int out;
Semaphore mutex, full, empty;
Buffer(int size) {
buf = new int[size];
this.size = size;
count = in = out = 0;
mutex = new Semaphore(1);
full = new Semaphore(0);
empty = new Semaphore(size);
}
void insert(int item) {
try{
empty.acquire();
mutex.acquire();
buf[in] = item;
in = (in+1)%size;
count++;
mutex.release();
full.release();
} catch (InterruptedException e) {}
}
int remove() {
try {
full.acquire();
mutex.acquire();
int item = buf[out];
out = (out+1)%size;
count--;
mutex.release();
empty.release();
return item;
} catch (InterruptedException e) {}
return -1;
}
}
class Producer extends Thread {
Buffer b;
int N;
Producer(Buffer b, int N) {
this.b = b; this.N = N;
}
public void run() {
for (int i=0; i<N; i++)
b.insert(i);
}
}
class Consumer extends Thread {
Buffer b;
int N;
Consumer(Buffer b, int N) {
this.b = b; this.N = N;
}
public void run() {
int item;
for (int i=0; i<N; i++)
item = b.remove();
}
}
class Test {
public static void main(String[] arg) {
Buffer b = new Buffer(100);
Producer p = new Producer(b, 10000);
Consumer c = new Consumer(b, 10000);
p.start();
c.start();
try {
p.join();
c.join();
} catch (InterruptedException e) {}
System.out.println("Number of items in the buf is " + b.count);
}
}
μμ μλ° μ½λμ κ°μ΄ μμ±μ(Producer), μλΉμ(Consmer)λ λ²νΌλΌλ κ°μ²΄λ₯Ό 곡μ νμ¬ κ°μ μΆκ°νκ±°λ μμ νλ€. λ¨μν μΈλͺ¨ν¬μ΄λ₯Ό μ μ©ν κ²½μ° μΈλͺ¨ν¬μ΄ νλμ΄ νμν κ²½μ° Busy Watingμ νκ² λλ©° μ΄λ λΉν¨μ¨μ μ΄λ€. λ°λΌμ 2κ°μ μΈλ§ν¬μ΄λ₯Ό μΆκ°νμ¬ Busy Watingμ λ°©μ§νλ©° μμ±μ, μλΉμ λ¬Έμ λ₯Ό ν΄κ²°ν μ μλ€.
- empty : λ²νΌ ν¬κΈ° λ§νΌ μ΄κΈ°ν νκ³ , μλ‘μ΄ κ°μ΄ μΆκ° λ κ²½μ° 1 κ°μ μν¨κ³ , κ°μ΄ μμ λ κ²½μ° 1 μ¦κ° μν¨λ€.
- full : emptyμ λ°λλλ μν μ μννλ€.
Dining Philosopher Problem
μν ν μ΄λΈμ 5λͺ μ μ² νμκ° μκ³ , μ κ°λ½μ 5κ°λ§ μλ μν©μ΄ μλ€. κ° μ² νμλ μκ°νκ³ μμ¬νλ κ³Όμ μ λ°λ³΅νλ©°, μμ¬λ₯Ό νκΈ° μν΄μλ 2κ°μ μ κ°λ½μ΄ μμ΄μΌ νλ€. μν ν μ΄λΈμ μμ μ² νμλ€μ μμ μ μΌμͺ½μ μ κ°λ½μ νλνκ³ , μ€λ₯Έμͺ½ μ κ°λ½μ΄ νλκ°λ₯ν κ²½μ° μμ¬λ₯Ό μμνλ€. μμ¬κ° λμ΄λλ©΄ μΌμͺ½ μ κ°λ½μ λ΄λ €λκ³ , μ€λ₯Έμͺ½ μ κ°λ½μ λ΄λ €λλ€.
import java.util.concurrent.Semaphore;
class Philosopher extends Thread {
int id; // philosopher id
Semaphore lstick, rstick; // left, right chopsticks
Philosopher(int id, Semaphore lstick, Semaphore rstick) {
this.id = id;
this.lstick = lstick;
this.rstick = rstick;
}
public void run() {
try {
while (true) {
lstick.acquire();
rstick.acquire();
eating();
lstick.release();
rstick.release();
thinking();
}
}catch (InterruptedException e) { }
}
void eating() {
System.out.println("[" + id + "] eating");
}
void thinking() {
System.out.println("[" + id + "] thinking");
}
}
class Test {
static final int num = 5; // number of philosphers & chopsticks
public static void main(String[] args) {
int i;
/* chopsticks */
Semaphore[] stick = new Semaphore[num];
for (i=0; i<num; i++)
stick[i] = new Semaphore(1);
/* philosophers */
Philosopher[] phil = new Philosopher[num];
for (i=0; i<num; i++)
phil[i] = new Philosopher(i, stick[i], stick[(i+1)%num]);
/* let philosophers eat and think */
for (i=0; i<num; i++)
phil[i].start();
}
}
μμ μ½λλ μμ¬νλ μ² νμ λ¬Έμ κ° λ°μν μ μλ μλ° μ½λμ΄λ€. νμ§λ§ μ΄ μ½λλ deadlockμ λΉ μ§ μ μλ€. μλ₯Ό λ€μ΄ λͺ¨λ μ² νμκ° λμμ μΌμͺ½ μ κ°λ½μ μ§μ΄λ€λ©΄ (μΈλ§ν¬μ΄λ₯Ό νλνλ©΄) λͺ¨λ μ² νμλ€μ μ€λ₯Έμͺ½ μ κ°λ½μ μ§μ΄λ€ μ μμ΄ starvationμ λΉ μ§κ² λλ€. μ΄λ μλͺ»λ μΈλ§ν¬μ΄ μ€μ μΌλ‘ νλ‘μΈμ€μ μμ μ΄ μ§νλμ§ μλ λνμ μΈ μν©μ΄λ©°, λ§μ½ μ΄μ체μ μμ deadlockμ΄ λ°μνλ€λ©΄ μ΄λ μ»΄ν¨ν°λ₯Ό μ¬μ©ν μ μλ μν©μ΄ λ μλ μλ€.
'ποΈββοΈ κΈ°λ° λ€μ§κΈ° > μ΄μ체μ ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
μ΄μ체μ : νλ‘μΈμ€ λκΈ°ν - λͺ¨λν° (0) | 2020.06.20 |
---|---|
μ΄μ체μ : νλ‘μΈμ€ λκΈ°ν - κ΅μ°©μν (0) | 2020.06.20 |
μ΄μ체μ : νλ‘μΈμ€ λκΈ°ν - μΈλ§ν¬μ΄ (0) | 2020.06.19 |
μ΄μ체μ : νλ‘μΈμ€μ μ°λ λ (0) | 2020.06.19 |
μ΄μ체μ : CPU μ€μΌμ€λ§ (0) | 2020.06.19 |