Question I
You are going to use the semaphores for process synchronization. Therefore, you are asked to develop a consumer and producer multithreaded program.
Let us assume, that we have a thread (producer, we will call it producer_thread) reading data (positive integer numbers) from the keyboard, entered by a user to be stored in an array (dynamic array). (Assume that the array can hold all numbers entered without overflow.)
Another thread (consumer, we will call it consumer_thread) should read data from the array and write them into a file. This thread should run concurrently with the producer (producer_thread).
Your program should make sure that the consumer_thread can read from the array only after the producer_thread has stored new data. Both threads will stop when the user enters a negative number (well synchronized).
Another thread (testing_thread) should start reading the array data as well as the file data and display them on the screen in order to verify if the consumer and producer have worked in a correctly synchronized fashion. This thread should not be synchronized with other threads, it is intended for testing that consumer thread is synchronized with produce thread.
Provide your tutor with the source code as well as screen snapshots that show the work of the testing_thread.
1
TM298_TMA_Q2/build.xml
Builds, tests, and runs the project TM298_TMA_Q2.
TM298_TMA_Q2/manifest.mf
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
TM298_TMA_Q2/nbproject/build-impl.xml
...
Presiding Officer Training module 2024 lok sabha elections
Question IYou are going to use the semaphores for process sy.docx
1. Question I
You are going to use the semaphores for process
synchronization. Therefore, you are asked to develop a
consumer and producer multithreaded program.
Let us assume, that we have a thread (producer, we will call it
producer_thread) reading data (positive integer numbers) from
the keyboard, entered by a user to be stored in an array
(dynamic array). (Assume that the array can hold all numbers
entered without overflow.)
Another thread (consumer, we will call it consumer_thread)
should read data from the array and write them into a file. This
thread should run concurrently with the producer
(producer_thread).
Your program should make sure that the consumer_thread can
read from the array only after the producer_thread has stored
new data. Both threads will stop when the user enters a negative
number (well synchronized).
Another thread (testing_thread) should start reading the array
data as well as the file data and display them on the screen in
order to verify if the consumer and producer have worked in a
correctly synchronized fashion. This thread should not be
synchronized with other threads, it is intended for testing that
consumer thread is synchronized with produce thread.
Provide your tutor with the source code as well as screen
snapshots that show the work of the testing_thread.
2. 1
TM298_TMA_Q2/build.xml
Builds, tests, and runs the project TM298_TMA_Q2.
TM298_TMA_Q2/manifest.mf
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
TM298_TMA_Q2/nbproject/build-impl.xml
3.
4.
5.
6.
7.
8. Must set src.dir
Must set test.src.dir
Must set build.dir
Must set dist.dir
Must set build.classes.dir
Must set dist.javadoc.dir
Must set build.test.classes.dir
Must set build.test.results.dir
Must set build.classes.excludes
Must set dist.jar
29. To run this application from the command line without
Ant, try:
java -jar "${dist.jar.resolved}"
30. Must select one file in the IDE or set run.class
Must select one file in the IDE or set run.class
31. Must select one file in the IDE or set debug.class
Must select one file in the IDE or set debug.class
Must set fix.includes
This target only works when run from inside the NetBeans
IDE.
32. Must select one file in the IDE or set profile.class
This target only works when run from inside the NetBeans
IDE.
This target only works when run from inside the NetBeans
IDE.
This target only works when run from inside the NetBeans
IDE.
34. Must select some files in the IDE or set test.includes
Must select one file in the IDE or set run.class
Must select one file in the IDE or set applet.url
35.
36. Must select some files in the IDE or set javac.includes
Some tests failed; see details above.
37. Must select some files in the IDE or set test.includes
Some tests failed; see details above.
Must select some files in the IDE or set test.class
Must select some method in the IDE or set test.method
Some tests failed; see details above.
Must select one file in the IDE or set test.class
Must select one file in the IDE or set test.class
Must select some method in the IDE or set test.method
38. Must select one file in the IDE or set applet.url
Must select one file in the IDE or set applet.url
40. [email protected]
# This file is used by a NetBeans-based IDE to track changes in
generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will
never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=f05f63ba
nbproject/build-impl.xml.script.CRC32=6ecbbebf
nbproject/[email protected]
TM298_TMA_Q2/nbproject/private/private.properties
compile.on.save=true
user.properties.file=/Users/macbook/Library/Application
Support/NetBeans/8.2/build.properties
TM298_TMA_Q2/nbproject/project.properties
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processor.options=
annotation.processing.processors.list=
annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.
dir}/ap-source-output
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
build.dir=build
build.generated.dir=${build.dir}/generated
build.generated.sources.dir=${build.dir}/generated-sources
# Only compile against the classpath explicitly listed here:
build.sysclasspath=ignore
build.test.classes.dir=${build.dir}/test/classes
build.test.results.dir=${build.dir}/test/results
# Uncomment to specify the preferred debugger connection
transport:
#debug.transport=dt_socket
41. debug.classpath=
${run.classpath}
debug.test.classpath=
${run.test.classpath}
# Files in build.classes.dir which should be excluded from
distribution jar
dist.archive.excludes=
# This directory is removed when the project is cleaned:
dist.dir=dist
dist.jar=${dist.dir}/TM298_TMA_Q2.jar
dist.javadoc.dir=${dist.dir}/javadoc
excludes=
includes=**
jar.compress=false
javac.classpath=
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
javac.external.vm=true
javac.processorpath=
${javac.classpath}
javac.source=1.8
javac.target=1.8
javac.test.classpath=
${javac.classpath}:
${build.classes.dir}
javac.test.processorpath=
${javac.test.classpath}
javadoc.additionalparam=
javadoc.author=false
javadoc.encoding=${source.encoding}
javadoc.noindex=false
javadoc.nonavbar=false
javadoc.notree=false
javadoc.private=false
javadoc.splitindex=true
43. TM298_TMA_Q2/src/tm298_tma_q2/ArrayStorage.javaTM298_
TMA_Q2/src/tm298_tma_q2/ArrayStorage.java/*
* To change this license header, choose License Headers in Pro
ject Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tm298_tma_q2;
/**
*
* @author macbook
*/
publicclassArrayStorage{
privateInteger[] arrayInt;
privateint size;
privateint item_Counter;// number of items still not taken from i
n the array
privateint index_in;// array index for the writer
privateint index_out;// array index for teh reader
publicArrayStorage(Integer[] arrayInt){
this.arrayInt = arrayInt;
this.size = arrayInt.length;
this.item_Counter =0;
44. this.index_in =0;
this.index_out =0;
}
publicvoid put_item(int data){
if(this.item_Counter < size){
arrayInt[index_in]= data;
this.item_Counter++;
index_in =(index_in +1)% size;// circular buffer
}
}
publicint take_item(){
int data =0;
if(this.item_Counter !=0){
data = arrayInt[index_out];
this.item_Counter--;//
index_out =(index_out +1)% size;// circular buffer
}
return data;
}
/**
* Get the value of index_out
*
* @return the value of index_out
*/
publicint getIndex_out(){
return index_out;
}
/**
* Set the value of index_out
*
* @param index_out new value of index_out
45. */
publicvoid setIndex_out(int index_out){
this.index_out = index_out;
}
/**
* Get the value of index_in
*
* @return the value of index_in
*/
publicint getIndex_in(){
return index_in;
}
/**
* Set the value of index_in
*
* @param index_in new value of index_in
*/
publicvoid setIndex_in(int index_in){
this.index_in = index_in;
}
/**
* Get the value of item_Counter
*
* @return the value of item_Counter
*/
publicint getItem_Counter(){
return item_Counter;
}
/**
* Set the value of item_Counter
*
* @param item_Counter new value of item_Counter
46. */
publicvoid setItem_Counter(int item_Counter){
this.item_Counter = item_Counter;
}
/**
* Get the value of size
*
* @return the value of size
*/
publicint getSize(){
return size;
}
/**
* Set the value of size
*
* @param size new value of size
*/
publicvoid setSize(int size){
this.size = size;
}
}
TM298_TMA_Q2/src/tm298_tma_q2/Consumer.javaTM298_TM
A_Q2/src/tm298_tma_q2/Consumer.java/*
* To change this license header, choose License Headers in Pro
ject Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tm298_tma_q2;
/**
47. *
* @author macbook
*/
publicclassConsumerextendsThread{
privateArrayStorage arrayData;
privateString fileName;
/**
* Get the value of arrayData
*
* @return the value of arrayData
*/
publicConsumer(ArrayStorage arrayData,String fileName){
super();
this.arrayData = arrayData;
}
@Override
publicvoid run(){
super.run();//To change body of generated methods, choose Too
ls | Templates.
// read the data from the arry and save them to file
// stops when negative number is read
}
publicConsumer(String name){
super(name);
}
48. }
TM298_TMA_Q2/src/tm298_tma_q2/Producer.javaTM298_TM
A_Q2/src/tm298_tma_q2/Producer.java/*
* To change this license header, choose License Headers in Pro
ject Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tm298_tma_q2;
/**
*
* @author macbook
*/
publicclassProducerextendsThread{
privateArrayStorage test;
privateString fileName;
publicProducer(ArrayStorage test,String fileName){
this.test = test;
this.fileName=fileName;
}
@Override
publicvoid run(){
super.run();//To change body of generated methods, choose Too
ls | Templates.
//ask the user to enter Integers, stop when a negative number is
enetred
//put the integer in teh array
// save it in a file to be compared with reader data
49. }
}
TM298_TMA_Q2/src/tm298_tma_q2/Testing_Thread.javaTM29
8_TMA_Q2/src/tm298_tma_q2/Testing_Thread.java/*
* To change this license header, choose License Headers in Pro
ject Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tm298_tma_q2;
/**
*
* @author macbook
*/
publicclassTesting_ThreadextendsThread{
String readerFileName="reader.txt";
String writerFileName="writer.txt";
publicTesting_Thread(String readerFileName,String writerFileN
ame){
this.readerFileName=readerFileName;
this.writerFileName=writerFileName;
}
@Override
publicvoid run(){
super.run();//To change body of generated methods, choose Too
ls | Templates.
50. //open read and writer files
//read the data from the reader file and compare them to the con
etent of writer file
// repeat this until the end of the file
// if all data macthes, then you can tell that both threads cosnum
er and producers are well synchronized
}
}
TM298_TMA_Q2/src/tm298_tma_q2/TM298_TMA_Q2.javaTM
298_TMA_Q2/src/tm298_tma_q2/TM298_TMA_Q2.java/*
* To change this license header, choose License Headers in Pro
ject Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tm298_tma_q2;
/**
*
* @author macbook
*/
publicclass TM298_TMA_Q2 {
/**
* @param args the command line arguments
*/
publicstaticvoid main(String[] args){
// TODO code application logic here
String readerFileName="reader.txt";
51. String writerFileName="writer.txt";
Integer[] arrayInt=newInteger[20];
Thread mainThread=Thread.currentThread();
ArrayStorage shared_buffer=newArrayStorage(arrayInt);
Consumer consumer=newConsumer(shared_buffer, readerFileNa
me);
Producer producer=newProducer(shared_buffer,writerFileName)
;
Testing_Thread test_sync=newTesting_Thread(readerFileName,
writerFileName);
consumer.start();
producer.start();
try{
consumer.join();// wait until the consumer finishes reading
data form the shared buffer
}
catch(Exception e){
System.out.println(e);
}
test_sync.start();
System.out.print("Mission accomplished");
}
}