Article 168361 of comp.os.vms:
This is a multi-part message in MIME format.

--------------7878571C716FCF257956A8FA
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Jeff Nelson wrote:
> 
> Summary
> =======
> I need help figuring out how to open a single file for write access so
> that multiple processes can append timestamped records to the end of
> the file and have them appear in the file in the order in which they
> were written.
>

--------------7878571C716FCF257956A8FA
Content-Type: text/plain; charset=us-ascii; name="b.b"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="b.b"

  As it was mentioned in this notes thread, what you want to do can be
  easily done on RMS level.

  While using direct RMS calls is always a good idea, you can accomplish
  your task with the following DEC C RTL open() call:

    if ( ( fd = open("file.log", O_CREAT | O_WRONLY | O_APPEND, 0,
		     "ctx=rec", "shr=get,put", "rop=rea") ) == -1 ) {
        perror("open");
        ....
    }
  
  You should not specify "shr=upi" option which disables all RMS locking
  and assumes, that the user takes responsibility for synchronization of
  multiple, simultaneous accessors of a file. What you need is to specify
  some RMS record locking option (RAB$V_REA in the example above) which
  would allow other accessors to read the record. This is necessary to
  proper positioning shared file opening for update to end-of-file on each
  write.

  Run the example below from several processes and look at the log file.

  Boris

#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unixio.h>

main( int argc, char **argv )
{
  int fd;
  time_t timer;
  char buffer[80];
  int len, i;

  if ( argc != 2 ) {
    puts("usage: example process_name");
    exit(EXIT_FAILURE);
  }

  if ( ( fd = open("file.log", O_CREAT | O_WRONLY | O_APPEND, 0,
		   "ctx=rec", "shr=get,put", "rop=rea") ) == -1 ) {
    perror("open");
    exit(EXIT_FAILURE);
  }

  for(i=0; i < 100; i++) {
    time_t timer = time(NULL);
    sprintf(buffer, "Process %s: %s", argv[1], ctime(&timer));
    len = strlen(buffer);
    if ( write(fd, buffer, len ) != len ) {
      perror("write");
      exit(EXIT_FAILURE);
    }
    sleep(1);
  }

  exit(EXIT_SUCCESS);
}

| All the opinions voiced are mine alone and do not reflect anyone else's |
| opinion, including my employer's.                                       |

--------------7878571C716FCF257956A8FA--



