Question

In: Computer Science

Question from Operating Systems Proc,Forks,Exec Examine the code given and do changes as mentioned in the...

Question from Operating Systems

Proc,Forks,Exec

Examine the code given and do changes as mentioned in the steps below:

#include "HALmod.h"

int GetCommand (string tokens [])
{
string commandLine;
bool commandEntered;
int tokenCount;

do
{
cout << "HALshell> ";
while (1)
{
getline (cin, commandLine);
commandEntered = CheckForCommand ();
if (commandEntered)
{
break;
}

}
} while (commandLine.length () == 0);

tokenCount = TokenizeCommandLine (tokens, commandLine);

return tokenCount;
}

int TokenizeCommandLine (string tokens [], string commandLine)
{
char *token [MAX_COMMAND_LINE_ARGUMENTS];
char *workCommandLine = new char [commandLine.length () + 1];
int i;
int tokenCount;

for (i = 0; i < MAX_COMMAND_LINE_ARGUMENTS; i ++)
{
tokens [i] = "";
}
strcpy (workCommandLine, commandLine.c_str ());
i = 0;
if ((token [i] = strtok (workCommandLine, " ")) != NULL)
{
i ++;
while ((token [i] = strtok (NULL, " ")) != NULL)
{
i ++;
}
}
tokenCount = i;

for (i = 0; i < tokenCount; i ++)
{
tokens [i] = token [i];
}

delete [] workCommandLine;

return tokenCount;
}


//Do not touch the below function
bool CheckForCommand ()
{
if (cullProcess)
{
cullProcess = 0;
cin.clear ();
cout << "\b\b \b\b";
return false;
}

return true;
}

  

int ProcessCommand (string tokens [], int tokenCount)
{
if (tokens [0] == "shutdown" || tokens [0] == "restart" || tokens [0] == "lo")
{
   if (tokenCount > 1)
   {
        cout << "HALshell: " << tokens [0] << " does not require any arguments" << endl;
   return 1;
   }
   cout << endl;
   cout << "HALshell: terminating ..." << endl;

return 0;
  
}
else
   return 1;
}
char ** ConvertToC (string tokens [], int tokenCount)
{
char ** words;
words = (char **) malloc (sizeof (char*) * tokenCount);

for (int i=0; i<tokenCount; i++)
{
words[i]=strdup(tokens[i].c_str());
}
return words;
}

Step 1- Modify the "ConvertToC" function ( last function) as mentioned:

1. allocate an extra "word" for NULL (Hint: tokenCount+1)
2. store the value of NULL into the extra "word"

Step 2- Modify the "ProcessCommand" function ( second last function) as mentioned:

You will be emulating a shell. First, the shell checks for specific commands ("shutdown", "lo", etc). If it does not recognize those commands then, a fork occurs and the child will execute the command as typed.

The algorithm to implement is as follows (Hint: add code inside the else):

1. fork to create a child and parent

2. the child will do the following:
Call the ConverToC function and capture the return into a char** variable
Call execvp sending it two arguments: the first element (or word) of the char** variable, and the entire array
Print an error message
exit

3. the parent will do the following:
wait for the child
return 1

4. don't forget to handle the error case of the fork


//Dont try to run the program, just do the following steps.

Solutions

Expert Solution

#include "HALmod.h"

int GetCommand (string tokens [])
{
string commandLine;
bool commandEntered;
int tokenCount;

do
{
cout << "HALshell> ";
while (1)
{
getline (cin, commandLine);
commandEntered = CheckForCommand ();
if (commandEntered)
{
break;
}

}
} while (commandLine.length () == 0);

tokenCount = TokenizeCommandLine (tokens, commandLine);

return tokenCount;
}

int TokenizeCommandLine (string tokens [], string commandLine)
{
char *token [MAX_COMMAND_LINE_ARGUMENTS];
char *workCommandLine = new char [commandLine.length () + 1];
int i;
int tokenCount;

for (i = 0; i < MAX_COMMAND_LINE_ARGUMENTS; i ++)
{
tokens [i] = "";
}
strcpy (workCommandLine, commandLine.c_str ());
i = 0;
if ((token [i] = strtok (workCommandLine, " ")) != NULL)
{
i ++;
while ((token [i] = strtok (NULL, " ")) != NULL)
{
i ++;
}
}
tokenCount = i;

for (i = 0; i < tokenCount; i ++)
{
tokens [i] = token [i];
}

delete [] workCommandLine;

return tokenCount;
}


//Do not touch the below function
bool CheckForCommand ()
{
if (cullProcess)
{
cullProcess = 0;
cin.clear ();
cout << "\b\b \b\b";
return false;
}

return true;
}

  

