Reputation: 14505
I would like to either make a shell script or c program that computes the free memory in similar fashion as command free
does on linux, for hp-ux.
On hp-ux default installation, only commands that I know which can calculate the free memory are vmstat, or eventually top.
Is there any c api that allows user to calculate all free memory? Or eventually a way to determine all physical memory available on system, then sum all used memory and calculate free PHYS memory?
Upvotes: 0
Views: 1051
Reputation: 2563
I do it with this little program:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/pstat.h>
int main () {
struct pst_static pst;
struct pst_dynamic psd;
memset(&pst,0,sizeof(struct pst_static));
pstat_getstatic(&pst, sizeof(pst), (size_t)1, 0);
memset(&psd,0,sizeof(struct pst_dynamic));
pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0);
printf("Total Memory: %lld\n",pst.physical_memory * pst.page_size);
printf("Free Memory: %lld\n",psd.psd_free * pst.page_size);
}
It outputs the total and the free memory.
See also the HP-UX manual page for pstat
.
Upvotes: 1