Article View: comp.sys.sun
Article #7558Share memory.
From: Peter-Son Nguyen
Date: Wed, 24 Apr 1991 00:00
Date: Wed, 24 Apr 1991 00:00
153 lines
3912 bytes
3912 bytes
I have a problem with detaching a share memory segment. I've included
the source code for the client and server programs. The server program
must be run first. The client program then will be run. The problem
occurs in the client program. In the client program, I just could not
detach the share memory segment even though attaching was possible. I am
open for suggestion. Thanks.
--------
Server.c
--------
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
static int shmid = -1;
static void bailout()
{
(void) printf("<server>: bailing out.\n");
if (shmid >= 0) {
if (shmctl(shmid, IPC_RMID, (struct shmid_ds *) 0) < 0)
(void) printf("<bailout>: Can't remove the memory segment.\n");
}
exit(-1);
}
sx
main()
{
int i, j;
unsigned char * _buf;
int bytes_total = 1000;
signal( SIGHUP, bailout );
signal( SIGINT, bailout );
signal( SIGTERM, bailout );
shmid = shmget( ( (key_t) 0xabc ), bytes_total,
( 0666 | IPC_CREAT | IPC_EXCL ) );
if ( shmid < 0 )
{
(void) printf( "<server>: Can't create shared memory.\n" );
exit( 1 );
}
_buf = (unsigned char *) shmat( shmid, (char *) 0, ~SHM_RDONLY );
if ( ( !_buf ) || ( _buf == ((unsigned char *) -1) ) )
{
(void) printf( "<server>: Can't attach memory.\n" );
(void) shmctl( shmid, IPC_RMID, (struct shmid_ds *) 0 );
exit( 1 );
}
for ( i = 0; i < 10; i++ )
{
for ( j = 0; j < 3; j++ )
{
/* Fill the share memory segments with data */
/* Source deleted ... */
/* Pause */
(void) printf( "Sleep for 1 second.\n" );
sleep( 1 );
(void) printf( "Done sleeping.\n" );
}
}
if ( shmdt( (char *) _buf ) < 0 )
(void) printf( "<server>: Can't detach memory.\n" );
if ( shmctl( shmid, IPC_RMID, (struct shmid_ds *) 0 ) < 0 )
(void) printf( "<server>: Can't remove the memory segment.\n" );
}
Client.c:
--------
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
extern char * shmat();
static int attach_detach()
{
char * _p = ((char *) 0);
int shmid = -1, status;
int bytes_allocated;
/* Calculate the total bytes allocated */
bytes_allocated = 1000;
/* Get shared memory segment id */
shmid = shmget( (key_t) 0xabc, bytes_allocated, ( 0444 | IPC_ALLOC ) );
if ( shmid < 0 )
{
(void) printf( "<client>: Can't do shmget for reading.\n" );
return( 0 );
}
else
{
(void) printf( "<client>: Shmid = %d\n", shmid );
}
/* Attaching to this segment */
_p = (char *) shmat( shmid, (char *) 0, SHM_RDONLY );
if ( ( !_p ) || ( _p == ((char *) -1) ) )
{
(void) printf( "<client>: Can't attach the shared memory.\n" );
return( 0 );
}
else
{
(void) printf( "<client>: Address <0x%lx>.\n", _p );
}
/* Detaching the shared memory. */
if ( ( status = shmdt( (char *) _p ) ) < 0 )
{
(void) printf( "<client>: Can't detach it. Status: %d\n",
status );
}
else
{
(void) printf( "<client>: Detaching OK.\n" );
}
return( 1 );
}
main()
{
while ( attach_detach() ) sleep( 1 );
}
Technical Details
Message-ID:
<2602@brchh104.bnr.ca>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!cunews!bnrgate!brchh104!brchs1!bnr.ca!rice.edu!sun-spots-requestShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!cunews!bnrgate!brchh104!brchs1!bnr.ca!rice.edu!sun-spots-request From: optigfx!nguyen@uunet.uu.net (Peter-Son Nguyen) Newsgroups: comp.sys.sun Subject: Share memory. Keywords: Miscellaneous Message-ID: <2602@brchh104.bnr.ca> Date: 24 Apr 91 00:00:00 GMT Sender: news@brchh104.bnr.ca Organization: Sun-Spots Lines: 155 Approved: Sun-Spots@rice.edu X-Original-Date: 22 Apr 91 21:52:04 GMT X-Sun-Spots-Digest: Volume 10, Issue 86, message 19 X-Note: Submissions: sun-spots@rice.edu, Admin: sun-spots-request@rice.edu