Вы находитесь на странице: 1из 11

5.

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 java.io.BufferedReader; import java.io.InputStreamReader; import java.rmi.Naming; public class StockAppServer { stockImpl c; public StockAppServer ! { BufferedReader reader; reader " ne# BufferedReader ne# InputStreamReader S$stem.in!!; 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 !;

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

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

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

8 8 8 Interface package stockshareapp; public interface stockInterf e4tends java.rmi.Remote { public double get7ur3rice String share7ode! thro#s java.rmi.Remote14ception; public String get9escription String share7ode! thro#s java.rmi.Remote14ception; public int getNo@fShares String share7ode! thro#s java.rmi.Remote14ception; public boolean bu$Shares String share7ode& int no@fRe?Share! thro#s java.rmi.Remote14ception; public String<= getShare7odes ! thro#s java.rmi.Remote14ception; public String getStatus ! thro#s java.rmi.Remote14ception; 8

Implementation package stockshareapp; import java.util.Arra$6ist; import java.util.1numeration; import java.util.Eashtable; class stockImpl e4tends java.rmi.server.2nicastRemote@bject implements stockInterf { private EashtableFString& Arra$6istG stockEt " ne# Eashtable '((!; private String flag"null; public stockImpl ! thro#s java.rmi.Remote14ception { super !; 8 public boolean addShare String share7ode& String description& double cur3rice& double start3rice& int no@fShares! { tr$ { Arra$6ist al " ne# Arra$6ist '(!; al.add description!; al.add cur3rice!;

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

return false; 8 else { setNo@fShares share7ode& noShare A no@fRe?Share!; S$stem.out.println %Share "%;share7ode ;%no@fShare"%;no@fRe?Share ; % bought.%!; 8 return true; 8 public boolean set7ur3rice String share7ode& double cur3rice! { stockEt.get share7ode!.set '& cur3rice!; return true; 8 public void setStatus String f!{ flag"f; 8 public String getStatus !{ return flag; 8 8

Output: Server 0aiting ... 1nter a choice'. Add Ne# Share ). 2pdate Share 3rice *. 14it Share "sid no@fShare", bought. ) 1nter a Share7odesid 1nter a Ne# 3rice)(( 1nter a choice'. Add Ne# Share ). 2pdate Share 3rice

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

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

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

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

Вам также может понравиться