home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 19995 < prev    next >
Encoding:
Internet Message Format  |  1993-01-28  |  24.1 KB

  1. Path: sparky!uunet!portal!lll-winken!taurus.cs.nps.navy.mil!taygeta.oc.nps.navy.mil!skip
  2. From: skip@taygeta.oc.nps.navy.mil (Skip Carter)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Objects in shared memory
  5. Message-ID: <C1HAwM.3qv@taurus.cs.nps.navy.mil>
  6. Date: 26 Jan 93 20:43:33 GMT
  7. References: <1993Jan19.082043.4341@dxcern.cern.ch>
  8. Sender: news@taurus.cs.nps.navy.mil
  9. Reply-To: skip@taygeta.oc.nps.navy.mil (Skip Carter)
  10. Organization: Naval Postgraduate School, Monterey
  11. Lines: 998
  12.  
  13.  
  14.     I tried to e-mail this but it bounced...
  15.  
  16. ---- Cut Here and unpack ----
  17. #!/bin/sh
  18. # shar:    Shell Archiver  (v1.22)
  19. #
  20. #                                                                          
  21. #                                                                          
  22. #
  23. #    Run the following text with /bin/sh to create:
  24. #      Classes.list
  25. #      Readme.sm
  26. #      Makefile
  27. #      sharmem.cpp
  28. #      sharmem.hpp
  29. #      persist.cpp
  30. #      smem.cpp
  31. #      smema.cpp
  32. #      smemb.cpp
  33. #
  34. if test -r s2_seq_.tmp
  35. then echo "Must unpack archives in sequence!"
  36.      next=`cat s2_seq_.tmp`; echo "Please unpack part $next next"
  37.      exit 1; fi
  38. echo "x - extracting Classes.list (Text)"
  39. sed 's/^X//' << 'SHAR_EOF' > Classes.list &&
  40. X
  41. X
  42. X    The following is a list of C++ classes that I have written and
  43. X    am distributing by request to those that want them.
  44. X
  45. X
  46. X    Derivs        Calculates the first derivative of a single
  47. X            parameter function by using Richardson Extrapolation.
  48. X            This class is a C++ translation of an Ada package
  49. X            given in the May 1991 issue of Computer Language .
  50. X
  51. X    Kalman        A general purpose Kalman filter class for optimal
  52. X            estimation of a dynamical system.
  53. X
  54. X
  55. X    Model, Map, Ode, Runge
  56. X
  57. X            A family of classes that allows the treatement of
  58. X            iterative maps and ordinary differential equations in
  59. X            a unified manner.  The ordinary differential equations
  60. X            are solved using the class Runge which is a C++ adaptation
  61. X            of the code given in:
  62. X            W.H. Press, B.P. Flannery, S.A. Teukolsky and W.T.
  63. X            Vetterling, Numerical Recipes: The Art of Scientific
  64. X            Computing, Cambridge Univ. Press, New York, 1986
  65. X            These classes were the example classes described in
  66. X            my recent Computer Language article:
  67. X            The Evolution of a C++ programmer, Computer Language,
  68. X            V. 9, No. 8, August 1992
  69. X
  70. X
  71. X    MonteIntegrator, Quasi
  72. X
  73. X             A Monte Carlo function integrator and its supporting
  74. X             random number generator.  The random number generator
  75. X             is a quasi-random (as opposed to a pseudo-random) number
  76. X             generator, this gives the integrator a 1/N convergence
  77. X                 rate (as opposed to 1/Sqrt(N) ). Can integrate functions
  78. X             up to 5 dimensions (this limit is due to the quasi random
  79. X             number generator).
  80. X
  81. X
  82. X
  83. X    Shared        A Unix Shared Memory object base class.  When this
  84. X            class is inherited by other classes then the derived
  85. X            class becomes a shared memory object that can be
  86. X            accessed by multiple processes.  These objects can be
  87. X            persistant.
  88. X
  89. X    SimAnneal    A general purpose Simulated Annealing class, for
  90. X            estimating optimum parameters of complicated functions
  91. X            or systems.
  92. X
  93. X
  94. X
  95. X
  96. X
  97. X Everett (Skip) Carter             Phone:  408-646-3318 FAX: 408-646-2712
  98. X Naval Postgraduate School         INTERNET: skip@taygeta.oc.nps.navy.mil
  99. X Dept. of Oceanography, Code OC/CR  UUCP:     ...!uunet!taygeta!skip
  100. X Monterey, CA. 93943               TELEMAIL: s.carter/omnet
  101. X
  102. X
  103. X
  104. X  (Note:  after 12 Dec 1992 the phone exchanges will change from 646 to 656)
  105. SHAR_EOF
  106. chmod 0644 Classes.list || echo "restore of Classes.list fails"
  107. echo "x - extracting Readme.sm (Text)"
  108. sed 's/^X//' << 'SHAR_EOF' > Readme.sm &&
  109. X    Shared memory/persistant objects
  110. X
  111. X    The class Shared is a limited shared memory object that will also
  112. X    allow persistant objects.
  113. X
  114. X    Each object is instantiated in a new shared memory segment which is
  115. X    a system wide (limited) resource.  If the object is not deleted, it
  116. X    will persist until the system is rebooted.  This can be exploited
  117. X    purposely in order to get persistant objects (as in persist.cpp).
  118. X
  119. X    Objects that contain pointers or references to other objects or
  120. X    variables that are not in shared memory will not work.  Also virtual
  121. X    methods cannot be used.
  122. X
  123. X
  124. X
  125. X    The following code demonstrates of the use of shared memory:
  126. X
  127. X    smem        A single program that creates child processes
  128. X            by forking.  The processes use shared memory
  129. X            for refering to a common object
  130. X
  131. X
  132. X    smema/smemb    Two independent programs that use shared memory
  133. X            for a common object.  Start smema first, then
  134. X            start smemb (which gets the handle info from a file
  135. X            created by smemb).  When semaphores are NOT used,
  136. X            start up smema in the background or from another window
  137. X            and immediately start smemb (smema just waits for a while
  138. X            and then assumes that there is valid data in the object).
  139. X
  140. X
  141. X
  142. X    persist    c    Creates an object in shared memory
  143. X
  144. X
  145. X    persist r    Reads (and removes an object in shared memory that
  146. X            was created with persist r).
  147. X
  148. X
  149. X
  150. X
  151. X
  152. X    See the comments in sharmem.hpp and in the demo code.
  153. X
  154. X
  155. X
  156. X
  157. X Everett (Skip) Carter             Phone:  408-646-3318 FAX: 408-646-2712
  158. X Naval Postgraduate School         INTERNET: skip@taygeta.oc.nps.navy.mil
  159. X Dept. of Oceanography, Code OC/CR  UUCP:     ...!uunet!taygeta!skip
  160. X Monterey, CA. 93943               TELEMAIL: s.carter/omnet
  161. X
  162. SHAR_EOF
  163. chmod 0644 Readme.sm || echo "restore of Readme.sm fails"
  164. echo "x - extracting Makefile (Text)"
  165. sed 's/^X//' << 'SHAR_EOF' > Makefile &&
  166. X# Makefile for misc C++ code
  167. X#
  168. XHOME = /usr/skip
  169. XCLIB = $(HOME)/clib
  170. XCPPLIB = $(HOME)/cpplib
  171. XCLASSDIR = $(HOME)/classlib
  172. X#
  173. X#
  174. X#
  175. XCC = cc
  176. XCFLAGS = -O -I$(HOME) -I.
  177. XCPFLAGS = $(CFLAGS)
  178. XCPP = CC
  179. X#
  180. X# The suffix for C++ code
  181. XC++ = c++
  182. X#
  183. X#
  184. XXFLAGS =  -I/usr/include/bsd -DXTFUNCPROTO
  185. XLFLAGS = -lm
  186. X#
  187. X#
  188. XXLIBS = -lXm -lXt -lX11 -lbsd
  189. X#
  190. X# use the appropriate version
  191. XSEMAPHORE =
  192. XSEMFLAG =
  193. X#
  194. X#SEMAPHORE = semaphore.o
  195. X#SEMFLAG = -DUSE_SEMAPHORES
  196. X#
  197. X#
  198. X# ===========================================================================
  199. X#
  200. X#
  201. XEXPORT_SOURCES = derivs.hpp derivs.cpp sharmem.cpp sharmem.hpp map.cpp model.cpp \
  202. X    ode.cpp runge.cpp map.hpp model.hpp ode.hpp runge.hpp quasi.cpp quasi.hpp \
  203. X    standio.cpp standio.hpp getargs.h getargs.c stoi.c monteint.hpp \
  204. X    monteint.cpp comment.cpp comment.hpp timestamp.hpp timestamp.cpp
  205. X#
  206. Xall:     derivkit sharmemkit modelkit mckit comkit
  207. X
  208. X#
  209. X# The test applications
  210. X#
  211. X#
  212. Xmctest: mctest.$(C++) monteint.o quasi.o standio.o getargs.o stoi.o
  213. X    $(CPP) $(CPFLAGS) -o mctest mctest.$(C++) monteint.o quasi.o standio.o getargs.o stoi.o -lm
  214. X
  215. Xmodriver: modriver.$(C++) model.o map.o ode.o runge.o
  216. X    $(CPP) $(CPFLAGS) -o modriver modriver.$(C++) model.o map.o ode.o runge.o -lm
  217. X
  218. Xsmem: smem.$(C++) sharmem.o $(SEMAPHORE)
  219. X    $(CPP) $(CPFLAGS) $(SEMFLAG) smem.$(C++) sharmem.o $(SEMAPHORE) -o smem
  220. X
  221. Xsmema: smema.$(C++) sharmem.o $(SEMAPHORE)
  222. X    $(CPP) $(CPFLAGS) $(SEMFLAG) smema.$(C++) sharmem.o $(SEMAPHORE) -o smema
  223. X
  224. Xsmemb: smemb.$(C++) sharmem.o $(SEMAPHORE)
  225. X    $(CPP) $(CPFLAGS) $(SEMFLAG) smemb.$(C++) sharmem.o $(SEMAPHORE) -o smemb
  226. X
  227. Xpersist: persist.$(C++) sharmem.o
  228. X    $(CPP) $(CPFLAGS) persist.$(C++) sharmem.o -o persist
  229. X
  230. X#
  231. X
  232. Xsigtest:
  233. X    $(CC) $(CFLAGS) -o sigtest sigtest.c
  234. X#
  235. X#
  236. X# ============================================================================
  237. X#
  238. X#
  239. Xclean:
  240. X    -rm -f $(EXPORT_SOURCES)
  241. X    -rm -f *.o *.bak *~
  242. X
  243. X#
  244. X#
  245. X#
  246. X#    The source distribution kits
  247. X#
  248. X#
  249. Xcomment.cpp: $(CLASSDIR)/comment.c++
  250. X    sed s/"<classlib\/"/"<"/g < $(CLASSDIR)/comment.c++ > comment.cpp
  251. X
  252. Xcomment.hpp: $(CLASSDIR)/comment.hpp
  253. X    cp $(CLASSDIR)/comment.hpp comment.hpp
  254. X
  255. Xderivs.cpp: $(CLASSDIR)/derivs.c++
  256. X    sed s/"<classlib\/"/"<"/g < $(CLASSDIR)/derivs.c++ > derivs.cpp
  257. X
  258. Xderivs.hpp: $(CLASSDIR)/derivs.hpp
  259. X    cp $(CLASSDIR)/derivs.hpp derivs.hpp
  260. X
  261. Xgetargs.c: $(CLIB)/getargs.c
  262. X    cp $(CLIB)/getargs.c getargs.c
  263. X
  264. Xgetargs.h: $(CLIB)/getargs.h
  265. X    cp $(CLIB)/getargs.h getargs.h
  266. X
  267. Xmap.hpp: $(CLASSDIR)/map.hpp
  268. X    sed s/"<classlib\/"/"<"/g < $(CLASSDIR)/map.hpp > map.hpp
  269. X
  270. Xmap.cpp: $(CLASSDIR)/map.c++
  271. X    sed s/"<classlib\/"/"<"/g < $(CLASSDIR)/map.c++ > map.cpp
  272. X
  273. Xmodel.hpp: $(CLASSDIR)/model.hpp
  274. X    cp $(CLASSDIR)/model.hpp model.hpp
  275. X
  276. Xmodel.cpp: $(CLASSDIR)/model.c++
  277. X    sed s/"<classlib\/"/"<"/g < $(CLASSDIR)/model.c++ > model.cpp
  278. X
  279. Xmonteint.hpp: $(CLASSDIR)/monteint.hpp
  280. X    sed s/"<classlib\/"/"<"/g < $(CLASSDIR)/monteint.hpp > monteint.hpp
  281. X
  282. Xmonteint.cpp: $(CLASSDIR)/monteint.c++
  283. X    sed s/"<classlib\/"/"<"/g < $(CLASSDIR)/monteint.c++ > monteint.cpp
  284. X
  285. Xode.hpp: $(CLASSDIR)/ode.hpp
  286. X    sed s/"<classlib\/"/"<"/g < $(CLASSDIR)/ode.hpp > ode.hpp
  287. X
  288. Xode.cpp: $(CLASSDIR)/ode.c++
  289. X    sed s/"<classlib\/"/"<"/g < $(CLASSDIR)/ode.c++ > ode.cpp
  290. X
  291. Xquasi.hpp: $(CLASSDIR)/quasi.hpp
  292. X    cp $(CLASSDIR)/quasi.hpp  quasi.hpp
  293. X
  294. Xquasi.cpp: $(CLASSDIR)/quasi.c++
  295. X    sed s/"<classlib\/"/"<"/g < $(CLASSDIR)/quasi.c++ > quasi.cpp
  296. X
  297. Xrunge.hpp: $(CLASSDIR)/runge.hpp
  298. X    sed s/"<classlib\/"/"<"/g < $(CLASSDIR)/runge.hpp > runge.hpp
  299. X
  300. Xrunge.cpp: $(CLASSDIR)/runge.c++
  301. X    sed s/"<classlib\/"/"<"/g < $(CLASSDIR)/runge.c++ > runge.cpp
  302. X
  303. X
  304. Xsharmem.hpp: $(CLASSDIR)/sharmem.hpp
  305. X    cp $(CLASSDIR)/sharmem.hpp sharmem.hpp
  306. X
  307. Xsharmem.cpp: $(CLASSDIR)/sharmem.c++
  308. X    sed s/"<classlib\/"/"<"/g < $(CLASSDIR)/sharmem.c++ > sharmem.cpp
  309. X
  310. Xstandio.hpp: $(CLASSDIR)/standio.hpp
  311. X    cp $(CLASSDIR)/standio.hpp  standio.hpp
  312. X
  313. Xstandio.cpp: $(CLASSDIR)/standio.c++
  314. X    cp $(CLASSDIR)/standio.c++ standio.cpp
  315. X
  316. Xstoi.c: $(CLIB)/stoi.c
  317. X    cp $(CLIB)/stoi.c stoi.c
  318. X
  319. Xtimestamp.cpp: $(CLASSDIR)/timestamp.c++
  320. X    sed s/"<classlib\/"/"<"/g < $(CLASSDIR)/timestamp.c++ > timestamp.cpp
  321. X
  322. Xtimestamp.hpp: $(CLASSDIR)/timestamp.hpp
  323. X    cp $(CLASSDIR)/timestamp.hpp timestamp.hpp
  324. X
  325. XSHAR_SOURCE_CODE = Readme.sm Makefile sharmem.cpp sharmem.hpp persist.cpp smem.cpp \
  326. X          smema.cpp smemb.cpp
  327. X
  328. XMODEL_SOURCE_CODE = Makefile map.cpp map.hpp model.cpp model.hpp ode.cpp ode.hpp \
  329. X        runge.cpp runge.hpp modriver.cpp
  330. X
  331. XMC_SOURCE_CODE = Readme.mc Makefile monteint.hpp monteint.cpp quasi.hpp quasi.cpp \
  332. X    standio.cpp standio.hpp getargs.h getargs.c stoi.c mctest.cpp
  333. X#
  334. X#
  335. Xderivkit: derivs.sh01
  336. X
  337. Xsharmemkit: sharmem.sh01
  338. X
  339. Xmodelkit: model.sh01
  340. X
  341. Xmckit: monte.sh01
  342. X
  343. Xcomkit: comkit.sh01
  344. X
  345. Xderivs.sh01: derivs.hpp derivs.cpp
  346. X    shar2 -v -c -l64 -oderivs.sh Classes.list derivs.hpp derivs.cpp
  347. X
  348. Xsharmem.sh01: $(SHAR_SOURCE_CODE)
  349. X    shar2 -v -c -l64 -osharmem.sh Classes.list $(SHAR_SOURCE_CODE)
  350. X
  351. Xmodel.sh01: $(MODEL_SOURCE_CODE)
  352. X    shar2 -v -c -l64 -omodel.sh Classes.list $(MODEL_SOURCE_CODE)
  353. X
  354. Xmonte.sh01: $(MC_SOURCE_CODE)
  355. X    shar2 -v -c -l64 -omonte.sh Classes.list $(MC_SOURCE_CODE)
  356. X
  357. Xcomkit.sh01: comment.hpp comment.cpp timestamp.hpp timestamp.cpp challenge1.cpp
  358. X    shar2 -v -c -l64 -ocomkit.sh comment.hpp comment.cpp timestamp.hpp \
  359. X                        timestamp.cpp challenge1.cpp
  360. X
  361. X#
  362. X# Generics
  363. X#
  364. X#.asm.o:
  365. X#    masm  $< /ML;
  366. X# 
  367. X.SUFFIXES :
  368. X.SUFFIXES : .cpp .cxx .cpp .c .h .o
  369. X
  370. X.cpp.o:
  371. X    $(CPP) $(CPFLAGS) -c $*.cpp
  372. X
  373. X.cxx.o:
  374. X    $(CPP) $(CPFLAGS) -c $*.cxx
  375. X
  376. X.c++.o:
  377. X    $(CPP) $(CPFLAGS) -c $*.c++
  378. X
  379. X.c.o:
  380. X    $(CC) $(CFLAGS) -c $*.c
  381. X
  382. SHAR_EOF
  383. chmod 0666 Makefile || echo "restore of Makefile fails"
  384. echo "x - extracting sharmem.cpp (Text)"
  385. sed 's/^X//' << 'SHAR_EOF' > sharmem.cpp &&
  386. X// sharmem.cpp        A Shared Memory Object base class implementation
  387. X
  388. Xstatic const char rcsid[] = "@(#)sharmem.c++    1.2 13:58:22 11/9/92   EFC";
  389. X
  390. X#include <iostream.h>
  391. X
  392. X#include <sharmem.hpp>
  393. X
  394. Xextern int errno;
  395. X
  396. Xvoid* Shared::operator new(size_t blocksize)
  397. X{
  398. X        Shared *rtn;
  399. X    int shmsize, shmid;
  400. X    
  401. X
  402. X    shmsize = blocksize;
  403. X         shmid = shmget( IPC_PRIVATE, shmsize, (IPC_CREAT | 0600) );
  404. X         if ( shmid == -1 )
  405. X          {
  406. X              cerr << "Shared::new() shared memory get (shmget) failed (" <<
  407. X            errno << ")\n",
  408. X              exit(1);
  409. X    }
  410. X
  411. X
  412. X
  413. X         if ( (int)(rtn = (Shared *)shmat( shmid, 0, SHM_RND) ) == -1 )
  414. X         {
  415. X            cerr << "Shared::new() (1) shared memory attach (shmat) error (" <<
  416. X                errno << ")\n";
  417. X        exit(1);
  418. X    }
  419. X
  420. X    rtn->shmid = shmid;
  421. X    rtn->ref_count = 1;
  422. X
  423. X    return rtn;
  424. X
  425. X}
  426. X
  427. Xvoid* Shared::operator new(size_t, int master_is)
  428. X{
  429. X        Shared *rtn;
  430. X
  431. X         if ( (int)(rtn = (Shared *)shmat( master_is, 0, SHM_RND) ) == -1 )
  432. X         {
  433. X            cerr << "Shared::new() (2) shared memory attach (shmat) error (" <<
  434. X                errno << ")\n";
  435. X        exit(1);
  436. X    }
  437. X
  438. X    rtn->ref_count++;
  439. X
  440. X    return rtn;
  441. X
  442. X}
  443. X
  444. Xvoid Shared::operator delete(void *p)
  445. X{
  446. X    Shared* ps = (Shared *)p;
  447. X
  448. X    if ( ps->ref_count-- == 1 )
  449. X           shmctl( ps->shmid, IPC_RMID, NULL);
  450. X    else
  451. X        shmdt( p );
  452. X}
  453. X
  454. X
  455. X
  456. SHAR_EOF
  457. chmod 0644 sharmem.cpp || echo "restore of sharmem.cpp fails"
  458. echo "x - extracting sharmem.hpp (Text)"
  459. sed 's/^X//' << 'SHAR_EOF' > sharmem.hpp &&
  460. X// sharmem.hpp        A Shared Memory Object base class definition
  461. X
  462. X// To use this class, an otherwise ordinary object can publicly inherit from
  463. X// this class, one process, the "master", can treat the object in a normal way
  464. X// all the other processes, the "clients", must declare the object as a pointer
  465. X// and instantiate the object with the special version of new that requires the
  466. X// shared memory block ID as a parameter
  467. X
  468. X
  469. X// rcsid: @(#)sharmem.hpp    1.2 13:58:10 11/9/92 EFC
  470. X
  471. X#ifndef _SHAR_MEM_HPP_
  472. X#define _SHAR_MEM_HPP_ 1.2
  473. X
  474. X#include <stdlib.h>
  475. X
  476. X#include <sys/types.h>
  477. X#include <sys/ipc.h>
  478. X#include <sys/shm.h>
  479. X
  480. Xclass Shared
  481. X{
  482. X    private:
  483. X      int shmid, ref_count;
  484. X    protected:
  485. X      Shared() : id(shmid), ref_count(0) {}
  486. X    public:
  487. X      const int& id;                // shared memory block id
  488. X      void *operator new(size_t blocksize);        // used by the master
  489. X      void *operator new(size_t,int master_is);    // used by clients
  490. X      void operator delete(void *);
  491. X};
  492. X
  493. X#endif
  494. SHAR_EOF
  495. chmod 0444 sharmem.hpp || echo "restore of sharmem.hpp fails"
  496. echo "x - extracting persist.cpp (Text)"
  497. sed 's/^X//' << 'SHAR_EOF' > persist.cpp &&
  498. X// persist.cpp    Test using shared memory objects as persistant objects
  499. X
  500. X// usage    persist c        create the objects
  501. X//        persist r        read (and remove) objects
  502. X
  503. X#define PROGRAM "persist"
  504. X
  505. X#include <fstream.h>
  506. X#include <stdlib.h>
  507. X#include <sysent.h>        // for fork(), may be elsewhere on other machines
  508. X
  509. X#include "sharmem.hpp"
  510. X
  511. X
  512. Xextern int errno;
  513. X
  514. Xclass Cube : public Shared
  515. X{
  516. X    private:
  517. X      int x, y, z;
  518. X    public:
  519. X      Cube() {}
  520. X      Cube(int a, int b, int c) : x(a), y(b), z(c) {}
  521. X     ~Cube() {}
  522. X      void size(int a, int b, int c) { x = a; y = b; z = c; }
  523. X      int volume() { return x * y * z; }
  524. X};
  525. X
  526. X
  527. Xvoid main(int argc, char **argv)
  528. X{
  529. X     int shmid, cubeid;
  530. X     Cube *cube;
  531. X     Cube *image;
  532. X
  533. X    if ( argc < 2 )
  534. X    {
  535. X    cerr << "Usage:\n\t" << PROGRAM << " c\t\tto create new objects\n";
  536. X    cerr << '\t' << PROGRAM << " r\t\tto read and remove objects\n";
  537. X    exit(1);
  538. X    }
  539. X
  540. X     if ( *argv[1] == 'c' )
  541. X     {
  542. X
  543. X         image = new Cube;
  544. X         cube = new Cube;
  545. X
  546. X         shmid = image->id;    // need this so that the shared one corresponds
  547. X         cubeid = cube->id;
  548. X
  549. X
  550. X         ofstream fout( "keys.dat" );
  551. X         if ( !fout )
  552. X         {
  553. X        cerr << PROGRAM << " unable to open 'keys.dat' for output\n";
  554. X        exit(1);
  555. X         }
  556. X
  557. X
  558. X         fout << shmid << '\n' << cubeid << '\n';
  559. X
  560. X    fout.close();
  561. X
  562. X        image->size(2,3,4);
  563. X
  564. X        cube->size(5,6,7);
  565. X    }
  566. X    else if ( *argv[1] == 'r' )
  567. X    {
  568. X
  569. X             ifstream fin( "keys.dat" );
  570. X             if ( !fin )
  571. X             {
  572. X            cerr << PROGRAM << " unable to open 'keys.dat' for input\n";
  573. X            exit(1);
  574. X             }
  575. X
  576. X             fin >> shmid >> cubeid;
  577. X
  578. X
  579. X        fin.close();
  580. X
  581. X              cerr << "shmid: " << shmid << "\tcubeid: " << cubeid << endl;
  582. X
  583. X             image = new(shmid) Cube;
  584. X             cube =  new(cubeid) Cube;
  585. X
  586. X    }
  587. X    else
  588. X    {
  589. X
  590. X    cerr << "Usage:\n\t" << PROGRAM << "c\t\tto create new objects\n";
  591. X    cerr << '\t' << PROGRAM << "r\t\tto read and remove objects\n";
  592. X    exit(1);
  593. X
  594. X    }
  595. X
  596. X
  597. X    
  598. X    cout << "The cube volume is: " << image->volume() << endl;
  599. X
  600. X    cout << "The 2nd cube volume is: " << cube->volume() << endl;
  601. X
  602. X
  603. X    // NOTE: If you don't do this, you will eventually consume ALL
  604. X    //       the systems shared memory resources
  605. X
  606. X     if ( *argv[1] == 'r' )
  607. X     {
  608. X         delete image;
  609. X         delete cube;
  610. X     }
  611. X
  612. X}
  613. X
  614. X
  615. X
  616. X
  617. X
  618. SHAR_EOF
  619. chmod 0666 persist.cpp || echo "restore of persist.cpp fails"
  620. echo "x - extracting smem.cpp (Text)"
  621. sed 's/^X//' << 'SHAR_EOF' > smem.cpp &&
  622. X// smem.cpp    Test using shared memory objects (and optionally semaphores)
  623. X//        the synchronization WITHOUT semaphores will only work for simple
  624. X//        programs (like this one), using SEMAPHORES is the preferred way
  625. X//        to go.
  626. X
  627. X#define PROGRAM "smem"
  628. X
  629. X#include <iostream.h>
  630. X#include <stdlib.h>
  631. X#include <sysent.h>            // for fork()
  632. X
  633. X#include "sharmem.hpp"
  634. X
  635. X// #define USE_SEMAPHORES
  636. X
  637. X#ifdef USE_SEMAPHORES
  638. X#include "semaphore.hpp"
  639. X
  640. X#define NSEM        1           /* set to the number of semaphores to use */
  641. X
  642. X#define SEM1        0
  643. X#define SEM2        (NSEM-1)
  644. X
  645. X#endif
  646. X
  647. Xclass Cube : public Shared
  648. X{
  649. X    private:
  650. X      int x, y, z;
  651. X    public:
  652. X      Cube() {}
  653. X      Cube(int a, int b, int c) : x(a), y(b), z(c) {}
  654. X     ~Cube() {}
  655. X      void size(int a, int b, int c) { x = a; y = b; z = c; }
  656. X      int volume() { return x * y * z; }
  657. X};
  658. X
  659. X
  660. XCube *image;
  661. X
  662. Xint semid = 1234;
  663. X
  664. Xvoid main()
  665. X{
  666. X     int child;
  667. X     int shmid, cubeid;
  668. X     Cube *cube;
  669. X
  670. X     image = new Cube;
  671. X     cube =  new Cube;
  672. X
  673. X     shmid = image->id;        // need this so that the shared one corresponds
  674. X     cubeid = cube->id;
  675. X
  676. X#ifdef USE_SEMAPHORES
  677. X     semaphore sem(semid, NSEM, 0);
  678. X#endif
  679. X
  680. X     if ( fork() == 0 )
  681. X       {
  682. X        delete image;        // get rid of childs copy
  683. X
  684. X        image = new(shmid) Cube;    // attach to shared copy of image
  685. X
  686. X#ifdef USE_SEMAPHORES
  687. X          semaphore sem(semid, NSEM, 0);
  688. X#endif
  689. X
  690. X        child = getpid();
  691. X        cout << "I am the child process: " << child;
  692. X        image->size(2,3,4);
  693. X        cout << "\tThe cube volume is: " << image->volume() << endl;
  694. X
  695. X        delete image;
  696. X
  697. X        sleep(1);
  698. X#ifdef USE_SEMAPHORES
  699. X    cerr << "(C1) Semaphore values: " << sem.value(SEM1) << '\n';
  700. X          sem[SEM1]++;
  701. X    cerr << "(C1) Semaphore values: " << sem.value(SEM1) << '\n';
  702. X#endif
  703. X          exit( 0 );
  704. X        }
  705. X
  706. X     if ( fork() == 0 )
  707. X       {
  708. X
  709. X        delete image;        // get rid of childs copy
  710. X
  711. X        image = new(cubeid) Cube;    // attach to shared copy of cube
  712. X#ifdef USE_SEMAPHORES
  713. X             semaphore sem(semid,NSEM,0);
  714. X#endif
  715. X
  716. X        child = getpid();
  717. X        cout << "I am the child process: " << child;
  718. X        image->size(5,6,7);
  719. X        cout << "\tThe cube volume is: " << image->volume() << endl;
  720. X
  721. X
  722. X        delete image;
  723. X
  724. X#ifdef USE_SEMAPHORES
  725. X    cerr << "(C2) Semaphore values: " << sem.value(SEM2) << '\n';
  726. X          sem[SEM2]++;
  727. X    cerr << "(C2) Semaphore values: " << sem.value(SEM2) << '\n';
  728. X#endif
  729. X          exit( 0 );
  730. X        }
  731. X
  732. X    // parent does this
  733. X
  734. X    // sleep( 3 );
  735. X
  736. X#ifdef USE_SEMAPHORES
  737. X    cerr << "(P) Semaphore values: " << sem.value(0) << '\n';
  738. X    cerr << "Decrementing the semaphore the first time\n";
  739. X    sem[SEM1]--;
  740. X    cerr << "Decrementing the semaphore the second time\n";
  741. X    sem[SEM2]--;
  742. X#else
  743. X    int status;
  744. X    child = wait(&status);
  745. X    child = wait(&status);
  746. X    cout << "The last child was: " << child << endl;
  747. X#endif
  748. X
  749. X    cout << "The cube volume is: " << image->volume() << endl;
  750. X
  751. X    cout << "The 2nd cube volume is: " << cube->volume() << endl;
  752. X
  753. X
  754. X    // NOTE: If you don't do this, you will eventually consume ALL
  755. X    //       the systems shared memory resources
  756. X
  757. X    delete image;
  758. X    delete cube;
  759. X
  760. X}
  761. X
  762. X
  763. X
  764. X
  765. X
  766. SHAR_EOF
  767. chmod 0666 smem.cpp || echo "restore of smem.cpp fails"
  768. echo "x - extracting smema.cpp (Text)"
  769. sed 's/^X//' << 'SHAR_EOF' > smema.cpp &&
  770. X// smema.cpp    Test using shared memory objects (and optionally semaphores)
  771. X//    two independent programs are used, this one creates the space and
  772. X//    writes the keys to a file, the second one reads the keys and access the
  773. X//    data
  774. X
  775. X#define PROGRAM "smemA"
  776. X
  777. X#include <fstream.h>
  778. X#include <stdlib.h>
  779. X#include <sysent.h>            // for fork()
  780. X
  781. X#include "sharmem.hpp"
  782. X
  783. X// #define USE_SEMAPHORES
  784. X
  785. X#ifdef USE_SEMAPHORES
  786. X#include "semaphore.hpp"
  787. X
  788. X#define NSEM        1           /* set to the number of semaphores to use */
  789. X
  790. X#define SEM1        0
  791. X#define SEM2        (NSEM-1)
  792. X
  793. X#endif
  794. X
  795. X
  796. Xclass Cube : public Shared
  797. X{
  798. X    private:
  799. X      int x, y, z;
  800. X    public:
  801. X      Cube() {}
  802. X      Cube(int a, int b, int c) : x(a), y(b), z(c) {}
  803. X     ~Cube() {}
  804. X      void size(int a, int b, int c) { x = a; y = b; z = c; }
  805. X      int volume() { return x * y * z; }
  806. X};
  807. X
  808. X
  809. Xint semid = 1234;
  810. X
  811. Xvoid main()
  812. X{
  813. X     int shmid, cubeid;
  814. X     Cube *cube;
  815. X     Cube *image;
  816. X
  817. X
  818. X     image = new Cube;
  819. X     cube = new Cube;
  820. X
  821. X     shmid = image->id;        // need this so that the shared one corresponds
  822. X     cubeid = cube->id;
  823. X
  824. X#ifdef USE_SEMAPHORES
  825. X     semaphore sem(semid, NSEM, 0);
  826. X#endif
  827. X
  828. X     ofstream fout( "keys.dat" );
  829. X     if ( !fout )
  830. X     {
  831. X    cerr << PROGRAM << " unable to open 'keys.dat' for output\n";
  832. X    exit(1);
  833. X     }
  834. X
  835. X
  836. X     fout << shmid << '\n' << cubeid << '\n' << semid << '\n';
  837. X
  838. X    fout.close();
  839. X
  840. X
  841. X    // sleep( 3 );
  842. X
  843. X#ifdef USE_SEMAPHORES
  844. X    for (int i = 0; i < NSEM; i++)
  845. X        cerr << "(P) Semaphore values: " << sem.value(i) << '\n';
  846. X    cerr << "Decrementing the semaphore the first time\n";
  847. X    sem[SEM1]--;
  848. X    for (i = 0; i < NSEM; i++)
  849. X        cerr << "(P) Semaphore values: " << sem.value(i) << '\n';
  850. X    cerr << "Decrementing the semaphore the second time\n";
  851. X    sem[SEM2]--;
  852. X    for (i = 0; i < NSEM; i++)
  853. X        cerr << "(P) Semaphore values: " << sem.value(i) << '\n';
  854. X#else
  855. X                // no semaphores so,
  856. X    sleep( 5 );        // just wait a while and hope the obect is ready
  857. X
  858. X#endif
  859. X
  860. X    cout << "A:  The cube volume is: " << image->volume() << endl;
  861. X
  862. X    cout << "A:  The 2nd cube volume is: " << cube->volume() << endl;
  863. X
  864. X
  865. X    // NOTE: If you don't do this, you will eventually consume ALL
  866. X    //       the systems shared memory resources
  867. X
  868. X     delete image;
  869. X     delete cube;
  870. X
  871. X}
  872. X
  873. X
  874. X
  875. X
  876. X
  877. SHAR_EOF
  878. chmod 0666 smema.cpp || echo "restore of smema.cpp fails"
  879. echo "x - extracting smemb.cpp (Text)"
  880. sed 's/^X//' << 'SHAR_EOF' > smemb.cpp &&
  881. X// smemb.cpp    Test using shared memory objects (and optionally semaphores)
  882. X//    two independent programs are used, this one
  883. X//    reads the keys and access the data
  884. X
  885. X#define PROGRAM "smemB"
  886. X
  887. X
  888. X#include <fstream.h>
  889. X#include <stdlib.h>
  890. X#include <sysent.h>            // for fork()
  891. X
  892. X#include "sharmem.hpp"
  893. X
  894. X// #define USE_SEMAPHORES
  895. X
  896. X#ifdef USE_SEMAPHORES
  897. X#include "semaphore.hpp"
  898. X
  899. X#define NSEM        1           /* set to the number of semaphores to use */
  900. X
  901. X#define SEM1        0
  902. X#define SEM2        (NSEM-1)
  903. X
  904. X#endif
  905. X
  906. Xclass Cube : public Shared
  907. X{
  908. X    private:
  909. X      int x, y, z;
  910. X    public:
  911. X      Cube() {}
  912. X      Cube(int a, int b, int c) : x(a), y(b), z(c) {}
  913. X     ~Cube() {}
  914. X      void size(int a, int b, int c) { x = a; y = b; z = c; }
  915. X      int volume() { return x * y * z; }
  916. X};
  917. X
  918. X
  919. XCube *image;
  920. X
  921. X
  922. Xvoid main()
  923. X{
  924. X     int child;
  925. X     int shmid, cubeid, semid;
  926. X     Cube *cube;
  927. X
  928. X     ifstream fin( "keys.dat" );
  929. X     if ( !fin )
  930. X     {
  931. X    cerr << PROGRAM << " unable to open 'keys.dat' for input\n";
  932. X    exit(1);
  933. X     }
  934. X
  935. X     fin >> shmid >> cubeid >> semid;
  936. X
  937. X
  938. X    fin.close();
  939. X
  940. X      cerr << "shmid: " << shmid << "\tcubeid: " << cubeid
  941. X         << "\tsemid: " << semid << endl;
  942. X
  943. X     image = new(shmid) Cube;
  944. X     cube =  new(cubeid) Cube;
  945. X
  946. X
  947. X#ifdef USE_SEMAPHORES
  948. X     semaphore sem(semid, NSEM, 0);
  949. X#endif
  950. X
  951. X
  952. X        child = getpid();
  953. X        cout << "I am the child process: " << child;
  954. X        image->size(2,3,4);
  955. X        cout << "\tThe cube volume is: " << image->volume() << endl;
  956. X
  957. X        delete image;
  958. X
  959. X        sleep(1);
  960. X#ifdef USE_SEMAPHORES
  961. X    cerr << "(C1) Semaphore values: " << sem.value(SEM1) << '\n';
  962. X          sem[SEM1]++;
  963. X    cerr << "(C1) Semaphore values: " << sem.value(SEM1) << '\n';
  964. X#endif
  965. X
  966. X
  967. X        child = getpid();
  968. X        cout << "B: I am the second process: " << child;
  969. X        cube->size(5,6,7);
  970. X        cout << "\tThe cube volume is: " << cube->volume() << endl;
  971. X
  972. X
  973. X        delete cube;
  974. X
  975. X#ifdef USE_SEMAPHORES
  976. X    cerr << "(C2) Semaphore values: " << sem.value(SEM2) << '\n';
  977. X          sem[SEM2]++;
  978. X    cerr << "(C2) Semaphore values: " << sem.value(SEM2) << '\n';
  979. X#endif
  980. X
  981. X
  982. X
  983. X}
  984. X
  985. X
  986. X
  987. X
  988. X
  989. SHAR_EOF
  990. chmod 0666 smemb.cpp || echo "restore of smemb.cpp fails"
  991. exit 0
  992.  
  993. -- 
  994.  Everett (Skip) Carter             Phone:  408-656-3318 FAX: 408-656-2712
  995.  Naval Postgraduate School         INTERNET: skip@taygeta.oc.nps.navy.mil
  996.  Dept. of Oceanography, Code OC/CR  UUCP:     ...!uunet!taygeta!skip
  997.  Monterey, CA. 93943               TELEMAIL: s.carter/omnet
  998.  
  999.  (please note the new phone numbers)
  1000.  
  1001.  
  1002.  
  1003.  
  1004. -- 
  1005.  Everett (Skip) Carter        Phone:  408-656-3318 FAX: 408-656-2712
  1006.  Naval Postgraduate School    INTERNET: skip@taygeta.oc.nps.navy.mil
  1007.  Dept. of Oceanography, Code OC/CR  UUCP:     ...!uunet!taygeta!skip
  1008.  Monterey, CA. 93943          TELEMAIL: s.carter/omnet
  1009.  
  1010.  (please note the new phone numbers)
  1011.