View
218
Download
0
Embed Size (px)
Citation preview
Software Engineering
Recitation 7
Suhit Gupta
Review
Anything from last time???
Today
Threads/pthreads Bit operators in Java The GNU make utility Javadocs RMI/Siena
Threads/pThreads
We have already done threads (therefore we are going to skip)
pThreads used in C. (POSIX Threads) pThreads is the name of a machine-
independent library of functions that are generally used to facilitate the multiprocessing of programs on shared memory machines.
pThreads is used to synchronize threads.
Bit Operators
Bit operators treat Number types as a 32 bit value, change bits according to the operator and then converts the value back to Number when done. The operators are bitwise NOT (~), AND (&), OR (|), XOR (^), left shift (<<), right shift (>>), unsigned right shift (>>>).
Bit operators in Java
Bitwise operations are not to be confused with logical operations (&&, ||...)
& - and | - or << - shift left >> - shift right ^ - XOR ~ - compliment/not
Bitwise operators
Bitwise operators (AND, OR) can be used in place of logical operators (&&,||), but they are less efficient, because logical operators are designed to reduce the number of comparisons made, in an expression, to the optimum: as soon as the truth or falsity of an expression is known, a logical comparison operator quits. A bitwise operator would continue operating to the last before the final result were known.
Bitwise operators are basically arithmetic operation. Now, bitwise are not really inefficient because they
are truly put on the chip… The first point is more or less in C. In Java they are
fairly different operations.
Eg-128 64 32 16 8 4 2 1 ----------------------------------- | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | = 1 ----------------------------------- Shift operators move whole bit patterns left or right by shunting them
between boxes. The syntax of this operation is: 1 << 1 would have the value 2, because the bit pattern would have
been moved one place the the left: 128 64 32 16 8 4 2 1 ----------------------------------- | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | = 2----------------------------------- Similarly: 1 << 4 has the value 16 because the original bit pattern is
moved by four places: 128 64 32 16 8 4 2 1 ----------------------------------- | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | = 16----------------------------------- And: 6 << 2 == 12128 64 32 16 8 4 2 1 ----------------------------------- | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | = 6----------------------------------- Shift left 2 places:128 64 32 16 8 4 2 1 ----------------------------------- | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | = 12-----------------------------------
The GNU make utility
http://www.gnu.org/manual/make-3.79.1/html_node/make_toc.html
The make utility automatically determines which pieces of a large program need to be recompiled, and issues commands to recompile them.
You have to have a Makefile Run make to start rules in the Makefile file.
Example of a Makefileedit : main.o kbd.o command.o display.o \ insert.o search.o files.o utils.o cc -o edit main.o kbd.o command.o display.o \ insert.o search.o files.o utils.o
main.o : main.c defs.h cc -c main.ckbd.o : kbd.c defs.h command.h cc -c kbd.ccommand.o : command.c defs.h command.h cc -c command.cdisplay.o : display.c defs.h buffer.h cc -c display.cinsert.o : insert.c defs.h buffer.h cc -c insert.csearch.o : search.c defs.h buffer.h cc -c search.cfiles.o : files.c defs.h buffer.h command.h cc -c files.cutils.o : utils.c defs.h cc -c utils.cclean : rm edit main.o kbd.o command.o display.o \ insert.o search.o files.o utils.o
From the example
To use this makefile to create the executable file called ‘edit’, type: make
make clean
Javadocs
http://java.sun.com Javadoc provides a way to integrate code
comments with program documentation.– Enter comments with a series of flags to indicate
the type of comment/documentation– Run javadoc to parse the flags and the comments
to create an html file documentation of the code
Example
If you miss recitation then you will miss the demonstration
You can run –
javadoc somefile.java
RMI/Siena
We have done Siena (therefore we will skip) Remote Method Invocation (RMI) enables
the programmer to create distributed JavaTM technology-based to Java technology-based applications, in which the methods of remote Java objects can be invoked from other Java virtual machines, possibly on different hosts.
More on RMI
Remote Method Invocation (RMI) is the object equivalent of Remote Procedure Calls (RPC).
While RPC allows you to call procedures over a network, RMI invokes an object's methods over a network.
In the RMI model, the server defines objects that the client can use remotely. The clients can now invoke methods of this remote object as if it were a local object running in the same virtual machine as the client.
More RMI… RMI hides the underlying mechanism of transporting
method arguments and return values across the network.
In Java-RMI, an argument or return value can be of any primitive Java type or any other Serializable Java object.
Java-RMI is a Java-specific middleware spec that allows client Java programs to invoke server Java objects as if they were local.
Java-RMI is tightly coupled with the Java language. Hence there are no separate IDL mappings that are required to invoke remote object methods. This is different from DCOM or CORBA where IDL mappings have to be created to invoke remote methods.IDL==Interface Definition Language
RMI contd… Because of this, parameters passed during method
calls between machines can be true Java Objects. This is impossible in DCOM or CORBA at present.
If a process in an RMI system receives an object of a class that it has never seen before, it can request that its class information be sent over the network.
Over and above all this, Java-RMI supports Distributed Garbage Collection that ties into the local Garbage Collectors in each JVM.
Since both the client and the server may reside on different machines/processes, there needs to be a mechanism that can establish a relationship between the two. Java-RMI uses a network-based registry program called RMIRegistry to keep track of the distributed objects. (Note: The RMI Registry is an RMI server itself!!!)
RMI/Remote Interface
package SimpleStocks;
import java.util.*;
import java.rmi.*;
public interface StockMarket extends java.rmi.Remote { float get_price( String symbol ) throws RemoteException; }
RMI/server
package SimpleStocks; import java.rmi.*; import java.rmi.server.UnicastRemoteObject;
public class StockMarketImpl extends UnicastRemoteObject implements StockMarket { public StockMarketImpl( String name ) throws RemoteException { try { Naming.rebind( name, this ); } catch( Exception e ) { System.out.println( e ); } } public float get_price( String symbol ) { float price = 0; for( int i = 0; i < symbol.length(); i++ ) { price += (int) symbol.charAt( i ); } price /= 5; return price; }
}
RMI/server-deploy
import java.rmi.*; import java.rmi.server.UnicastRemoteObject;import SimpleStocks.*;
public class StockMarketServer { public static void main(String[] args) throws Exception { if(System.getSecurityManager() == null) { System.setSecurityManager( new RMISecurityManager() ); } StockMarketImpl myObject = new StockMarketImpl( "NASDAQ" ); System.out.println( "RMI StockMarketServer ready..." ); } }
RMI/policy.all
grant { permission java.security.AllPermission "", "";
};
RMI/server start
Compile the Files, generate the stubs & skeletons, startup the RMIRegistry and Run the Server
E:\MyProjects\StockRMI\SimpleStocks>E:\MyProjects\StockRMI\SimpleStocks>javac *.java
E:\MyProjects\StockRMI\SimpleStocks>cd..
E:\MyProjects\StockRMI>rmic SimpleStocks.StockMarketImpl
E:\MyProjects\StockRMI>javac *.java
E:\MyProjects\StockRMI>start rmiregistry
E:\MyProjects\StockRMI>java -Djava.security.policy=policy.all StockMarketServerRMI StockMarketServer ready...
RMI/client
import java.rmi.*; import java.rmi.registry.*; import SimpleStocks.*;
public class StockMarketClient { public static void main(String[] args) { try { if(System.getSecurityManager() == null) { System.setSecurityManager( new RMISecurityManager() ); } StockMarket market = (StockMarket)Naming.lookup("rmi://localhost/NASDAQ"); System.out.println( "The price of MY COMPANY is " + market.get_price("MY_COMPANY") ); } catch( Exception e ) { System.out.println( e ); } }
}
RMI/policy.all
grant { permission java.security.AllPermission "", ""; };
RMI/run the client
Compile the files and run the client
E:\MyProjects\StockRMI>E:\MyProjects\StockRMI>javac *.java
E:\MyProjects\StockRMI>java -Djava.security.policy=policy.all StockMarketClientThe price of MY COMPANY is 159.2
E:\MyProjects\StockRMI>
Conclusion
We covered quite a few concepts. Any questions???