I'm trying to build a basic shell for Unix.
This is the code I use to create a new process:
Where complexCommand is a function that handles the command (uses execvp after generating the correct string out of argv).Code:pid = fork(); if (pid == 0) { setpgrp(); complexCommand(argv, isComplex); } else if (pid == -1) { //ERROR cout << "smash error: > Creating new process failed. Terminating smash..." << endl; exit(1); } else { jobs.push_back(jobType(argv[0], pid)); if (argv.back().compare("&")) { fgPid = pid; int result; wait(&result); } }
My problem is that after if while in the shell I use a program that needs to print to the screen and handle input, it doesn't work as expected.
To test this, I built this small program:
And when I use the shell I built to run it, the output is only "Enter 2 numbers" for one time. It doesn't print the result, or anything else.Code:#include <iostream> #include <stdio.h> #include <stdlib.h> using namespace std; int main() { int x1, x2; do { cout << "Enter 2 numbers\n"; scanf("%d %d", &x1, &x2); cout << x1 << " + " << x2 << " = " << x1+x2 << ".\n"; } while (1); return(0); }
Is there anything I need to do in the parent process (my shell in this case) to give the child the output?
Thanks.




Reply With Quote