Question: Process Synchronization Due Date: Apr. 11th Objective: This program assignment is provided to let the students understand how to the Operating Systems schedule all the

Process Synchronization

Due Date: Apr. 11th

Objective: This program assignment is provided to let the students understand how to the Operating Systems schedule all the processes on a multiprocessor or multicore environment.

Environment: Unix/Linux environment (VM Linux or Triton Server), Windows platform, Mac OS

Language: C, C++, and Java

Requirements:

i. You can write an application program which shows process/thread synchronization using:

1. Semaphore

2. Monitor

3. Java Synchronized feautre

ii. Among the above synchronization methods, I hope you would pick one method and apply your application program which requires process/thread synchronization.

iii. In case of Java, please refer to the reference found at the bottom.

iv. Final result should be organized as a document which explains the overview and the structure of your program, real code, execution results (including captured image), and the conclusion including justification of your program, lessons you've learned, comments, etc.

Reference 1: Concurrent/Multithreaded Programming in Java

Java is known as one of the first languages to make multithreading easily available to developers. A thread is assigned code through Thread subclass (start() and run() method) or Runnable interface implementation.

from Java Concurrency/Multithreadingby Jakob Jenkov

(1) Critical Section in Java

- Every method declared with the synchronized keyword is a critical section.

- Only one execution thread will access one of the methods of an object declared with the synchronized keyword.

- Only one execution thread will access one of the static method declared with the synchronizedkeyword, but another thread can access other non-static methods of an object of that class.

- Two threads can access two different synchronized methods if one is static and the other one is not.

- If both methods change the same data, you can have data inconsistency errors.

(2) Bank Synchronization Program

- Description

o Account class : using the synchronized keyword, we guarantee correct access to shared data in concurrent applications

- tmp, to store the value of the account's balance.

o Bank class : makes 100 calls to the subtractAmount() method that decrements the balance by 100 in each call.

o Company class : makes 100 calls to the addAmount() method that increments the balance by 1000 in each call.

o TestBank class

- Remark: Only a thread can access the methods of an object that use the synchronized keyword in their declaration. If a thread (A) is executing a synchronized method and another thread (B) wants to execute other synchronized methods of the same object, it will be blocked until the thread (A) ends.

But if thread (B) has access to different objects of the same class,

none of them will be blocked.

from Java 7 Concurrency Cookbook by Javier Fernandez Gonzalez (2012)

(3) Blocking Queues

o Java comes with blocking queue implementations in the java.util.concurrent package.

(4) Thread Pools

o Java comes with built in thread pools in the java.util.concurrent package.

(5) Reference

o Java Concurrency/Multithreading Tutorial by Jakob Jenkov

Reference 2: Producer-Consumer Problem using Monitor in Java

Solution to the Producer-Consumer Problem using Monitor in Java (synchronized feature in Java):

This solution has four Java classes:

1) The outer class: ProducerConsumer

2) The 2nd and 3rd classes: Producer and Consumer

3) The monitor class: OurMonitor

REMARK: DO NOT COPY AND PASTE THE FOLLOWING CODE DIRECTLY (Hidden special characters may cause errors.).

import java.util.*;

public class ProducerConsumer { static final int N = 100; // constant for the buffer size

static Producer p = new Producer(); // instantiate a new Producer thread

static Consumer c = new Consumer(); // instantiate a new Consumer thread

static OurMonitor mon = new OurMonitor(); // instantiate a new OurMonitor

public static void main(String args[]) {

p.start(); // start the Producer thread c.start(); // start the Consumer thread } static class Producer extends Thread { public void run() { // run method with the thread code int item;

while (true) { // producer loop: infinite loop

item = produceItem();

mon.insert(item);

} // end of while } // run()

private int produceItem() {

Random rand = new Random();

int num = rand.nextInt(100)+1;

System.out.println("P:" + num + " ");

return num;

} // produceItem()

} // class Producer

static class Consumer extends Thread { public void run() { // run method with the thread code int item;

while (true) { // consumer loop : infinite loop

item = mon.remove();

consumeItem(item);

} // end of while } // run()

private void consumeItem(int item) {

System.out.println("C:" + item + " ");

} // consumeItem()

} // class Consumer

static class OurMonitor { // Monitor definition

// shared data of the Monitor

private int buffer[] = new int [N];

private int count = 0;

private int lo = 0, hi = 0;

// Monitor operations

// 1. insert() operator public synchronized void insert(int val) { if (count==N) gotoSleep(); // if the buffer is full, go to sleep

buffer[hi] = val; // insert an item into the buffer

hi = (hi+1) % N; // slot to place next item in

count = count + 1;

if (count==1) notify(); // if consumer was sleeping, wake it up

} // insert()

// 2. remove() operator public synchronized int remove() { int val;

if (count==0) gotoSleep(); // if the buffer is empty, go to sleep

val = buffer[lo]; // fetch an item from the buffer

lo = (lo+1) % N; // slot to fetch next item from

count = count - 1;

if (count==N-1) notify(); // if producer was sleeping, wake it up

return val;

} // remove()

private void gotoSleep() {

try {

wait(); // wait() can be interrupted

} catch(InterruptedException exc) {

System.out.println("Interrupt occurred!");

};

} // gotoSleep()

} // class OurMonitor

} // class ProducerConsumer

Execution

> run ProducerConsumer

P:13 P:23 P:88 P:55 P:32 P:45 P:81 P:7 P:19 P:26 P:7 P:81 P:26 P:11 P:3 P:24 P:23 P:14 P:88 P:24 P:37 P:82 P:82 P:55 P:44 P:84 C:13 P:95 C:23 P:50 C:88 P:95 C:55 P:52 C:32 P:86 C:45 C:81 P:84 C:7 P:58 C:19 P:45 C:26 P:57 C:7 P:12 C:81 P:19 C:26 C:11 P:58 C:3 P:8 C:24 C:23 C:14 P:49 C:88 P:58 C:24 C:37 P:96 P:84 P:51 P:71 P:32 P:66 P:32 P:27 P:41 P:12 C:82 C:82 C:55 C:44 C:84 C:95 C:50 C:95 C:52 C:86 C:84 C:58 C:45 C:57 C:12 C:19 C:58 C:8 C:49 C:58 C:96 C:84 C:51 C:71 C:32 C:66 C:32 C:27 C:41 C:12 >

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!