int ProcessCommand (string tokens [], int tokenCount)
{

if (tokens [0] == "shutdown" || tokens [0] == "restart" || tokens [0] == "lo")
{
if (tokenCount > 1)
{
cout << "HALshell: " << tokens [0] << " does not require any arguments" << endl;
return 1;
}
cout << endl;
cout << "HALshell: terminating ..." << endl;

return 0;
  
}
else{
char ** va;
pid_t pid;
pid = fork();
if (pid == 0) {
va=ConvertToC();
execvp(*word, words);
printf("Error\n");
}

  
else{
wait(NULL);
}
return 1;
}
}
char ** ConvertToC (string tokens [], int tokenCount)
{
char ** words;
char ** word
words = (char **) malloc (sizeof (char*) * tokenCount);
word = (char **) malloc (sizeof (char*) * tokenCount+1);
word=NULL;
for (int i=0; i<tokenCount; i++)
{
words[i]=strdup(tokens[i].c_str());
}
return words;
}


Related Solutions

Proc,Forks,Exec Examine the code given and do changes as mentioned in the steps below: #include "HALmod.h"...
Proc,Forks,Exec Examine the code given and do changes as mentioned in the steps below: #include "HALmod.h" int GetCommand (string tokens []) { string commandLine; bool commandEntered; int tokenCount; do { cout << "HALshell> "; while (1) { getline (cin, commandLine); commandEntered = CheckForCommand (); if (commandEntered) { break; } } } while (commandLine.length () == 0); tokenCount = TokenizeCommandLine (tokens, commandLine); return tokenCount; } int TokenizeCommandLine (string tokens [], string commandLine) { char *token [MAX_COMMAND_LINE_ARGUMENTS]; char *workCommandLine = new char...
One feature that separates Linux from other operating systems is that the source code, along with...
One feature that separates Linux from other operating systems is that the source code, along with any changes, remains public.   Closed source operating systems, where developers refuse to share some or all of the code, may hinder third-party software developers who create programs and apps for the operating system. Supporters of open source maintain that open source software enables developers to examine, correct and enhance code to create better programs. Communities of open source programmers can make changes immediately, which...
Write Algoritm , code and output. In Operating Systems , . Implement a program for page...
Write Algoritm , code and output. In Operating Systems , . Implement a program for page replacement using the following a.FIFO b.LRU c.OPTIMAL
Write Algoritm , code and output. In C++ In Operating Systems , Simulate with a program...
Write Algoritm , code and output. In C++ In Operating Systems , Simulate with a program to schedule disk in seek optimization.
Write Algoritm , code and output. In Operating Systems , 26. Implement a program to allocate...
Write Algoritm , code and output. In Operating Systems , 26. Implement a program to allocate memory by applying the following strategies.a .FIRST FIT b.BEST FIT c.WORST FIT
Embedded computer systems: Configure and code clock for Pic24e referring to the example C code given...
Embedded computer systems: Configure and code clock for Pic24e referring to the example C code given below. a) Write code on how to configure bits that uses its fast RC oscillator with PLL to run at 40MHz. Assume the RC's frequency is 7.5MHz. b) Write code on how to configure bits that uses its primary crystal (XT) with PLL to run at 40MHz. Assume the external clock is a crystal oscillator of 8MHz. ====== Given C Code ======= #include "ConfigurationBits.h"...
OPERATING SYSTEMS HOMEWORK: PLEASE CODE IN JAVA with comments & POST SCREENSHOTS OF OUTPUTS SCAN This...
OPERATING SYSTEMS HOMEWORK: PLEASE CODE IN JAVA with comments & POST SCREENSHOTS OF OUTPUTS SCAN This algorithm is performed by moving the R/W head back-and-forth to the innermost and outermost track. As it scans the tracks from end to end, it process all the requests found in the direction it is headed. This will ensure that all track requests, whether in the outermost, middle or innermost location, will be traversed by the access arm thereby finding all the requests. This...
Language for this question is Java write the code for the given assignment Given an n...
Language for this question is Java write the code for the given assignment Given an n x n matrix, where every row and column is sorted in non-decreasing order. Print all elements of matrix in sorted order.Input: The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the matrix. Then the next line contains the n x n elements...
in c++ QUESTION 4: Write the code to do the following: -read input from the keyboard...
in c++ QUESTION 4: Write the code to do the following: -read input from the keyboard by displaying the message on the screen to ask and read the following information: * customer ID (string) * customer name (string)                                                        * balance (float) -open output file customer .txt to write -Write to the output file customer.txt the following information:                 Customer ID – customer name – balance For example: 1561175753 - James Smith – 1255.25 -close file QUESTION 5: -create one notepad...
Why do Apple's personal computer Operating Systems benefit from network effects? Include in your explanation a...
Why do Apple's personal computer Operating Systems benefit from network effects? Include in your explanation a brief discussion of the importance of Apple’s large installed base of current users. How do these network effects and this installed base create an entry barrier into the computer Operating System market?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT