CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2012
    Posts
    9

    Give output to child process

    I'm trying to build a basic shell for Unix.
    This is the code I use to create a new process:

    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);
    	}
    }
    Where complexCommand is a function that handles the command (uses execvp after generating the correct string out of argv).
    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:

    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);
    }
    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.
    Is there anything I need to do in the parent process (my shell in this case) to give the child the output?

    Thanks.

  2. #2
    Join Date
    Mar 2004
    Location
    Central Florida
    Posts
    293

    Re: Give output to child process

    You need to post the actual project code that has the issue.

    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);
    }
    This is a complete program and funcitons as expected. The other code block is a snippet from some fiunction and is not used.

    From a design perspective, you should provide some mechanism for gracefully exiting the applicaiton.
    Code:
    } while (1);
    Will run forever and is not good design.

    CTRL-C will break the loop but causes an exception.
    "Effective teaching is the essence of leadership..."

    "There is no substitute for a carefully thought-out design."

    If you have found this post to be useful, please Rate it.

  3. #3
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Give output to child process

    The code has problems, but to confront your immediate concern,
    instead of scanf try:
    Code:
    cin >> x1 >> x2;
    I am noticing your ambitious goals. Well just make sure that you
    are understanding the basic building blocks of what you are trying
    to accomplish before you do. Are you learning C and trying to learn C++?
    Are you trying to learn OOP? Then I would check out Design Patterns,
    it may help you to understand some tried and proven approaches
    to your program architecture. It may help you to have some more
    background.


    OK HTH,
    Last edited by ahoodin; April 17th, 2012 at 10:04 AM.
    ahoodin
    To keep the plot moving, that's why.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured