统计失败:-:错误的文件描述符
stat failed: -: Bad file descriptor
我正在实施 shell 、管道和叉子。我基本上是在同一个 parent 的两个 child 进程之间传递消息。个别命令的工作,但管道命令不工作我收到错误文件描述符错误。谁能帮我解决这个问题?
以下是我的代码。
/* 分叉 parent */
if ((pid = fork()) <0)
cout<<"fork error";
/* 如果是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';
}
}
/* 在 parent 等待 child 完成 */
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";
}
您需要在调用 fork
之前创建管道。如果 parent 和 child 各自都是 pipe
,它们将各自创建一个不同的管道。您需要两个进程之间的共享管道。
我正在实施 shell 、管道和叉子。我基本上是在同一个 parent 的两个 child 进程之间传递消息。个别命令的工作,但管道命令不工作我收到错误文件描述符错误。谁能帮我解决这个问题?
以下是我的代码。
/* 分叉 parent */
if ((pid = fork()) <0)
cout<<"fork error";
/* 如果是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';
}
}
/* 在 parent 等待 child 完成 */
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";
}
您需要在调用 fork
之前创建管道。如果 parent 和 child 各自都是 pipe
,它们将各自创建一个不同的管道。您需要两个进程之间的共享管道。