Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Purpose Exercise the fork, exec, pipe and dup system calls. Assignment Write a C

ID: 3680450 • Letter: P

Question

Purpose Exercise the fork, exec, pipe and dup system calls. Assignment Write a C++ program that keeps prompting for 2 UNIX commands that are then executed in a pipe arrangement. Program Implement a C++ program that prompts the user for 2 commands. Each input string should be a UNIX command, with arguments allowed. For example, input 1 could be "Is -1" and input 2 could be "wc -1". The program will then create a pipe, and two child processes. The first child process will run the command specified in the first input. It will output to the pipe instead of standard output. The second child process will run the command specified in the second input. It will take its input from the pipe rather than standard input. The parent process will wait on its two children to complete, then the whole thing will repeat. Execution will stop when "quit" is entered as the first command. Error Checking Execute the command and its arguments as the user has entered it. Use one of the "exec" library functions to execute it. Use the "perror" library function to report any error if "exec" fails. Other Points make sure that your assignment is contained in a single file called "zl23456.cxx" based on your Z-id make sure that your program compiles, links and runs fine on your Linux system, tilling or hopper. Submission Submit your C++ source code file via Blackboard.

Explanation / Answer

Hi below i have written the code for your reference,

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <cstring>
#include <stdlib.h>
#include <iostream>
using namespace std;

int main()
{
//Variables
string command1, command2;
char quit[]=”quit”;

cout << “Enter your first command (including arguments) or quit to exit: “;
cin >> command1;

while(command1!=”quit”)
{
cout << “Enter your second command (including arguments): “;
cin >> command2;

//Create pipe
int pipefd[2];
int rs=pipe(pipefd);
//error check pipe
if(rs==-1)
{
perror(“pipe”);
exit(EXIT_FAILURE);
}

//fork into two processes
rs=fork();
//error check fork
if(rs==-1)
{
perror(“pipe”);
exit(EXIT_FAILURE);
}

//child process
if(rs==0)
{
//close read end of pipe, keep write end open
close(pipefd[0]);
//close standard output
close(1);
//duplicate write end of pipe into standard output
dup(pipefd[1]);
//close write end of pipe
close(pipefd[1]);
//run the first command
rs=execlp(command1.c_str(), command1.c_str(), (char*)NULL);
//error check execlp
if(rs==-1)
{
perror(“execlp”);
exit(EXIT_FAILURE);
}
}

//parent process
else
{
//close write end of pipe, keep read open
close(pipefd[1]);
//close standard input
close(0);
//duplicate read end of pipe into standard input
dup(pipefd[0]);
//close read end
close(pipefd[0]);
//fork into two processes
rs=fork();
//error check the fork
if(rs==1)
{
perror(“pipe”);
exit(EXIT_FAILURE);
}

//child process 2
if(rs==0)
{
//run second command
rs=execlp(command2.c_str(), command2.c_str(), (char*)NULL);
//error check execlp
if(rs==-1)
{
perror(“execlp”);
exit(EXIT_FAILURE);
}
}

//parent process
else
{
wait(NULL);
wait(NULL);
}

}
cout << “Enter your first command (including arguments) or quit to exit: “;
cin >> command1;
}
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote