I need help with this practice problem?
Execute the following coding segment and identify the errors in the program. Debug the program
and provide the correct version of the code. Hinclude int main() printf(\"%s\", isdigit(\'A\') ? \"A
is digit\" \"A is not digit\"); a : a printf(\"Inlnln\") return return e;
Solution
in range [0,9]. If it\'s not then this function returns 0.
Given program compiles successfully, compiler does not give any error.
But If the input of isdigit(\'A\') is changed then the program will always print
\"A is a digit\" or
\"A is not a digit\" .
It will not print exact value of char A.
So, correct code is given below.
#include
int main()
{
char A = \'1\';
printf(\"%c%s\",A,isdigit(A)? \" is a digit\" : \" is not a digit\");
printf(\"\ \");
A = \'B\';
printf(\"%c%s\",A,isdigit(A)? \" is a digit\" : \" is not a digit\");
printf(\"\ \");
return 0;
}
Sample Output:
1 is a digit
B is not a digit.
9548086042 for call girls in Indira Nagar with room service
Need help implementing the skeleton code below, I have provided the .pdf
1. 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
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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 = "";
2. 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;
}
3. -------------------------------------------------------------------------
/***** 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()
11. /* 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
Solution
#include
#include
#include
#include
12. #include
#include
#include
#include
#include
#include
#include
#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;
13. }
/*
* 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()
17. }
[/EndCode]
Main.cpp given: Nothing to change here.
[Code]
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
/* ------------------------------------------------------------------------- */
/* User Defined */
/* ------------------------------------------------------------------------- */
#include "proctest.h"
int main()
{
/* Read in PID */
int pid;
std::cout << "Enter the pid of a process: ";
std::cin >> pid;
while(std::cin.fail())
{
std::cout << "[ERROR]: PID is not an integer. Try again: ";
std::cin >> pid;
}
/* Check a process with that pid actually exists in the system */
std::string fname = "";
while(true)