Displaying amount of ram?

This is a discussion on Displaying amount of ram? within the C Programming forums, part of the General Programming Boards category; HI I would like to start a program (memtester, I allready have it) and the first thing is that program ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    23

    Displaying amount of ram?

    HI
    I would like to start a program (memtester, I allready have it) and the first thing is that program has to display amount of used and free memory, and then you enter amount of memory to test. The only thing is, that I don't really know to get that amount of free and used memory. In linux, function free -m displays that(more than that) and the data is available on /proc/meminfo. I don't really want to use system function, because it doesn't work well.
    Please help me.
    Have a nice Day

    Matt

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    What is the problem with opening /proc/meminfo and reading its contents?

    There is no portable way of determining the info you need, so you're forced to implement platform specific methods to get the information.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    23
    How can I implement it in the program-I have memtester and have to write a line that would display information of ram and program would run forward. So, when program starts, displays data from meminfo and starts to check the ram without me pressing a button(I have base.txt,from where program reads parameters and can insert amount of memory checked there).
    Have a nice Day

    Matt

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    open /proc/meminfo with fopen
    read each line with fgets
    compare the start of the line with strncmp to find the line(s) of data you're interested in.
    Use sscanf to grab the numeric value(s)
    display the value(s) to the user

  5. #5
    @codeguru
    Join Date
    Jan 2006
    Posts
    34
    bad... bad... somehow th os gets ram capacity to. so there must be a way(probally some asm code or something :P)

  6. #6
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    obviously, but why would you use assembly language if the operating system provides a standard (for that OS) facility for getting the information you require?

    If you use assembly language you still run into the same portability problems (and more). It will vary from architecture to architecture, and the OS may not allow a user-space program to access such resources.

  7. #7
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    cwr is right..

    free just parses /proc/meminfo for its operation..

    as far as what detects and writes meminfo on a lower level, that would probably be the kernel..

    and as cwr asked, why would you want to do it on your own after its has already been done better

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    I think you're going to run into a bigger, underlying problem. Linux is only going to give your program a certain amount of memory to work with. Going out of those bounds will cause your program to segfault. Not only that, but the address range you're given is for virtual memory and doesn't correspond to actual physical memory addresses. All of this is to prevent you from stomping on memory that is being used by other programs and the OS itself.

    This is why programs like memtest86 require you to run it outside of any OS influence.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    HI
    I would like to start a program (memtester, I allready have it) and the first thing is that program has to display amount of used and free memory, and then you enter amount of memory to test. The only thing is, that I don't really know to get that amount of free and used memory. In linux, function free -m displays that(more than that) and the data is available on /proc/meminfo. I don't really want to use system function, because it doesn't work well.
    Please help me.
    If you want to start another program without using system(), see the FAQ.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Determing the amount of change(money)
    By Kyeong in forum C Programming
    Replies: 11
    Last Post: 09-30-2008, 04:36 PM
  2. calcuations
    By redmondtab in forum C Programming
    Replies: 39
    Last Post: 09-15-2006, 08:09 PM
  3. Having a problem!
    By Zildjian in forum C++ Programming
    Replies: 6
    Last Post: 10-18-2004, 09:40 AM
  4. I need help badly
    By taz_jiggy in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2002, 09:36 PM
  5. pointerz
    By xlordt in forum C Programming
    Replies: 6
    Last Post: 01-11-2002, 07:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21