私は割り当てに取り組んでおり、他のプログラムがさまざまな機能を処理できるようにパイプを作成する必要があります。コマンドラインから問題なくパイプできます。簡単です。ただし、dup2 と execl を使用するのは、私にとってトリッキーでした。ある時点で、プログラムの一部から出力を取得できましたが、別の部分からは何も読み取れませんでした。
ここに私が持っているものがあります:
パイプライン.cpp
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <cstdlib>
#include <iostream>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include<iostream>
#include<cstdlib>
#include<unistd.h>
#include<iomanip>
#include <sys/wait.h>
using namespace std;
int main(int argc, char *argv[])
{
int number = atoi(argv[1]);
int x2ypipe[2];
pipe(x2ypipe);
if(x2ypipe==0){
cout<<"ERROR:"<<errno<<endl;
}
pid_t xchild =fork();
if(xchild==0){
dup2(x2ypipe[1],STDOUT_FILENO);
close(x2ypipe[0]);
close(x2ypipe[1]);
execl("./part1.cpp","part1.cpp", (char *)NULL);
}
pid_t ychild =fork();
if(ychild==0){
dup2(x2ypipe[0],STDIN_FILENO);
close(x2ypipe[0]);
close(x2ypipe[1]);
execl("./part2.cpp", "part2.cpp", (char *)NULL);
}
close(x2ypipe[0]);
close(x2ypipe[1]);
wait(NULL);
wait(NULL);
part1.cpp
#include<iostream>
#include<cstdlib>
#include<unistd.h>
#include<iomanip>
using namespace std;
int main(int argc, char *argv[])
{
int number = atoi(argv[1]);
for (int k = 1; k <= 9; k++)
{
cout << k << " " << flush;
sleep(1);
}
return 0;
}
part2.cpp
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <iomanip>
using namespace std;
int main()
{
int number;
while (cin >> number)
{
cout << 2 * number - 1 << " " << flush;
}
return 0;
}
わかりましたので、pipeline.cpp : 2 回フォークし、2 つの子の間にパイプを作成します。次に、それぞれが Excel を使用して、そのプロセスをプログラム part1 と part2 に置き換えます。私の理解では、part1 プログラムが実行され、それが出力するものはすべて、part2 を実行する 2 番目の子によって取得され、そこから、出力記述子が変更されていないため、パート 2 が正常に出力されるということです。ここで何かを見逃したり、誤用したりしていますか?