How to fix loop not working using execv and forks - c++

When running this program after the first iteration the program stops. This is because of execv function. What can I do so my loop still continues on until the user types quit.
I have tried creating a fork process before doing the execv in the child process but that does not work.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;
int main(){
int pipefd[2];
int rs;
pid_t cpid;
pid_t cpid2;
rs = pipe(pipefd);
char* args1[256];
char* args2[256];
if (rs < 0){
perror("pipe");
exit(1);
}
char cmd1[256];
char cmd2[256];
char path1[10];
char path2[10];
while(true){
cout << "Command 1";
cin.getline(cmd1,256);
cout << "command 2";
cin.getline(cmd2,256);
if (strcmp(cmd1,"quit") == 0)
break;
if (strcmp(cmd2,"quit") == 0)
break;
char *token;
token = strtok(cmd1," ");
int i=0;
while(token != NULL){
args1[i] = token;
token = strtok(NULL, " ");
i++;
}
args1[i] = NULL;
token = strtok(cmd2," ");
i = 0;
while(token != NULL){
args2[i] = token;
token = strtok(NULL, " ");
i++;
}
args2[i] = NULL;
strcpy(path1,args1[0]);
strcpy(path2,args2[0]);
rs = fork();
if (rs < 0){
perror("Fork");
exit(1);
}
if (rs == 0){//child process
close(pipefd[1]);
close(0);
dup(pipefd[0]);
close(pipefd[0]);
rs = execvp(path2,args2);
if (rs < 0){
perror("execl");
exit(EXIT_FAILURE);
}
}
else{//PARENT PROCESS
close(pipefd[0]);
close(1);
dup(pipefd[1]);
close(pipefd[1]);
wait(&rs);
rs = execvp(path1,args1);
if (rs < 0){
perror("execl");
exit(EXIT_FAILURE);
}
}
};
return 0;
}
After outputting the answer the function should then ask the user for two more commands, This should go on until the user types in quit.

First of all exit(EXIT_FAILURE) is making your program exit.So remove it.
And you can put the possible exception creating code in try {} catch {} block.
In your case like this..
try{
rs = execvp(path1,args1);
if (rs < 0){
perror("execl");
//exit(EXIT_FAILURE);
}
}catch(int y){
continue;//here you can put your handling logic
}
By this you will get the desired output.

Related

dup() creating file but not printing to it

I am trying to create a shell in c++. It creates a child process which executes a command and pipes the response back to the parent. I want to specify if the second argument of a command is -o then I would like to redirect the output of the command to a file. (output.txt).I used dup() to redirect output to my file. However, when I run the program and enter for example wc -o fileName the program creates the file output.txt but does not write to it when I specify to print the result of my child process.
#include <iostream>
#include <unistd.h>
#include <stdio.h>
#include <cstring>
#include <fcntl.h>
#include <vector>
#include <sys/wait.h>
int main(){
// array of file descriptors for parent and child
int filedes[2];
char foo[4096];
char** argv;
std::cout << "$$-> ";
char command[128];
std::cin.getline(command, 128);
if(strlen(command) != 0) {
std::vector<char *> args;
char *prog = strtok(command, " ");
char *tmp = prog;
while(tmp != NULL) {
args.push_back(tmp);
tmp = strtok(NULL, " ");
}
argv = new char *[args.size() + 1];
for (int k = 0; k < args.size(); k++) {
argv[k] = args[k];
}
argv[args.size()] = NULL;
}
char* newargc = argv[0];
char *newargv[] = {newargc,argv[2],NULL};
if(pipe(filedes) < 0){
std::cout << "There was an error creating the pipe";
}
int pid = fork();
if(pid == 0){
// writing to the pipe
// close read end of pipe
close(filedes[0]);
close(STDOUT_FILENO);
dup(filedes[1]);
if(strcmp(argv[1],(char*)"-o") == 0 ||strcmp(argv[1], (char*) "-b") == 0){
execvp(newargv[0], newargv);
}
else{
execvp(argv[0],argv);
}
}
else if (pid > 0) {
std::cout << "This is the parent process\n";
while(wait(NULL) > 0);
close(filedes[1]);
int output_fd = open("output.txt", O_CREAT, O_TRUNC, O_RDWR);
read(filedes[0], foo, sizeof(foo));
if(strcmp(argv[1],(char*)"-o") == 0){
close(STDOUT_FILENO);
dup(output_fd);
write(output_fd, foo, sizeof(foo));
}
else if(strcmp(argv[1], (char*) "-b") == 0){
int stdoutHolder = dup(STDOUT_FILENO);
close(STDOUT_FILENO);
dup(output_fd);
std::cout<< foo;
dup2(stdoutHolder, 1);
}
std::cout << foo;
}
//pid is less than 0 if error
else{
std::cout << "There is an error.";
}
return 0;
}

While loop not continuing after fork and execvp calls

After running my program for the first time around it runs correctly but the loop does not continue.
I have tried adding more forks into my function but it seems to not work.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;
int main(){
int pipefd[2];
int rs;
pid_t cpid;
char* args1[256];
char* args2[256];
char cmd1[256];
char cmd2[256];
char path1[10];
char path2[10];
//starts while loop
while(true){
//creates pipe
rs = pipe(pipefd);
if (rs < 0){
perror("pipe");
exit(1);
}
//gets comands from user
cout << "Command 1";
cin.getline(cmd1,256);
cout << "command 2";
cin.getline(cmd2,256);
//checks id with commands are quit
if (strcmp(cmd1,"quit") == 0)
break;
if (strcmp(cmd2,"quit") == 0)
break;
char *token;
token = strtok(cmd1," ");
int i=0;
//splits char arrays up
while(token != NULL){
args1[i] = token;
token = strtok(NULL, " ");
i++;
}
args1[i] = NULL;
token = strtok(cmd2," ");
i = 0;
while(token != NULL){
args2[i] = token;
token = strtok(NULL, " ");
i++;
}
args2[i] = NULL;
strcpy(path1,args1[0]);//copis the command to the path file
strcpy(path2,args2[0]);
//forks and creates child process
rs = fork();
if (rs == 0){//child process
close(pipefd[1]);//close write end of pipe
close(0);//close standard input
dup(pipefd[0]);//duplicate read end of pipe into standard
input
close(pipefd[0]);//close read end of pipe
rs = execvp(path2,args2);//runs program 2
if (rs < 0){
perror("execl");
exit(1);
}
}
else{//PARENT PROCESS
close(pipefd[0]);//close read end of pipe
close(1);//close standard input
dup(pipefd[1]);//duplicate write end of pipe into standard
input
close(pipefd[1]);//clsoe write end of pipe
rs = execvp(path1,args1);//runs command 1
if (rs < 0){
perror("execl");
exit(1);
}
}
}
return 0;
}
After going through the loop the first time the user should be asked for enter in two more commands or be able to quit out of the function
rs = execvp(path1,args1);//runs command 1
This line in the "parent process" replaces the current program. There is no while loop after this succeeds anymore, only program 1.
Think of it this way. When user inputs m pairs of commands to your program, how many processes do you expect to be spawned? You expect a total of 2m processes each corresponding to a command but you only fork m times each corresponding to an iterations of the while loop in your current code.
You should instead fork a different process for program 1 as well, similar to how you did it for program 2.

c++ Process hangs when piping between two child processes

