Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: C++
Expertise: Intermediate
Dec 20, 2005



Application Security Testing: An Integral Part of DevOps

Implementing the GetFreePhysicalMemory API in Linux

Because everything in Linux is composed of files, you can get any required information from the /proc/meminfo file. Here's the code:

int getFreePhysicalMemory()
        ifstream meminfo("/proc/meminfo");

        if ( ! meminfo.is_open() )
                return -1;

        char szTmp[256];
        char szMem[256];

        string s0("MemFree:");
        string s1("kB");

        while ( ! meminfo.eof() )
                meminfo.getline( szTmp, 256 );

                string s2(szTmp);

                string::size_type pos0 = s2.find(s0);

                if( pos0 != string::npos )
                        string::size_type pos1  = s2.find(s1);

                        if ( pos1 != string::npos )
                                string s3 = s2.substr( pos0 + s0.size(), pos1 - (pos0+s0.size()) );
                                strncpy(szMem, s3.c_str(), s3.size() );
                                return (atoi(szMem)*1024*1024) ;

Elsayed Elghadban
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date