當前位置:編程學習大全網 - 編程軟體 - 編寫-段C語言程序便其完成:父子進程通過無名管道傳遞三條消息: 管道文件的測試程序開始; 管道文件測試

編寫-段C語言程序便其完成:父子進程通過無名管道傳遞三條消息: 管道文件的測試程序開始; 管道文件測試

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <string.h>

#define BUFFSIZ sizeof("Start the test of pipe file")

main( )

{

int i,f_des[2];

char message[3][BUFFSIZ]={"Start the test of pipe file","Test is going",

"Test ends"};

if(pipe(f_des)==-1)

{

printf("pipe");

exit(2);

}

switch(fork( ))

{

case -1:

printf("Fork");

exit(3);

case 0:

close(f_des[1]);

for(i=0;i<3;i++)

{

if(read(f_des[0],message[i],BUFFSIZ)!=-1)

{

printf("message received by child:[%s]\n",message[i]);

fflush(stdout);

}

else

{

printf("Read Failed");

exit(4);

}

}

break;

default:

close(f_des[0]);

for(i=0;i<3;i++)

{

if(write(f_des[1],message[i],BUFFSIZ)!=-1)

{

printf("message sent by parent:[%s]\n",message[i]);

fflush(stdout);

}

else

{

printf("rite Failed");

exit(5);

}

}

}

exit(0);

}

  • 上一篇:室外設計軟件叫什麽城市
  • 下一篇:python自動化運維是做什麽的
  • copyright 2024編程學習大全網