Process Synchronization
Process synchronization is one of the techniques which is used in shared resources. When multiple processes are executed, this is used to coordinate the resources.
Here, we have taken the producer, consumer problem. Producer
produces the item. Consumer uses the item.
Java Program:
- · Include the semaphore package.
- · Create a buffer class for shared resource.
- · Create a semaphore for empty, full and mutex.
- · To produce the item,use the steps as follows.
- · If empty acquires it wait for not empty buffer.
- · If mutex acquires means it locks the critical section.
- · Assign the value and display it.
- · Finally, release the semaphores.
- · To consume the item,use the steps.
- · Wait for buffer is not empty.
- · Lock the mutex.
- · Take the item to value.
- · Print the value.
- · Release the semaphores.
- · Create two classes for producer and consumer.
- · Assign the value to the constructor.
- · Execute the run function.
- · Create a main class. Objects are created. Call the functions. Print the output.
Code:
import java.util.concurrent.Semaphore;
//create a shared buffer class
class SBuffer {
private int s_item;
private boolean hasItem
= false;
// create Semaphores
private Semaphore
s_mutex = new Semaphore(1); // mutual
exclusion purpose
private Semaphore
s_empty = new Semaphore(1); // is
buffer empty
private Semaphore s_full
= new Semaphore(0); // is buffer full
// Producer produces the
item
public void
s_produce(int value) throws InterruptedException {
s_empty.acquire(); // it
waits if the buffer is not empty
s_mutex.acquire(); // it
locks critical section
s_item = value;
hasItem = true;
System.out.println("Produced: " + value);
s_mutex.release(); // unlock
the control
s_full.release(); // it
gives the signal that buffer is full
}
// Consumer consumes the
item
public int s_consume()
throws InterruptedException {
s_full.acquire(); // wait
if buffer is empty
s_mutex.acquire(); // lock
critical section
int value = s_item;
hasItem = false;
System.out.println("Consumed: " + value);
s_mutex.release(); // it
unlocks
s_empty.release(); // it
makes the signal buffer is empty
return value;
}
}
class s_Producer extends Thread {
private SBuffer buffer1;
public
s_Producer(SBuffer buffer1) { this.buffer1 = buffer1; }
public void run() {
try {
for (int i = 1;
i <= 6; i++) {
buffer1.s_produce(i);
Thread.sleep(500); // it simulates the work
}
} catch
(InterruptedException e) { e.printStackTrace(); }
}
}
class s_Consumer extends Thread {
private SBuffer buffer1;
public
s_Consumer(SBuffer buffer1) { this.buffer1 = buffer1; }
public void run() {
try {
for (int i = 1;
i <= 6; i++) {
buffer1.s_consume();
Thread.sleep(900); // it simulates the work
}
} catch
(InterruptedException e) { e.printStackTrace(); }
}
}
public class ProConDemo {
public static void
main(String[] args) {
SBuffer buffer1 =
new SBuffer();
s_Producer p = new
s_Producer(buffer1);
s_Consumer c = new
s_Consumer(buffer1);
p.start();
c.start();
}
}
Output:
C:\raji\blog>javac ProConDemo.java
C:\raji\blog>java ProConDemo
Produced: 1
Consumed: 1
Produced: 2
Consumed: 2
Produced: 3
Consumed: 3
Produced: 4
Consumed: 4
Produced: 5
Consumed: 5
Produced: 6
Consumed: 6
Thus, the way to create process synchronization using producer, consumer problem was developed. Hope, you understood this concept. Keep Coding!!!
Comments
Post a Comment