天天看點

Linux有名管道FIFO的簡單應用(一程式擷取另外一程式的執行結果)

設計兩個程式,一個hello程式,輸出”Hello, I am process 程序id”;另一程式獲得hello程式的輸出結果并顯示。可用pipe或popen和pclose函數實作。

3_7_A:

#include<stdio.h>

#include<string.h>

#include<unistd.h>

int main()

{

    printf("I am process %d.", getpid());

    return 0;

}

3_7_B:

#include<stdio.h>

#include<string.h>

#include<unistd.h>

int main()

{

    FILE* fp;

    fp = popen("./E3_7_A", "r");

    char buf[30];

    if(fp != NULL)

     {

      puts("Congratulations!\n");

      fgets(buf, sizeof(buf), fp);

      puts(buf);

     }

    else

     {

      puts("Failure.\n");

     }

    return 0;

}

Linux有名管道FIFO的簡單應用(一程式擷取另外一程式的執行結果)

繼續閱讀