I am trying to pipe data from one child process to another. When I run this, it hangs. If I don't make it wait for the first child process, it goes back to the top of the loop prompting for commands without giving the expected output, and when I prompt it to quit, it dumps all of the output I was expecting. I had it working with just one child process, but then the second execvp killed the parent process, and I didn't get back to the top of the loop prompting for more commands.
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
using namespace std;
int main(int argc, const char * argv[]) {
bool quit=0;
char quitArray[] = "quit";
int pipeReturnValue, fork1ReturnValue, fork2ReturnValue, pipefd[2], checkForQuit;
//Enter a loop where each iteration prompts for two single-line inputs
while (!quit) {
//Get command 1
char command1[128];
printf("Enter command 1: ");
fgets(command1,128,stdin);
command1[strlen(command1) -1] = 0;
//Exit if user enters quit
checkForQuit = strncmp(command1, quitArray, 4);
if (checkForQuit == 0) {
exit(0);
}
//Get command 2
char command2[128];
printf("Enter command 2: ");
fgets(command2,128,stdin);
command2[strlen(command2) -1] = 0;
//Exit if user enters quit
checkForQuit = strncmp(command2, quitArray, 4);
if (checkForQuit == 0) {
exit(0);
}
//Open pipe
pipeReturnValue = pipe(pipefd);
if (pipeReturnValue < 0) {
perror("Pipe failed");
exit(1);
}
//Fork 1
fork1ReturnValue = fork();
if(fork1ReturnValue < 0) {
perror("Fork failed");
exit(1);
}
else if (fork1ReturnValue == 0) {
//Fork 2
fork2ReturnValue = fork();
if (fork2ReturnValue < 0) {
perror("Fork 2 failed");
}
else if (fork2ReturnValue == 0) {
//close read end of pipe
close(pipefd[0]);
//parse command 1 arguments
//store tokens in array
char *arguments[6] = {};
arguments[0] = strtok(command1, " ");
int tokenCounter = 0;
while (arguments[tokenCounter] != NULL) {
tokenCounter++;
arguments[tokenCounter] = strtok(NULL, " ");
}
//dup stdo to pipe
dup2(pipefd[1], 1);
//execute arguments
execvp(arguments[0], arguments);
}
else {
wait(&fork2ReturnValue);
//close write end of pipe
close(pipefd[1]);
//parse command 2 arguments
//store tokens in array
char *arguments[6] = {};
arguments[0] = strtok(command2, " ");
int tokenCounter = 0;
while (arguments[tokenCounter] != NULL) {
tokenCounter++;
arguments[tokenCounter] = strtok(NULL, " ");
}
//dup stdin to pipe
dup2(pipefd[0], 0);
//exec
execvp(arguments[0], arguments);
}
}
else {
wait(&fork1ReturnValue);
}
}
return 0;
}
I finally figured it out. I needed to open the pipe after the first fork rather than before.
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
using namespace std;
int main(int argc, const char * argv[]) {
bool quit=0;
char command1[128],
command2[128],
quitArray[] = "quit";
int pipeReturnValue,
fork1ReturnValue,
fork2ReturnValue,
checkForQuit,
pipefd[2];
//Loop where each iteration prompts for two single-line inputs
while (!quit) {
//Get command 1
printf("Enter command 1: ");
fgets(command1,128,stdin);
command1[strlen(command1) -1] = 0;
//Exit if user enters quit
checkForQuit = strncmp(command1, quitArray, 4);
if (checkForQuit == 0) {
quit = 1;
exit(0);
}
//Get command 2 and trim trailing new line character
printf("Enter command 2: ");
fgets(command2,128,stdin);
command2[strlen(command2) -1] = 0;
//Exit if user enters quit
checkForQuit = strncmp(command2, quitArray, 4);
if (checkForQuit == 0) {
quit = 1;
exit(0);
}
//Fork to create 1st child process, return error if fork fails
fork1ReturnValue = fork();
if(fork1ReturnValue < 0) {
perror("Fork 1 failed");
exit(1);
}
//Open pipe, return error if fork fails
pipeReturnValue = pipe(pipefd);
if (pipeReturnValue < 0) {
perror("Pipe failed");
exit(1);
}
//First child process
else if (fork1ReturnValue == 0) {
//Fork to create 2nd child process, return error if fork fails
fork2ReturnValue = fork();
if (fork2ReturnValue < 0) {
perror("Fork 2 failed");
}
//Second child process
else if (fork2ReturnValue == 0) {
//close read end of pipe
close(pipefd[0]);
//Parse command 1 arguments, store tokens in an array
char *arguments[6] = {};
arguments[0] = strtok(command1, " ");
int tokenCounter = 0;
while (arguments[tokenCounter] != NULL) {
tokenCounter++;
arguments[tokenCounter] = strtok(NULL, " ");
}
//Dup standard output to write side of pipe
dup2(pipefd[1], 1);
//Execute arguments from command 1
execvp(arguments[0], arguments);
}
//First child code continued
else {
//Wait for child 2 to to terminate
wait(&fork2ReturnValue);
//Close write end of pipe
close(pipefd[1]);
//Parse command 2 arguments, store tokens in array
char *arguments[6] = {};
arguments[0] = strtok(command2, " ");
int tokenCounter = 0;
while (arguments[tokenCounter] != NULL) {
tokenCounter++;
arguments[tokenCounter] = strtok(NULL, " ");
}
//dup standard input to read side of pipe
dup2(pipefd[0], 0);
//Execute arguments from command 2
execvp(arguments[0], arguments);
}
}
//Parent process continued
else {
//Wait for child 1 to terminate
wait(&fork1ReturnValue);
}
//return to top of loop
}
return 0;
}

Fork: Resource temporarily unavailable when running shell with one arg

I am trying to write a microshell in C++ that will take in 1 or 2 args and run them in UNIX. My shell takes two args split by || fine, but when I run only one I get a massive fork error. My shell will look for || as a pipe instead of just |. Thank you in advance!
Some Functional commands are:
cat filename || sort
ls -l || less
Code:
#include <iomanip>
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <stdio.h>
using namespace std;
void getParms (char[], char* [], char* []);
int main()
{
char command[160];
pid_t pid1 = 1, pid2 = 1;
cout << "myshell> ";
cin.getline(command, 160);
while (strcmp(command, "q") != 0 && strcmp(command, "quit") != 0 && pid1 > 0 && pid2 > 0)
{
char* arg1[6];
char* arg2[6];
char path1[21], path2[21];
int pipefd[2];
arg1[0]=NULL;
arg2[0]=NULL;
getParms(command, arg1, arg2);
if (pipe(pipefd) < 0)
{
perror ("Pipe");
exit (-1);
}
//cerr <<"This is arg2"<<arg2[0]<<endl;
pid1 = fork();
if (pid1 < 0)
{
perror ("Fork");
exit (-1);
}
if (pid1 == 0)
{
//cout<<"Child 1"<<endl;
//cerr<<arg1[0]<<endl;
if(arg2[0] != NULL)
{
close(pipefd[0]);
close(1);
dup(pipefd[1]);
close(pipefd[1]);
}
strcpy(path1, "/bin/");
strcat(path1, arg1[0]);
if (execvp(path1, arg1) < 0)
{
strcpy(path1, "/usr/bin/");
strncat(path1, arg1[0], strlen(arg1[0]));
if (execvp(path1, arg1) < 0)
{
cout<<"Couldn't execute "<<arg1[0]<<endl;
exit (127);
}
}
if(arg2[0]== NULL)
{ // Parent process
close (pipefd[0]); //read
close (pipefd[1]); //write
waitpid(pid1, NULL, 0); // Waits for child2
cout << "myshell> ";
cin.getline(command, 160);
}
}
else if(arg2[0] != NULL)
{
//cerr<<"Child 2"<<endl;
pid2 = fork();
if (pid2 < 0)
{
perror ("Fork");
exit (-1);
}
if (pid2 == 0)
{
close(pipefd[1]);
close(0);
dup(pipefd[0]);
close(pipefd[0]);
strcpy(path2, "/bin/");
strncat(path2, arg2[0], strlen(arg2[0]));
if (execvp(path2, arg2) < 0)
{
strcpy(path2, "/usr/bin/");
strncat(path2, arg2[0], strlen(arg2[0]));
if (execvp(path2, arg2) < 0)
{
cout<<"Couldn't execute "<<arg2[0]<<endl;
exit (127);
}
}
}
else
{ // Parent process
//cerr<<"in last 2 else"<<endl;
close (pipefd[0]); //read
close (pipefd[1]); //write
waitpid(pid2, NULL, 0); // Waits for child2
cout << "myshell> ";
cin.getline(command, 160);
}
}
}
return 0;
}
/****************************************************************
FUNCTION: void getParms (char [], char* [], char* [])
ARGUMENTS: char str[] which holds full command
char* args[] args2[] which will hold the individual commands
RETURNS: N/A
****************************************************************/
void getParms(char str[], char* args[], char* args2[])
{
char* index;
int i= 0;
int j= 0;
index = strtok(str, " ");
//cerr<<"before first while"<<endl;
// While the token isn't NULL or pipe
while (index != NULL && strstr(index,"||") == NULL)
{
args[i] = index;
index = strtok(NULL, " ");
i++;
}
args[i] = (char*) NULL; // makes last element Null
//cerr<<" getParms before ||"<<endl;
if(index != NULL && strcmp(index,"||") != 0)
{
//cerr<<"after checking for ||"<<endl;
index = strtok(NULL," ");
while (index != NULL)
{
args2[j] = index;
index = strtok(NULL," ");
j++;
}
}
//cerr<<"After second IF"<<endl;
args2[j] = (char*) NULL; // makes last element Null
}
Your problem is that the main while loop is not going to any of the if-else statements in which you have the prompt for another command - the same statement is executed over and over. When you use the double pipe it goes to else if(arg2[0] != NULL) and the parent process shows a new prompt.
Try removing both prompts for a command from the main while loop in your if-else statement and move the prompt to the beginning of the loop like this:
//Move these two below into the while loop
//cout << "myshell> ";
//cin.getline(command, 160);
while (strcmp(command, "q") != 0 && strcmp(command, "quit") != 0 && pid1 > 0 && pid2 > 0)
{
cout << "myshell> ";
cin.getline(command, 160);
//...
}
Try not to make such redundant calls of the same thing. If you have a couple of those and you need to change something it can get messy.

Linux - Shell does not redirect outputs

I created a simple shell in Linux using fork() and execvp(). It works fine with cat, ls etc. but when I try to redirect its output like ./hello.o > output.txt it doesn't work.
I am guessing I didn't provide the write path to look for the definitions. My shell is currently searching on /bin/ path where most of the commands are stored.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define ARG_SIZE 100 // MAX LENGTH FOR ARGUMENTS
#define PATH "/bin/" // PATH FOR ARGUMENTS
int main() {
char inputLine[BUFSIZ];
char *argv[ARG_SIZE];
// for path + argv
char programPath[200];
while (1) {
printf("myshell> ");
// check if ctrl + D is pressed
if (fgets(inputLine, BUFSIZ, stdin) == NULL)
break;
inputLine[strlen(inputLine) - 1] = '\0';
// check if exit is typed
if (strcmp(inputLine, "exit") == 0)
break;
int i = 0;
argv[0] = strtok(inputLine, " \n");
for (i = 0; argv[i] && i < ARG_SIZE-1; ++i)
argv[++i] = strtok(NULL, " \n");
// create a fork call
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(1);
}
// parent
if (pid != 0) {
wait();
// child
} else {
strcat(programPath, argv[0]);
// will not return unless it fails
execvp(programPath, argv);
perror("execvp");
exit(1);
}
}
return 0;
}