Operating System Concepts in Java

Meddle@#600
Consumer-Producer.zip

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

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 Must set javac.includes No tests executed. Must set JVM to use for profiling in profiler.info.jvm Must set profiler agent JVM arguments in profiler.info.jvmargs.agent Must select some files in the IDE or set javac.includes To run this application from the command line without Ant, try: java -jar "${dist.jar.resolved}" Must select one file in the IDE or set run.class Must select one file in the IDE or set run.class 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. 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. Must select one file in the IDE or set run.class 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 Must select some files in the IDE or set javac.includes Some tests failed; see details above. 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 Must select one file in the IDE or set applet.url Must select one file in the IDE or set applet.url

TM298_TMA_Q2/nbproject/genfiles.properties

build.xml.data.CRC32=f05f63ba build.xml.script.CRC32=4dc8f402 build.xml.stylesheet.CRC32=8064a381@1.80.1.48 # 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/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48

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 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 javadoc.use=true javadoc.version=false javadoc.windowtitle= main.class=tm298_tma_q2.TM298_TMA_Q2 manifest.file=manifest.mf meta.inf.dir=${src.dir}/META-INF mkdist.disabled=false platform.active=default_platform run.classpath=\ ${javac.classpath}:\ ${build.classes.dir} # Space-separated list of JVM arguments used when running the project. # You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. # To set system properties for unit tests define test-sys-prop.name=value: run.jvmargs= run.test.classpath=\ ${javac.test.classpath}:\ ${build.test.classes.dir} source.encoding=UTF-8 src.dir=src test.src.dir=test

TM298_TMA_Q2/nbproject/project.xml

org.netbeans.modules.java.j2seproject TM298_TMA_Q2

TM298_TMA_Q2/src/tm298_tma_q2/ArrayStorage.java

TM298_TMA_Q2/src/tm298_tma_q2/ArrayStorage.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package  tm298_tma_q2 ;

/**
 *
 *  @author  macbook
 */
public   class   ArrayStorage   {

     private   Integer []  arrayInt ;

     private   int  size ;

     private   int  item_Counter ; // number of items still not taken from in the array 

     private   int  index_in ; // array index for the writer

     private   int  index_out ; // array index for teh reader

     public   ArrayStorage ( Integer []  arrayInt )   {
         this . arrayInt  =  arrayInt ;
         this . size  =  arrayInt . length ;
         this . item_Counter  =   0 ;
         this . index_in  =   0 ;
         this . index_out  =   0 ;
     }

     public   void  put_item ( int  data )   {
         if   ( this . item_Counter  <  size )   {
            arrayInt [ index_in ]   =  data ;
             this . item_Counter ++ ;

            index_in  =   ( index_in  +   1 )   %  size ;   // circular buffer
         }
     }

     public   int  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
     */
     public   int  getIndex_out ()   {
         return  index_out ;
     }

     /**
     * Set the value of index_out
     *
     *  @param  index_out new value of index_out
     */
     public   void  setIndex_out ( int  index_out )   {
         this . index_out  =  index_out ;
     }

     /**
     * Get the value of index_in
     *
     *  @return  the value of index_in
     */
     public   int  getIndex_in ()   {
         return  index_in ;
     }

     /**
     * Set the value of index_in
     *
     *  @param  index_in new value of index_in
     */
     public   void  setIndex_in ( int  index_in )   {
         this . index_in  =  index_in ;
     }

     /**
     * Get the value of item_Counter
     *
     *  @return  the value of item_Counter
     */
     public   int  getItem_Counter ()   {
         return  item_Counter ;
     }

     /**
     * Set the value of item_Counter
     *
     *  @param  item_Counter new value of item_Counter
     */
     public   void  setItem_Counter ( int  item_Counter )   {
         this . item_Counter  =  item_Counter ;
     }

     /**
     * Get the value of size
     *
     *  @return  the value of size
     */
     public   int  getSize ()   {
         return  size ;
     }

     /**
     * Set the value of size
     *
     *  @param  size new value of size
     */
     public   void  setSize ( int  size )   {
         this . size  =  size ;
     }

}

TM298_TMA_Q2/src/tm298_tma_q2/Consumer.java

TM298_TMA_Q2/src/tm298_tma_q2/Consumer.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package  tm298_tma_q2 ;

/**
 *
 *  @author  macbook
 */
public   class   Consumer   extends   Thread {

     private   ArrayStorage  arrayData ;
     private   String  fileName ;
    

     /**
     * Get the value of arrayData
     *
     *  @return  the value of arrayData
     */
     public   Consumer ( ArrayStorage  arrayData ,   String  fileName )   {     
         super ();
         this . arrayData  =  arrayData ;
     }

    @ Override
     public   void  run ()   {
         super . run ();   //To change body of generated methods, choose Tools | Templates.
         // read the data from the arry and save them to file 
         // stops when negative number is read
      
    
     }

    
    

     public   Consumer ( String  name )   {
         super ( name );
     }
    
    
    
}

TM298_TMA_Q2/src/tm298_tma_q2/Producer.java

TM298_TMA_Q2/src/tm298_tma_q2/Producer.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package  tm298_tma_q2 ;

/**
 *
 *  @author  macbook
 */
public   class   Producer    extends   Thread {
     private   ArrayStorage  test ;
     private   String  fileName ;

     public   Producer ( ArrayStorage  test , String  fileName )   {
         this . test  =  test ;
         this . fileName = fileName ;
     }

    @ Override
     public   void  run ()   {
         super . run ();   //To change body of generated methods, choose Tools | 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
       
    
     }
    
    
}

TM298_TMA_Q2/src/tm298_tma_q2/Testing_Thread.java

TM298_TMA_Q2/src/tm298_tma_q2/Testing_Thread.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package  tm298_tma_q2 ;

/**
 *
 *  @author  macbook
 */
public   class   Testing_Thread   extends   Thread   {
    
     String  readerFileName = "reader.txt" ;
     String  writerFileName = "writer.txt" ;

     public   Testing_Thread ( String  readerFileName , String  writerFileName )   {
         this . readerFileName = readerFileName ;
         this . writerFileName = writerFileName ;
        
     }

    @ Override
     public   void  run ()   {
         super . run ();   //To change body of generated methods, choose Tools | Templates.
        //open read and writer files
        //read the data from the reader file and compare them to the conetent of writer file
        // repeat this until the end of the file
        // if all data macthes, then you can tell that both threads cosnumer and producers are well synchronized
    
    
     }
    
    
    
}

TM298_TMA_Q2/src/tm298_tma_q2/TM298_TMA_Q2.java

TM298_TMA_Q2/src/tm298_tma_q2/TM298_TMA_Q2.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package  tm298_tma_q2 ;

/**
 *
 *  @author  macbook
 */
public   class  TM298_TMA_Q2  {

     /**
     *  @param  args the command line arguments
     */
     public   static   void  main ( String []  args )   {
         // TODO code application logic here
          String  readerFileName = "reader.txt" ;
          String  writerFileName = "writer.txt" ;
          Integer []  arrayInt =   new   Integer [ 20 ];
        
        
         Thread  mainThread = Thread . currentThread ();
         ArrayStorage  shared_buffer =   new   ArrayStorage ( arrayInt );
        
         Consumer  consumer =   new   Consumer ( shared_buffer ,  readerFileName );
         Producer  producer =   new   Producer ( shared_buffer , writerFileName );
         Testing_Thread  test_sync =   new   Testing_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" );
     }
    
}