Share memory.

Peter-Son Nguyen optigfx!nguyen at uunet.uu.net
Wed Apr 24 10:00:00 AEST 1991


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 );
}





More information about the Comp.sys.sun mailing list