Question: #include #include #include #include #include #include #include #include using namespace std; void executeCommand ( const vector& args ) ; vector parseInput ( string input )

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
void executeCommand(const vector& args);
vector parseInput(string input);
void printCpuClockSpeed();
int main(){
string input;
string prompt = "cwushell>";
vector args;
while (true){
cout prompt "";
getline(cin, input);
args = parseInput(input);
if (args.empty()){
continue;
}
// Internal command: exit
if (args[0]== "exit"){
int exitStatus = args.size()>1? stoi(args[1]) : 0;
exit(exitStatus);
}
// Internal command: prompt
else if (args[0]== "prompt"){
if (args.size()>1){
prompt = args[1];
} else {
prompt = "cwushell>";
}
}
// Internal command: cpuinfo
else if (args[0]== "cpuinfo"){
if (args.size()>1){
if (args[1]=="-c"){
printCpuClockSpeed();
} else if (args[1]=="-t"){
// Print CPU type
} else if (args[1]=="-n"){
// Print number of CPU cores
} else {
cout "Invalid switch for cpuinfo" endl;
}
} else {
cout "No switch provided for cpuinfo" endl;
}
}
// Internal command: meminfo
else if (args[0]== "meminfo"){
// Similar to cpuinfo
}
// External commands
else {
executeCommand(args);
}
}
return 0;
}
void printCpuClockSpeed(){
ifstream file("/proc/cpuinfo");
if (!file.is_open()){
cerr "Error opening file: " strerror(errno) endl;
return;
}
string line;
while (getline(file, line)){
if (line.find("cpu MHz")!= string::npos){
// Process the line to extract CPU speed
// The line typically looks like "cpu MHz : 2400.000"
size_t pos = line.find(":");
if (pos != string::npos){
string speedStr = line.substr(pos +1);
double speedMHz = stod(speedStr);
cout "CPU Clock Speed: " speedMHz " MHz" endl;
}
break;
}
}
file.close();
}
vector parseInput(string input){
vector args;
string arg;
istringstream iss(input);
while (iss >> arg){
args.push_back(arg);
}
return args;
}
void executeCommand(const vector& args){
pid_t pid = fork();
if (pid ==-1){
perror("fork");
} else if (pid ==0){
// Convert vector to char* array for execvp
vector c_args;
for (const auto& arg : args){
c_args.push_back(const_cast(arg.c_str()));
}
c_args.push_back(nullptr);
execvp(c_args[0], c_args.data());
perror("execvp");
exit(EXIT_FAILURE);
} else {
wait(nullptr); // Wait for the child process to finish
}
}
 #include #include #include #include #include #include #include #include using namespace std;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!