shaaraddalvi
September 29th, 2012, 03:17 PM
I actually wanted to learn about the pipe() command and so I thought I'll fork() a child, take input of an integer array from user in parent process, send that array to child using pipe() and and print the array via child process.
While doing this, I am struck with taking input of the array in the parent process at the first place...Whenever I dynamically create an array, say of 5 elements, it takes input of only the first element via the for loop, and as soon as I press the enter key to enter the second element, the program closes. The for loop should be completed..but the program is ending..
Any help would be ace...thanks in advance.. :)
#include<unistd.h>
#include<iostream>
using namespace std;
int main()
{
fork();
int fd[2],pid,i;
int * array;
pid=fork();
if(pid==0)
{
int tot;
cin>>tot;
array=new int[tot];
for(i=0;i<tot;i++)
{
cout<<"Enter "<<(i+1)<<" element : ";
cin>>array[i];
}
}
}
While doing this, I am struck with taking input of the array in the parent process at the first place...Whenever I dynamically create an array, say of 5 elements, it takes input of only the first element via the for loop, and as soon as I press the enter key to enter the second element, the program closes. The for loop should be completed..but the program is ending..
Any help would be ace...thanks in advance.. :)
#include<unistd.h>
#include<iostream>
using namespace std;
int main()
{
fork();
int fd[2],pid,i;
int * array;
pid=fork();
if(pid==0)
{
int tot;
cin>>tot;
array=new int[tot];
for(i=0;i<tot;i++)
{
cout<<"Enter "<<(i+1)<<" element : ";
cin>>array[i];
}
}
}