새소식

infra

* Pipe를 이용한 fork 사용 example

  • -
728x90

#include <stdio.h>

#include<stdlib.h>

#define MSGSIZE 16

char *msg1 = “hello, world #1”;

char *msg2 = “hello, world #2”;

char *msg3 = “hello, world #3”;

main()

{    char inbuf[MSGSIZE];       

      int p[2], j, pid;

      if (pipe(p) < 0 ) 

         {

            printf(“pipe call error\n”); exit(1);

          }

      if ((pid = fork() ) < 0 )

        {   printf(“fork error\n”);

             exit(1);

         }

      if (pid > 0 )

        {    close(p[0]);

              write(p[1], msg1, MSGSIZE);

              write(p[1], msg2, MSGSIZE);

              write(p[1], msg3, MSGSIZE);

              wait((int *) 0 );

         }

 if (pid ==0 )

   {

       close(p[1]);

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

         {

             read(p[0], inbuf, MSGSIZE);

             printf(“%s\n”, inbuf);

          }

   }

  exit(0);

}

*  Pipe를 이용한 fork 사용 example


a.out으로 그냥 실행

         

반응형
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.