Linux: Memory Management Programming Excercise in C++ HELP!! Thank You a C/C++ p
ID: 3889903 • Letter: L
Question
Linux: Memory Management Programming Excercise in C++
HELP!!
Thank You
a C/C++ program to read memory information about an executable, you should use the filename called "meminfo.cpp" and the target executable "meminfo". For detecting memory information about an executable, Linux systems have a built-in package called binutils which has a executable called size. It is suggested you use size since it is a lot more complicated to retrieve executable memory information directly from C++. Things to note: 1. You should have one command line argument that accepts the executables name 2. You must make use of a subprocess call (eg. popen). 3. You must perform significant error checking, such as invalid files, etc. . Your program must display the sizes of each segment in "Human Readable" bytes. Your program should show the closest range to different sizes for example, 1020 bytes, or 13.65 KB, etc. 5. Your program executable should be able to be accessed in any terminal as seen in (Is, make, rm, etc.)Explanation / Answer
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <sys/stat.h>
#include <sstream>
// char* convert_to_human_readable(long double size);
char* convert_to_human_readable(long double size, char *final_size) {
int count = 0;
while (size > 1024) {
size /= 1024;
count = count + 1;
}
const char* sizes[] = {"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
sprintf(final_size, "%.*Lf %s", count, size, sizes[count]);
return final_size;
}
int main(int argc, char const *argv[])
{
//if second argument is not given the return by printing message
if (argc < 2)
{
std::cout << "meminfo: Please provide the executeable name. "
<< "Usage ./meminfo <executeable-file> ";
}
//variables to get file stats
struct stat buf;
stat(argv[1], &buf);
//check wheather file with given name is dir if yes then return
//by printing error message
if(S_ISDIR(buf.st_mode))
{
std::cout << "meminfo: [" << argv[1] << "] is a directory. ";
return -1;
}
//check wheather file with given name exists or not
if(!S_ISREG(buf.st_mode))
{
std::cout << "meminfo: [" << argv[1] << "] no such file. ";
return -1;
}
//file to get the piped data from size command
FILE* in;
//get the file name from command line arguments
std::string filename = argv[1];
//concatenate the file name with size command
std::string command = "size " + filename;
//execute the "size" command and get its output using popen
if(!(in = popen(command.c_str(), "r"))) return 1;
//store data to output string
char buff[512];
std::string output;
while(fgets(buff, sizeof(buff), in)!=NULL){
output += buff;
}
//extract data from output stream using strigstream
long double text, data, bss, total;
std::istringstream o_stream(output);
std::string dummy;
// text data bss dec hex filename
// 120822 2040 3432 126294 1ed56 /bin/ls
// skip text data bss dec hex and filename by putting it to dummy string
// read text data bss and toatal in decimal
o_stream >> dummy >> dummy >> dummy >> dummy >> dummy >> dummy
>> text >> data >> bss >> total;
char readable_size[100];
//conversion to human readable format and output the data
std::cout << "MEMINFO "
<<"========================" << std::endl;
std::cout << "FILE: " << argv[1] << std::endl;
std::cout << "TEXT: " << convert_to_human_readable(text, readable_size) << std::endl;
std::cout << "DATA: " << convert_to_human_readable(data, readable_size) << std::endl;
std::cout << "BSS: " << convert_to_human_readable(bss, readable_size) << std::endl;
std::cout << "TOTAL: " << convert_to_human_readable(total, readable_size) << std::endl;
//close the FILE in
pclose(in);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.