Path: utzoo!mnetor!uunet!lll-winken!lll-tis!ames!mailrus!umix!nancy!eecae!super.upenn.edu!rutgers!iuvax!pur-ee!ea.ecn.purdue.edu!davy From: davy@ea.ecn.purdue.edu (Dave Curry) Newsgroups: comp.unix.questions Subject: Re: Determining system memory Message-ID: <2386@ea.ecn.purdue.edu> Date: 31 Mar 88 21:04:57 GMT References: <248@lxn.UUCP> Reply-To: davy@ea.ecn.purdue.edu.UUCP (Dave Curry) Organization: Purdue University Engineering Computer Network Lines: 85 In article <248@lxn.UUCP> chris@lxn.UUCP (Christopher D. Orr) writes: > >I have been trying to find a nice, clean way to determine system >memory under SYS V 2.2. I have come up with two solutions to this >problem. However, there must be a cleaner/faster way :-). > Of course the simplest and fastest way, assuming you have the permissions, is to just go dig it out of the kernel. --Dave /* * Figure out how much memory there is on a machine. This works under * Berkeley UNIX. It should work under System V if you change the * UNIX define to "/unix" instead of "/vmunix". * * Of course, if you don't have read permission on the kernel and * kernel memory, this won't work. * * Dave Curry * Purdue University * Engineering Computer Network * davy@intrepid.ecn.purdue.edu */ #include #include #include #include #ifndef ctob /* For System V */ #include #endif #define UNIX "/vmunix" #define KMEM "/dev/kmem" struct nlist nl[] = { #define X_PHYSMEM 0 { "_physmem" }, #define X_MAXMEM 1 { "_maxmem" }, { NULL } }; main() { int kmem; int maxmem, physmem; /* * Look up addresses of variables. */ if ((nlist(UNIX, nl) < 0) || (nl[0].n_type == 0)) { fprintf(stderr, "%s: no namelist.\n", UNIX); exit(1); } /* * Open kernel memory. */ if ((kmem = open(KMEM, 0)) < 0) { perror(KMEM); exit(1); } /* * Read variables. */ lseek(kmem, (long) nl[X_PHYSMEM].n_value, 0); read(kmem, (char *) &physmem, sizeof(int)); lseek(kmem, (long) nl[X_MAXMEM].n_value, 0); read(kmem, (char *) &maxmem, sizeof(int)); close(kmem); /* * Print the numbers. The internal representation is * in units of core clicks; convert to bytes. */ printf("Physical machine memory: %d\n", ctob(physmem)); printf("Max memory available to a process: %d\n", ctob(maxmem)); exit(0); }