Question: For this assignment, you will be writing a single program that enters a loop in which each iteration prompts the user for two, single-line inputs.
For this assignment, you will be writing a single program that enters a loop in which each iteration prompts the user for two, single-line inputs. If the text of either one of the inputs is quit, the program should immediately exit. If quit is not found, each of these lines of input will be treated as a command line to be executed. These two commands should be executed as if the user had typed command1 | command2 at the shell prompt, meaning that the standard output of the first command gets redirected into the standard input of the second command. (It should be noted that using cin >> will not grab a whole line; you will need to use another function instead.) You will need to be able to split the text entered by the user into the different command line arguments that comprise it. I do not care how this is accomplished, but some possibilities are to use the C function strtok, or a C++ istringstream object. You do not need to worry about escaping special characters when splitting the string for the purposes of this assignment; the split can be performed based on spaces alone.After these commands have both finished executing, your program should prompt for another pair of input commands to run, repeating the process over and over again until the user gives quit as input.
Requirements
-You may not use the system function to do this. You must make your own child processes, do the appropriate changes to the file descriptors in each of them, and have them use one of the exec calls to run the commands given by the user.
-Use the pipe system call to create the file descriptors used to communicate between the two programs that are run.
-If an error occurs at any point, there should be an appropriate error message printed to the standard error stream.
-Your program must be able to handle user-specified commands that are up to 127 characters long.
-Your program must be able to handle user-specified commands that contain up to five command-line arguments.
-Make sure to close unused pipe file descriptors in all process. If you do not, your program is likely to stall.
-Do not forget that this program involves a loop. After both programs have finished running (but not until), the program should go back to the beginning and prompt for two more commands to run.
My code is not looping properly please help
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main(int argc, char const *argv[])
{
int pfd[2];
pipe(pfd);
char op = 'y';
char cmdn1[10][20] = {0}; // saparate it
char cmdn2[10][20] = {0}; // saparate it
char * argv1[10] ; // give it to argv
char * argv2[10] ; // give it to argv
int i , j = 0 , k = 0;
char a[20]={0};
int wordc1 = 0;
int wordc2 = 0;
string quit;
while (quit[0] != 'Q' || quit[0] != 'q')
{
string string1, string2;
cout << "enter first string ";
getline(cin, string1);
cout << "enter second string ";
getline(cin, string2);
for( i =0 ; i<10;i++)
argv1[i] = NULL; // in the starting null all pointer
k = 0, j = 0 , i = 0;
for(i=0;i <= string1.size() ; i++)
{
if(string1[i] == ' ' || string1[i] == '\0') // saparate command
{
a[j] = '\0';
strcpy(cmdn1[k++],a);
argv1[k-1] = cmdn1[k-1]; // allocate each saparate argument to argv
j = 0;
wordc1++;
memset(a,0,sizeof(a));
}
else
a[j++] = string1[i];
}
for( i =0 ; i<10;i++)
argv2[i] = NULL; // in the starting null all pointer
k = 0, j = 0 , i = 0;
for(i=0;i <= string2.size() ; i++)
{
if(string2[i] == ' ' || string2[i] == '\0') // saparate command
{
a[j] = '\0';
strcpy(cmdn2[k++],a);
argv2[k-1] = cmdn2[k-1]; // allocate each saparate argument to argv
wordc2++;
j = 0;
memset(a,0,sizeof(a));
}
else
a[j++] = string2[i];
}
if (fork() == 0)
{
// child gets here and handles "grep Villanova"
// replace standard input with input part of pipe
dup2(pfd[0], 0);
// close unused hald of pipe
close(pfd[1]);
// execute grep
execvp(argv2[0], argv2);
}
else
{
// parent gets here and handles "cat scores"
// replace standard output with output part of pipe
dup2(pfd[1], 1);
// close unused unput half of pipe
close(pfd[0]);
// execute cat
execvp(argv1[0], argv1);
}
cin.clear();
cin.ignore(255,' ');
cout << "Do you want to quit (quit/no)? "; //this part not working .
cin >> quit;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
