      *
      * @(#)sharemem.cbl	1.6
      ************************************************************************
      * Copyright (C) 1989-1999 Micro Focus International Ltd.
      * All rights reserved.
      *  
      * This demonstration program is provided for use by users of 
      * Micro Focus products (the "Software") and may be used, 
      * modified and distributed as part of your application provided 
      * that you properly acknowledge the copyright of Micro Focus in this
      * material.
      *     
      * All conditions, warranties and undertakings, express or implied, 
      * statutory or otherwise, on the part of Micro Focus are excluded, 
      * including without limitation those of merchantability or fitness  
      * for purpose. This shall not, however, purport to exclude or 
      * restrict the liability of Micro Focus to any extent not 
      * permitted by law.
      *
      * IN NO EVENT WILL MICRO FOCUS BE LIABLE FOR ANY INDIRECT, 
      * INCIDENTAL, SPECIAL, CONSEQUENTIAL OR OTHER DAMAGES ARISING OUT 
      * OF THE USE OF THE DEMONSTRATION PROGRAM DISTRIBUTED WITH THE 
      * SOFTWARE, WHETHER OR NOT INFORMED OF THE POSSIBILITIES OF 
      * DAMAGES IN ADVANCE.  THESE LIMITATIONS APPLY TO ALL CAUSES OF 
      * ACTION, INCLUDING BREACH OF CONTRACT, BREACH OF WARRANTY, STRICT
      * LIABILITY, AND ANY ACTIONS IN TORTS.
      ************************************************************************

      $if P64 set
      $ set vsc2 rtncode-size(8)
      $else
      $ set vsc2 rtncode-size(4)
      $end
      *
	identification division.
	program-id. sharemem.
      *
      * This program demonstrates the use of shared memory via COBOL
      * calling Unix functions. When this program is run 2 or more
      * times, each process will update a counter in shared memory.
      *
      * The demo can be animated or run. 
      * You need to create an executable for each COBOL program that includes
      * the necessary Unix support routines.
      * This can be done be entering:
      *
      * cob -xgv smstart.cbl -I shmctl -I semget -I semop -I shmat 
      * -I shmget smipc.c
      *
      * and
      *
      * cob -xgv sharemem.cbl -I shmctl -I semget -I semop -Ishmat
      * -I shmget smipc.c
      *
      * smstart.cbl initialises the semaphore and the shared memory and must
      * be run first on one of the terminals you are using. sharemem.cbl
      * can then be run or animated to demonstrate the use of shared memory.
      *
      * So once the programs are compiled if you want the animated demo
      * you will require at least 2 terminals to demo. On the 1st terminal
      * enter:
      *
      * smstart
      * COBSW=+A
      * export COBSW
      * sharemem
      *
      * and on all subsequent terminals enter:
      *
      * COBSW=+A
      * export COBSW
      * sharemem
      *
      * You can then step through each animation. Observe how access to
      * the shared memory is controlled by use of semaphores.
      *
      * Using the 'smdemo' script will allow you to run the demo directly on
      * a single terminal. Note though that smstart is still run first to
      * initialise the semaphore and the shared memory.
      * This will create 3 files called 'out1', 'out2', and 'out3'. These
      * files show how each process has updated the piece of shared memory.
      *
      * Optionally, you can pass an argument through to each running version
      * of 'sharemem'. This argument, if valid, controls the number of
      * iterations that the program attempts to update the shared memory.
      * Range of values possible are 1-99999.
      *
	copy "smwrks.cpy".

        procedure division.
        main section.
      *
      * Get number of iterations via command line.
      *
	    accept cmdln from command-line
	    if cmdln not = zeros
	    	move cmdln to iterations
	    end-if
      *
      * Get semaphore id
      *
	    perform cbl-semget
      *
      * Get the shared memory id and attach it to this process
      *
	    perform cbl-memget
      *
      * Access to the shared memory is controlled by the semaphore
      *
            perform until counter > iterations
      *
      * This is the 'critical section' which must have controlled access
      * otherwise, Program A could be in the middle of updating when
      * Program B comes along and messes things up.
      *
		perform cbl-aquire-sem
                if return-code <> -1
                    add 1 to counter
		    display "counter=" counter
      *
      * The end of the critical section is marked by the Semaphore clear
      *
		    perform cbl-release-sem
                end-if
      * 
      * Short delay to allow other processes the chance to update counter
      *
                call "sleep" using by value 1
            end-perform
      *
      * Close down nicely
      *

      *
      * Acquire semaphore so that no one else can remove it.
      *
      
            perform cbl-aquire-sem 

            if return-code <> -1
	        call "mysemctl" using by value
 			        cbl-semid zeroarg rmid zeroarg

	        call "shmctl" using by value cbl-shmid rmid zeroarg
            end-if 
            stop run.
      *
      * Generic routines
      *
	copy "smproc.cpy".
