Shared memory under Ultrix 3.0

Rick Watson rick at ut-emx.UUCP
Sun May 14 02:02:05 AEST 1989


The following program works under 2.2 but the fails under 3.0.
The child process fails with EMFILE "Too many open files" when
the shmat is executed.  The man page indicates that I've exceeded
the "system imposed limit".  What am I doing wrong?  Does the system
have to be configured to use shared memory?

Rick Watson
University of Texas Computation Center
 arpa:   watson at utadnx.cc.utexas.edu (128.83.1.26)
 uucp:   ...cs.utexas.edu!ut-emx!rick
 bitnet: watson at utadnx
 span:   utspan::watson (UTSPAN is 25.128)
 phone:  512/471-8220 512/471-3241

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

int shmid;				/* shared memory id */

static struct shared {
	int flag;
} *share;				/* shared memory pointer */

main()
{
/*  get a shared memory segment */

	shmid = shmget(IPC_PRIVATE, sizeof(struct shared), (IPC_CREAT | 0600));
	if ((int) shmid < 0) {
		perror("shmget");
		exit(0);
	}

/*  map the shared struct. allow 2M for malloc to grow */

	share = (struct shared *) shmat(shmid, sbrk(0) + 2048*1024, SHM_RND);
	if ((int) share <= 0) {
		perror("parent shmat");
		exit(0);
	}	

	share->flag = 0;		/* preset the flag */

/*  now we go our separate ways */

	if (fork()) {
		share->flag++;
		sleep (5);
		printf("parent exiting, flag=%d\n", share->flag);
	}
	else child();
}


child()
{
/*  map the shared struct */

	share = (struct shared *) shmat(shmid, 0, 0);
	if (share < 0) {
		perror("child shmat");
		exit(0);
	}	

	share->flag++;
	sleep (5);

	printf("child exiting, flag=%d\n", share->flag);
}



More information about the Comp.unix.ultrix mailing list