[comp.unix.questions] Available shared memory.

venkat@matrix.UUCP (D Venkatrangan) (01/08/91)

I'm looking for a system call that will return the amount of shared memory
that is currently available.  If not, is there a kernel variable?

Please reply to uunet!matrix!venkat

Thanks.

mike (Michael Stefanik) (01/09/91)

In article <173@matrix.UUCP> venkat@matrix.UUCP () writes:
>I'm looking for a system call that will return the amount of shared memory
>that is currently available.  If not, is there a kernel variable?

This is kernel specific, but since this may be of interest to others, I'll
post it here.  Now this solution is specific to XENIX, but the philosophy
remains the same.  The amount of shared memory available is as ambiguous
as the amount of memory available, especially so if you have virtual
memory, etc.  However, the kernel may set a maximum size that any process 
may allocate in terms of a block of shared memory.

Here is a program that returns the maximum size of a shared memory segment
under XENIX.  More text follows the code ...

---[ cut here ]-------------------------------------------------------------

/*      @(#)shm.c       1.1 91/01/08
        determine the maximum amount of shared memory available for
        a process

	written by Mike Stefanik in about 10 minutes; should this
	program cause your computer to spontaneously combust, don't
	come looking for me ...
*/

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <a.out.h>
#include <sys/param.h>
#include <sys/types.h>

/* here we define a structure that contain the elements that we are
   concerned about looking for */

struct shmblk {
        long    s_shmmax;       /* max shared memory size */
        long    s_shmmin;       /* min shared memory size */
        long    s_shmmni;       /* max shared memory identifiers */
        long    s_shmseg;       /* number of shared segments per proc */
        long    s_shmbrk;       /* ? */
        long    s_shmall;       /* max in-use shared memory segments */
        };

main()
{
int             kmem;   /* kernel memory file descriptor */
struct nlist    nl[2];  /* the namelist */
struct shmblk   sm;     /* the shared memory info structure */

        if ( (kmem = open("/dev/kmem",O_RDONLY)) == -1 ) {
                fprintf(stderr,"Cannot open /dev/kmem, ciao!\n");
                exit(1);
                }

        /* here we load the namelist with the symbol that we're looking
           for, specifically _shminfo */

        strncpy(nl[0].n_name,"_shminfo",8);

        /* here we search the symbol table for _shminfo in /xenix; if
           your curious, this is why the kernel isn't stripped (if it
           was, we wouldn't be able to this!) */

        if ( nlist("/xenix",nl) < 0 ) {
                fprintf(stderr,"Cannot read the namelist, adios!\n");
                exit(1);
                }

        /* check to see if _shminfo was found */
        
        if ( nl[0].n_type == 0 ) {
                fprintf(stderr,"Cannot find the symbol.  Is this XENIX?\n");
                exit(1);
                }

        /* since it was, the value is the offset into /dev/kmem; read
           the contents of the kernel memory into our own structure */

        lseek(kmem,(long)nl[0].n_value,SEEK_SET);
        read(kmem,&sm,sizeof(struct shmblk));
        close(kmem);

        /* show what we have */

        printf("The maximum size of a shared memory segment is %ld bytes\n",
                sm.s_shmmax);
}

----[ cut here ]-------------------------------------------------------------

This may not be exactly what you're looking for, but it's about as close
as you're going to get, methinks.

-- 
Michael Stefanik, Systems Engineer (JOAT), Briareus Corporation
UUCP: ...!uunet!bria!mike
--
technoignorami (tek'no-ig'no-ram`i) a group of individuals that are constantly
found to be saying things like "Well, it works on my DOS machine ..."