[comp.unix.ultrix] Shared memory under Ultrix 3.0

rick@ut-emx.UUCP (Rick Watson) (05/13/89)

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@utadnx.cc.utexas.edu (128.83.1.26)
 uucp:   ...cs.utexas.edu!ut-emx!rick
 bitnet: watson@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);
}

rick@ut-emx.UUCP (Rick Watson) (05/16/89)

In article <13026@ut-emx.UUCP>, rick@ut-emx.UUCP (Rick Watson) writes:
> 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?

Oops, sorry, dumb error.  The child didn't need to shmat the shared
memory as it already was mapped.

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