I am implementing a shell , pipe, and fork. I am basically passing messages between two child processes of the same parent . Individual command's work but pipe commands don't work I get errors like bad file descriptors . Can anyone help me with this ?
Following is my code .
/* Forking the parent */
if ((pid = fork()) <0)
cout<<"fork error";
/* If it is a child */
else if (pid == 0)
{
/* child */
if(pipe(pipeA)==-1)
cerr<<"Pipe Creation error"<<'\n';
/* Duplicating the output */
dup2(pipeA[1],1);
close(pipeA[0]);
/* Running the first command */
if(execvp(*command1,command1)<0)
{
cout<<"error in executing:"<< command<<'\n';
}
}
/* In the parent waiting till the child completes */
if ( (pid = waitpid(pid, &status, 0)) < 0)
cout<<"waitpid error";
/* calling method, to create second process and execute it */
secondExecute(command2);
}
// Second process will be created here
void secondExecute(char *command2)
{
if ((pid = fork()) <0)
cout<<"fork error";
else if (pid == 0)
{
dup2(pipeA[0],0);
close(pipeA[1]);
/* Executing the second process */
if(execvp(*args,args)<0)
{
cout<<"couldn't execute:"<< command2<<'\n';
}
}
if ( (pid = waitpid(pid, &status, 0)) < 0)
cout<<"waitpid error";
}