The problem I am trying to solve is

Write a program using fork() to compute average of N numbers. The parent process forks a child process that takes input from the user, adds all the numbers and returns the sum and the count of numbers to the parent process that divides the sum by the count of the number, and prints out the average.

right now I just have a program where the user enters in a number in the child process and the parent process prints it out just to test things out. The problem I am having is I'm not sure how to send the data to the parent process from the child. If someone could help me out I would appreciate it. Also if I'm going at it with the wrong approach, I would appreciate suggestions as well. My code so far is below.

#include <iostream>
#include <string>

// Required by for routine
#include <sys/types.h>
#include <unistd.h>

#include <stdlib.h>   // Declaration for exit()

using namespace std;



main()
{
   string sIdentifier;

   pid_t pID = fork();
   if (pID == 0)                // child
   {
      sIdentifier = "Child Process:";
      int num;
      // Code only executed by child process
      cout << "Enter a num" << endl;
      cin >> num;

   }
    else if (pID < 0)            // failed to fork
    {
        cerr << "Failed to fork" << endl;
        exit(1);
        // Throw exception
    }
    else                                   // parent

    {
      // Code only executed by parent process

      sIdentifier = "Parent Process:";
      cout << num;
    }

    // Code executed by both parent and child.



}
~
~

Recommended Answers

All 2 Replies

Try creating a pipe. Here's a simple example.

#include <iostream>
#include <unistd.h>

enum PIPES {READ, WRITE};

int main(int argc, char**argv)
{
	int hpipe[2];
	pipe (hpipe);

	if (fork())
	{
		close (hpipe[WRITE]);
		
		int x = 0;

		while (true)
		{
			read (hpipe[READ], (char*)&x, sizeof(int));
			if (x < 0) break;	
			std::cout<<"child recieved->"<<x<<"\n";
		}

		close (hpipe[READ]);
	}
	else
	{
		close (hpipe[READ]);
		const int MINUS = -1;

		for (int i = 0; i < 1000; ++i)
			write (hpipe[WRITE], (char*)&i, sizeof(int));
		write (hpipe[WRITE], (char*)&MINUS, sizeof(int));
		close (hpipe[WRITE]);
	}
	return 0;
}

You need to use vfork(). Here's an example.

#include <iostream>
#include <sys/types.h>
#include <unistd.h>

using std::cout;
using std::endl;
using std::cin;

int main() {

    int temp, avg;
    int num, sum=0;

    /*fork a child process*/
    pid_t pid = vfork();

    if(pid<0){
        cout<<"fork failed"<<endl;
        return -1;
    }
    else if(pid==0){//this is the child process 
        cout<<"Please enter how many numbers to average: "<<endl;
        cin>>num;
        for(int i=0;i<num;++i){
            cout <<"Enter number "<<i+1<<" "<<endl;
            cin>>temp;
            sum+=temp;
        }
        std::exit(sum);
    }
    else{
        wait(NULL);
        cout<<"child complete"<<endl;
        avg=(sum/(num));
        cout<<"The average of the numbers is: "<< avg <<endl;
    }
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.