我很初學linux,但是我設法做我自己的shell。現在是時候添加管道了。 (這就是功課所說的)。任何人都可以解釋我多一點怎麼做?我知道理論上它應該這樣工作。使用c編程的Linux管道。通過管道重定向輸入/輸出。自己的外殼
unsigned char* child_results; //buffer to save results from child processes
for (int i = 0; i < 2; i++) {
pid = fork();
if (pid == 0) {
//if it's the first process to launch, close the read end of the pipe
//otherwise read what the parent writes to the pipe and then close the
//read end of the pipe
//call execvp()
}
else {
//if you've launched the first process, close the write end of the pipe
//otherwise write the contents of child_result to the child process
//and then close the write end of the pipe
//read from the child's pipe what it processed
//save the results in the child_results buffer
wait(NULL); //wait for the child to finish
}
}
但是,我不能得到它的工作。我整天都在這樣做,仍然沒有。我明白這個主意,但我無法得到它的工作。有人能幫助我嗎?這裏是我的管道部分的代碼:
for (int i = 0; i <= pipeline_count; i++) {
int pdesc[2];
// creating pipe
pipe(pdesc);
int b = fork();
// child
if (b == 0) {
// 1st pipeline
if (i == 0) {
//<?>
}
// last pipeline
if (i == pipeline_count) {
//<?>
}
// inside pipeline
if (i > 0 && i < pipeline_count) {
//<?>
}
execvp(array[0], array);
}
else {
// parent
//<?>
wait(NULL);
}
}
,這裏是一個shell命令的例子
ls -al | tr a-z A-Z
感謝
閱讀http://advancedlinuxprogramming.com/ –