*BSD News Article 49204


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.anu.edu.au!harbinger.cc.monash.edu.au!simtel!lll-winken.llnl.gov!uwm.edu!uwvax!newssinet!botan!idzumi
From: idzumi@shimane-u.ac.jp (Makoto Idzumi)
Newsgroups: comp.unix.bsd.freebsd.misc
Subject: Bug in shmget()???
Date: 21 Aug 1995 06:51:32 GMT
Organization: Shimane University, Shimane, Japan
Lines: 74
Message-ID: <419adk$k60@botan.shimane-u.ac.jp>
NNTP-Posting-Host: lev.shimane-u.ac.jp
X-Newsreader: mnews [version 1.18] 1994-05/16(Mon)

Hello.  I'm learning C programming on my FreeBSD 2.0R machine.  
When I run a simple program which uses shared memories, a strange
behaviour of a system call "shmget()" was observed when a flag 
IPC_EXCL was set.  Is it a bug in shmget()?  Or simply my
misunderstanding of it? 

The followings are a simple test program to check 
  shmget( shmkey, sizeof( some_data ), IPC_CREAT | IPC_EXCL | 0666 );
and a log when it runs.  Please look at them ...:

=== begin TEST PROGRAM =========
/****************************************************************/
/* shmtest -- to check how shmget(..IPC_CREAT | IPC_EXCL) works */
/*   Usage: shmtest shmkey                                      */
/****************************************************************/
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>

int main( int argc, char **argv )
{
  key_t shmkey;         /* shm KEY */
  int shmid;            /* shm ID */
  char some_data[4096];   /* dummy */

  if ( argc == 1 ) {
    (void) fprintf( stderr, "Usage: shmtest shmkey\n" );
    exit( 1 );
  }

  shmkey = atol( argv[1] );
  (void) printf( "shmkey = %ld\n", shmkey );
  shmid = shmget( shmkey, sizeof( some_data ), IPC_CREAT | IPC_EXCL | 0666 );

  /* should be EOF if error...(BUT!!!) */
  (void) printf( "shmid = %d\n", shmid );

  return( 0 );
}
=== end   TEST PROGRAM =========

=== begin LOG ==================
bash$ ipcs -m
Shared Memory:
T     ID     KEY        MODE       OWNER    GROUP
m 196608          1 --rw-rw-rw-   idzumi    wheel

bash$ ./shmtest 1
shmkey = 1
shmid = 196608     /* <--- Why not EOF ????? */
bash$ ./shmtest 2
shmkey = 2
shmid = 65537
bash$ ipcs -m
Shared Memory:
T     ID     KEY        MODE       OWNER    GROUP
m 196608          1 --rw-rw-rw-   idzumi    wheel
m  65537          2 --rw-rw-rw-   idzumi    wheel

bash$ 
=== end   LOG ==================

Additionally, two more questions: 
1. Why no definition of IPC_ALLOC (=0100000) in sys/ipc.h ?
2. Why no man pages on shmget(), shmat(), msgget(), ... ?

Thank you.
-----------
Makoto Idzumi     Dept.Math./Shimane Univ./Japan
idzumi@botan.shimane-u.ac.jp