簡體   English   中英

pipe - fork - c - 為什么程序無響應且沒有任何錯誤?

[英]pipe - fork - c - why the program is unresponsive without any error?

嗨,我正在編寫一個程序來執行 cat input.txt | 排序 | uniq 在 c 編程中使用 pipe、fork 和 dup2 但該程序似乎根本沒有運行任何東西。 我在開頭放置了一個 printf,緊接着 main() 之后,我仍然看不到任何輸出。我不知道后台發生了什么,或者為什么會發生這種情況?

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

int main(){
    printf("we are here");
    int pipe1[2];
    int pipe2[2];
    if(pipe(pipe1)==-1){
        perror("failed to pipe\n");
        exit(1);
    }


    int process1=fork();

    if(process1<0){
        perror("failed to create the first child1\n");
        exit(1);
    }else if(process1>0){
        if(close(pipe1[0])==-1){
            perror("failed to close the first pipe read\n");
            exit(1);
        }

        if(dup2(pipe1[1],1)==-1){
            perror("failed to duplicate\n");
            exit(1);
        }

        if(close(pipe1[1])==-1){
            perror("failed to close the first pipe write\n");
            exit(1);
        }

        execlp("cat","cat","inputs.txt",NULL);
        perror("failed to exec\n");
        exit(1);
    }else if(process1==0){
        if(pipe(pipe2)==-1){
            perror("failed to pipe\n");
            exit(1);
        }

        int process2=fork();

        if(process2<0){
            perror("failed to create the first child1\n");
            exit(1);
        }else if(process2==0){

            if(close(pipe2[1])==-1){
                perror("failed to close the second pipe write-third process\n");
                exit(1);
            }

            if(dup2(pipe2[0],0)==-1){
                perror("failed to duplicate\n");
                exit(1);
            }

            if(close(pipe2[0])==-1){
                perror("failed to close the second pipe read-third process\n");
                exit(1);
            }

            execlp("uniq","uniq",NULL);
            perror("failed to exec\n");
            exit(1);
        }else if(process2>0){
            if(close(pipe1[1])==-1){
                perror("failed to close the first pipe write\n");
                exit(1);
            }
            if(close(pipe2[0])==-1){
                perror("failed to close the second pipe read\n");
                exit(1);
            }

            if(dup2(pipe1[0],0)==-1){
                perror("failed to duplicate\n");
                exit(1);
            }

            if(dup2(pipe2[1],1)==-1){
                perror("failed to duplicate\n");
                exit(1);
            }

            if(close(pipe1[0])==-1){
                perror("failed to close the first pipe read\n");
                exit(1);
            }

            if(close(pipe2[1])==-1){
                perror("failed to close the second pipe write\n");
                exit(1);
            }

            execlp("sort","sort",NULL);
            perror("failed to exec\n");
            exit(1);
        }
    }


}
  1. printf實際上不會立即打印——它只是將內容放入緩沖區以供稍后打印,無論是在緩沖區填滿或進程結束時,還是在緩沖區中放入換行符時。 因為你沒有做這些事情,它永遠不會出現。 您可以使用fflush(stdout); 刷新緩沖區,如果可能有緩沖數據,這通常是在調用fork之前做的一個好主意——否則緩沖區將被分叉,因此其內容可能會出現兩次。

  2. 在執行“uniq”之前,您永遠不會關閉pipe1中的 pipe1。 由於pipe1是“sort”程序的輸入,它永遠不會得到 EOF,並且 sort 永遠不會終止。 因此,您的程序似乎什么都不做。

  3. “uniq”在孫子中運行並輸出到標准輸出,這(可能?)你的終端。 如果父 (cat) 首先退出(它可能會退出),shell 將再次獲得控制權,並可能將控制終端從“uniq”下切換出來。 這可能會導致 uniq 停止,具體取決於您的終端設置。 默認情況下,在大多數系統上,這不會發生(uniq 會在 shell 提示后愉快地寫入終端,導致可能令人困惑的 output_,但它可能會發生。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM