You are on page 1of 11


Write a simple stocks & shares application that makes use of a shared data structure on the server an is safe from multiple client calls. Server package stockshareapp; import; import; import java.rmi.Naming; public class StockAppServer { stockImpl c; public StockAppServer ! { BufferedReader reader; reader " ne# BufferedReader ne# InputStreamReader S$!!; tr$ { c " ne# stockImpl !; c.addShare %abc%& %pragma infotech%& '((& '(& )((!; c.addShare %ank%& %mahavir infotech%& )((& )(& *((!; c.addShare %aks%& %akash infotech%& '((& '(& *((!; c.addShare %sid%& %siddh infotech%& )((& '(& +((!; c.addShare %kr%& %mehsana snacks%& ,(& '(& *((!; Naming.rebind %rmi-..localhost-'(//.stockService%& c!; S$stem.out.println %0aiting ...%!; do { S$stem.out.println %1nter a choice-%!; S$stem.out.println %'. Add Ne# Share%!; S$stem.out.println %). 2pdate Share 3rice%!; S$stem.out.println %*. 14it5n%!; int op; double price; op " Integer.parseInt reader.read6ine !!; s#itch op! { case 'S$stem.out.println %1nter a ne# Share7ode-%!; String share7ode " reader.read6ine !; if ifAlr14its share7ode!! { S$stem.out.println %Share 7ode Alread$ 14ists...%!; break; 8 S$stem.out.println %1nter a 9escription-%!; String description " reader.read6ine !;

out. 8 private boolean ifAlr14its String sd! { String s<= " c.out.parseInt reader. e!. double curprice " 9ouble.parse9ouble reader.out. i.println %1nter a Ne# 3rice-%!. int no " Integer. s<i= >" null. int i. case *S$stem. S$stem.e4it (!.println %:rouble. 8 8 #hile true!.! { if s<i=. price " 9ouble. break. double startprice " 9ouble.read6ine !!.read6ine !!. break.println %1nter a 7urrent 3rice-%!.println %1nter a Share7ode-%!. case )S$stem.addShare share7ode& description& curprice& startprice& no!.read6ine !!.read6ine !.out. 8 8 public static void main String args<=! { StockAppServer s " ne# StockAppServer !.parse9ouble reader. S$stem.set7ur3rice s& price!.println %1nter a No of Shares-%!. 8 catch 14ception e! { S$stem. c.println %1nter a Start 3rice-%!.S$stem.setStatus s!. S$stem.. String s " reader. 8 8 .setStatus null!.out.getShare7odes !. c.parse9ouble reader.e?uals sd!! { return true. for i " (. c. 8 8 return false.read6ine !!. c.% .out.

import java. S$stem.out. share7ode " s<op A '=.out.read6ine !!.. S$stem. do { s " i .read6ine !!. BufferedReader reader.println %1nter a choice for sharecode-%!. 8 S$stem. Bu$ Shares%!. int i " (. reader " ne# BufferedReader ne# InputStreamReader S$stem. % . s#itch op! { case 'if c. op " Integer. %. % as sharecode%!.getStatus ! "" null DD > c.out.out. Bet 9escription%!. Bet current priCe%!. s<i= .parseInt reader.Naming.out. '!! { S$stem.. Bet No of Shares%!. S$stem.! { S$stem. 14it%!.println %1nter an$ one sharecode-%!. String share7ode " null. op " Integer. if op >" i . S$ String s<=. S$stem. %.rmi. do { S$stem.out.println %. e4it%!.getShare7odes !.Client package stockshareapp.localhost. '! . public class client { public static void main String<= args! thro#s I@14ception { tr$ { stockInterf c " stockInterf! Naming.lookup %rmi-.println %'.parseInt reader. import java.out..println i .stockService%!.io.println %*. import java.InputStreamReader.out. for i " (. s<i= >" null.println %+.out. op " (. S$stem.println %).out. '! .in!!.BufferedReader. i.e?uals share7ode!!! { .getStatus !. import java.println %1nter a choice for more information-%!. int op " (.

descib!.println %9escription-% .out. if ans! { S$stem. 8 8 else { S$stem. break.read6ine !!.out.out.getStatus ! "" null DD > c.double price " c.getStatus !.get9escription share7ode!.println %7urrentl$ Server is updating Share price.!. 8 break. :r$ Again 6ater%!. defaultS$stem. S$stem.println %Not enough shares%!. 8 8 #hile op >" i.println %1nter No of Shares%!.println %7urrent 3rice-% . case )String descib " c. case . int bu$noofshares " Integer.out. 8 catch 14ception e4! { S$stem.println %Number of Shares-% . 8 else { S$stem.get7ur3rice share7ode!.println e4.toString !!.println %Successful%!.out. S$stem. :r$ Again 6ater%!. S$stem.out. 8 else { S$stem. break.break. boolean ans " c.e?uals share7ode!!! { S$stem.println %Inappropriate 7hoice%!.getNo@fShares share7ode!.bu$Shares share7ode& bu$noofshares!. .println %7urrentl$ Server is updating Share price. 8 8 #hile op >" .parseInt reader.out. case +if c. 8 break.out. price!. share!.out.'!!. case *int share " c.out.

Remote14ception.Remote14ception.add cur3rice!. import java. public int getNo@fShares String share7ode! thro#s java. private String flag"null.rmi. public interface stockInterf e4tends java.util. 8 public boolean addShare String share7ode& String description& double cur3rice& double start3rice& int no@fShares! { tr$ { Arra$6ist al " ne# Arra$6ist '(!.Remote14ception. import java.util.rmi. 8 Implementation package stockshareapp.2nicastRemote@bject implements stockInterf { private EashtableFString& Arra$6istG stockEt " ne# Eashtable '((!. import java. public String getStatus ! thro#s java.Eashtable.server. .rmi. al.util. public String<= getShare7odes ! thro#s java. public stockImpl ! thro#s java.rmi.add description!.rmi.8 8 8 Interface package stockshareapp. class stockImpl e4tends java.Remote14ception. al.Remote14ception.1numeration.rmi.Remote { public double get7ur3rice String share7ode! thro#s java.rmi.Arra$6ist.Remote14ception { super !. public boolean bu$Shares String share7ode& int no@fRe?Share! thro#s java. public String get9escription String share7ode! thro#s java.Remote14ception.rmi.rmi.

8 public String<= getShare7odes !{ String s<= " ne# String<'(=.add start3rice!. 8 I@verride public int getNo@fShares String share7ode! { return Integer. al. i. 8 catch 14ception e! { S$stem. 1numeration e " stockEt.get share7ode!.ke$s !.out. 8 return true. 8 public void setNo@fShares String share7ode& int no@fRe?Share! { stockEt.toString !!.get share7ode!..get share7ode!. stockEt.get '!..println e.hasHore1lements !!{ s<i= " String. 8 I@verride public String get9escription String share7ode! { return stockEt. return false.value@f e.toString !!.add no@fShares!.get (!.parse9ouble stockEt. 8 return s.get *!. #hile e. if no@fRe?Share G noShare! { .ne4t1lement !!. 8 I@verride public double get7ur3rice String share7ode! { return 9ouble.set *& no@fRe?Share!. 8 I@verride public boolean bu$Shares String share7ode& int no@fRe?Share! { int noShare " getNo@fShares share7ode!.put share7ode& al!.al.toString !!.get share7ode!.parseInt stockEt.toString !. int i"(.

Add Ne# Share ).set '& cur3rice!. 8 public void setStatus String f!{ flag"f. % bought. 8 public String getStatus !{ return flag. 8 else { setNo@fShares share7ode& noShare A no@fRe?Share!.get share7ode!.%no@fShare"%. 8 return true.return false. Add Ne# Share ).. 8 public boolean set7ur3rice String share7ode& double cur3rice! { stockEt. return true. 14it Share "sid no@fShare".out. 8 8 Output: Server 0aiting . 1nter a choice'.no@fRe?Share . S$stem.%!. ) 1nter a Share7odesid 1nter a Ne# 3rice)(( 1nter a choice'.println %Share "%.. bought.share7ode . 2pdate Share 3rice . 2pdate Share 3rice *.

Bet 9escription *. 14it 1nter a choice for more information' 7urrent 3rice-)((. Bet No of Shares +. abc as sharecode *..( 1nter a choice for more information. sid as sharecode ). 14it Client 1nter an$ one sharecode'. Bet current priCe ). ank as sharecode . e4it 1nter a choice for sharecode' '. 1nter a choice for sharecodeJ .*.( 1nter a choice for more information) 9escription-siddh infotech 1nter a choice for more information+ 1nter No of Shares . Bu$ Shares . Successful 1nter a choice for more information' 7urrent 3rice-)((.. aks as sharecode +. kr as sharecode J.

( L732 #hich is idle//. cat top. cat top.N echo %Servicing Soft#are interrupts-%. cat top.( Servicing Soft#are interrupts(.( L732 spent b$ I.* L732 spent 6o# priorit$ user mode(.bash top Ab An ' G top. Calculate CPU Usa e Pro ram: K>.( .6.@ #aiting-%.t4t echo %L732 spent b$ user space application.@ #aiting(.+ L732 spent b$ s$stem(.t4t D grep 7pu D cut Ac .t4t D grep 7pu D cut Ac *JA*/ echo %L732 spent b$ I. cat top.t4t D grep 7pu D cut Ac N*AN.t4t D grep 7pu D cut Ac )MA*( echo %L732 #hich is idle-%.( steal time(.t4t D grep 7pu D cut Ac '(A') echo %L732 spent b$ s$stem-%.t4t D grep 7pu D cut Ac J+AJJ echo %steal time-%. cat top.t4t D grep 7pu D cut Ac +JA+M echo %Servicing Eard#are interrupts-%.%. cat top.A.t4t D grep 7pu D cut Ac '/A)' echo %L732 spent 6o# priorit$ user mode-%.bin.) Servicing Eard#are interrupts(. cat top. Output: L732 spent b$ user space application(. cat top..

hG Kinclude Fs$s. for c " RaR. char Qshm& Qs. e4it (!. e4it '!.ipc. Server Kinclude Fs$s. if shmid " shmget ke$& SEHSO& I37P7R1A: D (JJJ!! F (! { perror %shmget%!.hG Kinclude Fs$s.shm.ipc. ke$ " . 8 if shm " shmat shmid& N266& (!! "" char Q! A'! { perror %shmat%!.!.hG Kinclude Fstdio. int shmid.t$pes.hG Kinclude Fs$s. 8 s " shm.. c.hG Kdefine SEHSO )N . c F" RCR..JNM.t$pes. " c.hG Kinclude Fstdio. #hile Qshm >" RQR! sleep '!. e4it '!. 8 Client Kinclude Fs$s. Qs " R5(R.hG Kinclude Fs$s. Implement the Shared memor" se ment. ke$Pt ke$.! Qs.shm.hG Kdefine SEHSO )N main ! { char c.

8 if shm " shmat shmid& N266& (!! "" char Q! A'! { perror %shmat%!. e4it '!. Qs >" N266. s. 8 Output Server abcdefghijklmnop?rstuv#4$C Client Qbcdefghijklmnop?rstuv#4$C . ke$Pt ke$.main ! { int shmid. Qshm " RQR. if shmid " shmget ke$& SEHSO& (JJJ!! F (! { perror %shmget%!.! putchar Qs!. e4it '!. 8 for s " shm. e4it (!.JNM.. putchar R5nR!. ke$ " . char Qshm& Qs.