|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Determining free(available) memory in MV linux
HI
I'm a rookie in C programming and I'm working in Monta Vista Linux. I have to write a program that displays free memory. I have memtester(allready written by someone else) and now I have to type how much amount of memory tester will test and I want that memtester finds out himself how much of free memory is available in the system to test and tests it. I tried by opening /proc/meminfo and locate the number of free memory, but when I would open a program to determine free memory free memory number would change probably and data wouldn't be real. Please I need any suggestion of you experts how to determine amount of free memory that will be tested with program. Have a nice Day Matt |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
My first suggestion is: get a copy of the source for the free utility - it is part of coreutils for Linux.
Second - this is probably a difficult exercise for a beginner. Consider using a call to free -m inside a system() call in C. |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
HI
If I compare numbers on what free -m displays or what is the number on meminfo, they are different free -m total used free shared buffers cached Mem: 492 23 469 0 1 14 -/+ buffers/cache: 7 485 Swap: 0 0 0 meminfo: total: used: free: shared: buffers: cached: Mem: 516644864 25026560 491618304 0 1560576 15425536 Swap: 0 0 0 MemTotal: 504536 kB MemFree: 480096 kB MemShared: 0 kB Buffers: 1524 kB Cached: 15064 kB SwapCached: 0 kB Active: 5556 kB Inactive: 12048 kB HighTotal: 0 kB HighFree: 0 kB LowTotal: 504536 kB LowFree: 480096 kB SwapTotal: 0 kB SwapFree: 0 kB Committed_AS: 1156 kB VmallocTotal: 491512 kB VmallocUsed: 596 kB VmallocChunk: 490916 kB What if I would somehow get the number of free memory and store it somewhere. Then memtester reads the number from stored place and checks that much amount of memory that number(should be in MB) is writen in stored place. Please help Have a nice Day Matt |
|
#4
|
|||
|
|||
|
HI
Is maybe some function available something like getfreemem() or something that would display or.......... I don't know - I just have to have number of MB that is available on system and that is reachable from other program like a parameter to the second program that orders to memtester to check exactly that amount of memory. Have a nice Day Matt |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
example: Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
double get_meminfo(void)
{
double retval=0;
char tmp[256]={0x0};
/* note= add a path to meminfo like /usr/bin/meminfo
to match where meminfo lives on your system */
FILE *shellcommand=popen("meminfo","r");
while(fgets(tmp,sizeof(tmp),shellcommand)!=NULL)
{
if(memcmp(tmp,"Mem:",4)==0)
{
int wordcount=0;
char *delimiter=" ";
char *p=strtok(tmp,delimiter);
while(*p)
{
wordcount++;
if(wordcount==3) retval=atof(p);
}
}
}
pclose(shellcommand);
return retval;
}
int main(int argc, char *argv[])
{
double freemem=get_meminfo();
printf("free memory=%f\n", freemem);
return 0;
} |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
I've noticed that meminfo does not display real values - function free -m does. If I insert to memtester the same value that I got from free -m memtester works normally, but when I put only 1MB higher number memtester terminates - not enough memory(so that means the function in shell free -m displays real free memory data). Question now is how to enter the shell function from C to save and display data from free -m?
Have a nice Day Matt |
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
HI
I've tried your program but I have got premission denied: sh: /proc/meminfo: Permission denied free memory=0.000000 When you wrote the path "meminfo", I entered whole path "/proc/meminfo" But that is not the question now as meminfo does not display real data. Have a nice Day Matt |
| Sponsored Links | ||
|
![]() |
| Tags |
| linux |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| free memory in debian linux | lhareigh890 | UNIX for Advanced & Expert Users | 1 | 07-01-2011 01:08 PM |
| how to get the more memory free space (see memory free column) | murthy76 | Solaris | 3 | 04-21-2009 09:34 AM |
| determining vxdg free space | itik | Linux | 0 | 12-23-2008 06:19 PM |
| Linux Server free memory decreases | Vaibhav Agarwal | Linux | 13 | 12-18-2008 09:15 AM |
| Free Linux Memory by Dropping Caches | Neo | Linux | 0 | 11-29-2008 10:29 AM |
|
|