Question: Need help implementing the skeleton code below, I have provided the main.cpp that was given, so no changes needed to be done there. Some guide
Need help implementing the skeleton code below, I have provided the main.cpp that was given, so no changes needed to be done there. Some guide lines given in the attached image.
The /proc directory is a pseudo filesystem that allows access to kernel data structures while in user space. It allows you to view some of the information the kernel keeps on running processes. To view information about a specific process you just need to view files inside of the directory: /proc/[pid]. For more information simply view the manpage with man proc.

Skeleton Code: Implement this using C++
[Code]
#include
/* ------------------------------------------------------------------------- */ /****User Defined Includes *****/ /* ------------------------------------------------------------------------- */
#include "proctest.h"
/* ------------------------------------------------------------------------- */ /* Proctest Private Member Functions */ /* ------------------------------------------------------------------------- */
/* * This function turns a char* buffer into a string given the address * to the buffer. */ std::string Proctest::stringify_buffer(char *buffer) { std::string buffer_string = ""; int pos = 0;
while(true) { if(buffer[pos] == '\0') { break; } else { buffer_string += buffer[pos]; pos++; } }
return buffer_string; }
/* * This function takes in a string and breaks it up into a vector * of space separated words. */ std::vector Proctest::split_on_spaces(std::string input) { std::vector components; std::string token = "";
for(unsigned short int i=0; i { if(input[i] != ' ') { token += input[i]; } else { components.push_back(token); token = ""; } }
return components; }
------------------------------------------------------------------------- /***** Proctest Public Member Functions *****/
-----------------------------------------------------------------------
Proctest::Proctest(int process_id) { /* Record process ID */
/* Initialize the read buffer */
/* Read from the stat file */
/* Read from the status file */
/* Find the number of open file descriptors by counting /proc/[pid]/fd */
/* Read Memory Map */ }
Proctest::~Proctest() { // Free buffer memory & clear all vectors }
std::string Proctest::getpid() { return ""; }
std::string Proctest::getppid() { return ""; }
std::string Proctest::geteuid() { return ""; }
std::string Proctest::getegid() { return ""; }
std::string Proctest::getruid() { return ""; }
std::string Proctest::getrgid() { return ""; }
std::string Proctest::getfsuid() { return ""; }
std::string Proctest::getfsgid() { return ""; }
std::string Proctest::getstate() { return ""; }
std::string Proctest::getthread_count() { return ""; }
std::string Proctest::getpriority() { return ""; }
std::string Proctest::getniceness() { return ""; }
std::string Proctest::getstime() { return ""; }
std::string Proctest::getutime() { return ""; }
std::string Proctest::getcstime() { return ""; }
std::string Proctest::getcutime() { return ""; }
std::string Proctest::getwwcode() { return ""; }
std::string Proctest::getendcode() { return ""; }
std::string Proctest::getesp() { return ""; }
std::string Proctest::geteip() { return ""; }
std::string Proctest::getfiles() { return ""; }
std::string Proctest::getvoluntary_context_switches() { return ""; }
std::string Proctest::getnonvoluntary_context_switches() { return ""; }
std::string Proctest::getlast_cpu() { return ""; }
std::string Proctest::getallowed_cpus() { return ""; }
std::vector Proctest::getmemory_map() { return std::vector(); }
[/EndCode]
Main.cpp given: Nothing to change here.
[Code]
#include
/* ------------------------------------------------------------------------- */ /* User Defined */ /* ------------------------------------------------------------------------- */
#include "proctest.h"
int main() { /* Read in PID */ int pid; std::cout > pid;
while(std::cin.fail()) { std::cout > pid; } /* Check a process with that pid actually exists in the system */ std::string fname = ""; while(true) { fname = "/proc/" + std::to_string(pid) + "/stat";
struct stat test_buffer; if(stat(fname.c_str(), &test_buffer) != 0) { std::cout > pid;
while(std::cin.fail()) { std::cout > pid; } } else { break; } }
Proctest *process_data = new Proctest(pid);
/* Print everything */ std::cout getpid() getppid() geteuid() getegid() getruid() getrgid() getfsuid() getfsgid() getstate() getthread_count() getpriority() getniceness() getstime() getutime() getcstime() getcutime() getstartcode() getendcode() getesp() geteip() getfiles() getvoluntary_context_switches() getnonvoluntary_context_switches() getlast_cpu() getallowed_cpus() getmemory_map(); for(unsigned short int i=0; i { std::cout
return 0; }
[EndMain.cpp]
Header File:
[Code]
#include
class Proctest { private: int m_process_id; char *m_read_buffer; std::string m_num_fds; std::vector m_stat_array; std::vector > m_status_array; std::vector m_mem_array;
std::string stringify_buffer(char *buffer); std::vector split_on_spaces(std::string input);
public: /* Constructor */ Proctest(int process_id); ~Proctest();
/* Process IDs */ std::string getpid(); std::string getppid(); std::string geteuid(); std::string getegid(); std::string getruid(); std::string getrgid(); std::string getfsuid(); std::string getfsgid();
/* State */ std::string getstate();
/* Thread Information */ std::string getthread_count();
/* Scheduling Information */ std::string getpriority(); std::string getniceness();
/* Time Information */ std::string getstime(); std::string getutime(); std::string getcstime(); std::string getcutime();
/* Address Space */ std::string getstartcode(); std::string getendcode(); std::string getesp(); std::string geteip(); /* Resources */ std::string getfiles(); std::string getvoluntary_context_switches(); std::string getnonvoluntary_context_switches();
/* Processor Information */ std::string getlast_cpu(); std::string getallowed_cpus();
/* Memory */ std::vector getmemory_map(); };
[EndCode]
Using the files stored at /proc write a program/script to find information about a specific process using a user provided pid. In the following, you will find a list of the task struct members for which you are required to find their value In the task.struct a lot of the data you are finding is not represented as member values but instead pointers to other linux data structures that contain these members. All of the information you will be retrieving can be found in a processs proc directory (/proc/Ipid). Your program must be able to retrieve the following data about any given process if the given process Id is existing under direcotry /proc Table #1: Process Attributes Category Required Variables/Items Description Identifiers PID. PPID Process ID of the current process and its parent EUID, EGID Effective user and group ID RUIDO, RGID Real user and group ID FSUID, FSGID File system user and group ID State D, T, Z. X Running, Sleeping, Disk sleeping, Stopped Zombie, and Dead Thread Info Thread IDs of a process Information Priority Priority Number Integer value from 1 to 99 for real time processes Niceness Value Integer value from -20 to 19 Time Time that a process has been scheduled stime ultime in kernel r mode Information & cu time Time that a process has waited on children. cstime being run in kernel /user mode Startcode & Endcode The start and end of a process in memory Resources File Handles & Number of fds used, and number of voluntary/involuntary context switches Context Switches Which cores the process is allowed to run on and which one was last used Address range, permissions Output a file containing the process's Memory Map offset, dev inode. currently mappped memory regions and path name
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
