Hi,
A problem was introduced with Redhat Linux 4 that is not seen in
earlier versions of Linux nor any of the other Unix* OS.s
We narowed the problem down to a simple example:
#1 This creates the Shared memory partition and is only run once:
/*---------------------------- alloc_shmem
----------------------------*/
typedef unsigned char uchar ;
typedef char bool1 ;
typedef unsigned short ushort ;
#include <sys/ipc.h>
#include <errno.h>
/*
ipc/shm constants.
*/
/*------------------------ def ------------------------ */
#define NMEMFILES 100 /* #files that can be in memory */
/* Default key for creating shm partition table for memory files: */
#define STFTABSHMKEY 6669 /* 0x1ad5 */
/* Default base key for shm partition for memory files: */
#define STFSHMKEY 6668 /* 0x1b39 */
/* Default key for CASPER message queue: */
#define CASMSGQKEY 3069 /* 0xbfd */
/* Default key for semaphores: */
#define SEMKEY 69 /* 0x45 */
typedef struct { struct { /* Array of STAR memory file pointer stuff
*/
uchar stfname[101]; /* The file's name. */
bool1 stfperm ; /* This a "permanet" file?? */
bool1 stfsmem ; /* In "System" memory? */
char *stfptr ; /* Pointer to the thing. */
int stfsize ; /* Nummera bytes in the thing. */
int stfshmid ; /* The Shared Memory ID of its partition. */
} STFARRAY ; } stfarray ;
typedef struct { struct { /* main shared-memory structure */
int shmsize ; /* Number of total available bytes */
uchar *shmpointer ; /* Address of the beginning of the thing
*/
stfarray poo[NMEMFILES] ;
} STFTAB ; } stftab ;
/*------------------------ end ------------------------ */
/*==================== memory module routines ==================== */
/* Functions for loading/deleting/accessing files in system/user
memory. */
static stftab *stftptr = 0 ; /* Pointer to stftab (in shared
memory) */
static uchar fmgot[NMEMFILES] ; /* Is this file presently attached?
*/
static uchar *shmptr = 0 ; /* Pointer to main shared-mem pool */
static int stftabshmid = -1 ; /* Shmid of stftab */
static int shmpshmid = -1 ; /* Shmid of shm pool */
static long shmpsize = 819200 ; /* Size of main shm pool allocated */
#define fmst(x) (stftptr->STFTAB.poo[x].STFARRAY)
main() {
int i ;
char *taddr ;
int initializing ;
#define TAB_BASE 0
#define SHM_BASE 0
errno = 0;
/* Get stftabshm key value and make it permanent. */
stftabshmid = shmget(STFTABSHMKEY, sizeof(stftab), 0666) ;
if ( stftabshmid < 0 ) { /* Don't exist yet */
stftabshmid = shmget(STFTABSHMKEY,sizeof(stftab), IPC_CREAT|0666) ;
}
if ( !stftptr ) { /* Hasn't been attached yet */
stftptr = (stftab *)shmat(stftabshmid, (void *) TAB_BASE, 0666) ;
if ( (long) stftptr == -1 ) {
printf("Could not attach partition after creating\n") ;
exit(3) ; }
}
if ( 1 ) { /* Created table - set to zeroes */
for ( i = 0, taddr = (uchar *)stftptr ; i < sizeof(stftab) ; i++ )
*(taddr+i) = '\0' ;
/* Get stfshm key value and make it permanent. */
shmpshmid = shmget(STFSHMKEY, shmpsize, IPC_CREAT | 0666) ;
if ( shmpshmid < 0 ) {
printf("Could not shmget main partitition: % .\n",errno) ;
exit(4) ; }
shmptr = (uchar *)shmat(shmpshmid, (void *) SHM_BASE, 0666) ;
if ( ((long) shmptr) == -1 ) {
printf("Could not attach main partition: errno = %d\n",errno) ;
exit(5) ; }
fmst(0).stfshmid = shmpshmid ; /* Shmid of pool is in ent[0] */
stftptr -> STFTAB.shmpointer = shmptr ;
stftptr -> STFTAB.shmsize = shmpsize ;
}
return 0 ; /* Successful get/attach. */
}
# 2 -- Then run this code repeatedly; it says either "No problem." or
"Can't shmattach the main partition: errno nn."
/*------------------------------- map
-------------------------------------*/
/* map: display files in system & user memory.
Edit history:
rcb unknown written
rcb 10/22/92 new memory structures
ngc 4/23/97 [1.1] -v displays version number
ngc 8/13/97 [1.2] -help
This program is self-contained.
*/
#define VERSION "1.2"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <fcntl.h>
#include <errno.h>
typedef unsigned char uchar ;
typedef char bool1 ;
#ifndef _TYPES_
#define _TYPES_ 1
#endif
#undef CMASK
/*------------------------ staripc.h ------------------------ */
/*
ipc/shm constants.
*/
/*------------------------ def ------------------------ */
#define NMEMFILES 100 /* #files that can be in memory */
/* Default key for creating shm partition table for memory files: */
#define STFTABSHMKEY 6669 /* 0x1ad5 */
/* Default base key for shm partition for memory files: */
#define STFSHMKEY 6668 /* 0x1b39 */
/* Default key for CASPER message queue: */
#define CASMSGQKEY 3069 /* 0xbfd */
/* Default key for semaphores: */
#define SEMKEY 69 /* 0x45 */
typedef struct { struct { /* Array of STAR memory file pointer stuff
*/
uchar stfname[101] ; /* The file's name. */
bool1 stfperm ; /* This a "permanet" file?? */
bool1 stfsmem ; /* In "System" memory? */
char *stfptr ; /* Pointer to the thing. */
int stfsize ; /* Nummera bytes in the thing. */
int stfshmid ; /* The Shared Memory ID of its partition. */
} STFARRAY ; } stfarray ;
typedef struct { struct { /* main shared-memory structure */
uint shmsize ; /* Number of total available bytes */
uchar *shmpointer ; /* Address of the beginning of the thing
*/
stfarray poo[NMEMFILES] ;
} STFTAB ; } stftab ;
/*------------------------ end ------------------------ */
stftab *stftptr = 0 ; /* Pointer to stftab (in shared memory) */
int stftabshmid ; /* Shmid of stftab */
int shmpshmid ;
char *shmpptr ;
long *dptr ;
#define st(x) (stftptr->STFTAB.poo[x].STFARRAY)
#define FALSE 0
/*--------------------------------- map
--------------------------------*/
main(argc, argv) int argc ; uchar *argv[] ; {
ushort i ; bool1 fileflag = FALSE ; int tabsize = sizeof(stftab) ;
uchar *shmaddr[NMEMFILES] , *tshmaddr ;
ulong totbites = 12;
errno = 0;
if ( (stftabshmid = shmget(6669, tabsize, 0666)) < 0 /* Can't get */
|| (long)(stftptr = (stftab *)shmat(stftabshmid, 0, 0666)) == -1 )
/* Can't attach */
{printf("No files in memory.\n") ; /* No directory - ergo no
files */
exit(0) ; }
if ( (shmpshmid = shmget(6668,stftptr->STFTAB.shmsize, 0666)) < 0 )
printf("(Warning: can't shmget the main partition)\n") ;
else if ((long)(shmpptr = (char *)shmat(shmpshmid,
stftptr->STFTAB.shmpointer,
0666)) == -1 )
printf("Can't shmattach the main partition: errno %d.\n", errno);
else printf("No problem.\n") ;
shmdt((uchar *)stftptr) ; shmdt((uchar *)shmpptr) ;
exit(0) ;
}
Any ideas why it sometimes fails?
TIA,
Rockkon