SUMMARY: phys. mem. size

From: Colin Allison (colin@cs.st-andrews.ac.uk)
Date: Thu May 30 1991 - 11:40:38 CDT


Thanks to all who responded.
The most efficient methods involve having read access to /dev/kmem.
The following program is available from sun-spots archives. Once
compiled it should have group set to kmem and setgid on execution
if universal access is required. (I hope this isn't a security problem!)

*******************************************************************

/*
 * mem.c - print physical memory installed in a machine
 *
 * $Author: kensmith $
 *
 * $Date: 90/03/12 14:18:18 $
 *
 * $Log: mem.c,v $
 * Revision 1.2 90/03/12 14:18:18 kensmith
 * misc. cleanup
 *
 * Revision 1.1 90/02/09 21:43:41 kensmith
 * Initial revision
 *
 */

static char rcsid[] = "$Id: mem.c,v 1.2 90/03/12 14:18:18 kensmith Exp Locker: kensmith $";

#include <stdio.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/file.h>
#include <nlist.h>

struct nlist nls[] = {
        { "_physmem" },
#define PHYSMEM 0
        { "" },
};

int fd, physmem;

char *name; /* program name */
char *rindex();

main(argc, argv)
        int argc;
        char *argv[];
{
        if ((name = rindex(argv[0], '/')) == NULL)
                name = argv[0];
        else
                name++;
        if (nlist("/vmunix", nls) < 0) {
                perror("nlist");
                exit(1);
        }
        if ((fd = open("/dev/kmem", O_RDONLY)) <= 0) {
                perror("/dev/kmem");
                exit(1);
        }
        if (nls[PHYSMEM].n_type == 0) {
                fprintf(stderr, "%s: physmem symbol not found\n", name);
                exit(1);
        }
        lseek(fd, nls[PHYSMEM].n_value, L_SET);
        read(fd, &physmem, sizeof(physmem));
        printf("%dK bytes of memory\n", ctob(physmem) / 1024);
}

 



This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:06:14 CDT