Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
05-20 19:12
관리 메뉴

zyint's blog

pipe() 본문

예전글들

pipe()

진트­ 2008. 4. 18. 00:47

pipe()

기능

[1] create an interprocess channel

 

선언

[1]

#include <unistd.h>

int pipe(int fildes[2]);

 

설명

[1]

The pipe() function creates an I/O mechanism called  a  pipe
     and  returns  two file descriptors, fildes[0] and fildes[1].
     The  files  associated  with  fildes[0]  and  fildes[1]  are
     streams and are both opened for reading and writing.
      The O_NDELAY and O_NONBLOCK flags are cleared.

     A read from fildes[0] accesses the data written to fildes[1]
     on  a  first-in-first-out  (FIFO)  basis  and  a  read  from
     fildes[1] accesses the data written to fildes[0] also  on  a
     FIFO basis.

     The FD_CLOEXEC flag will be clear on both file descriptors.

     Upon successful  completion  pipe()  marks  for  update  the
     st_atime, st_ctime, and st_mtime fields of the pipe.


[2]

pipe 를 이용하면 2개의 파일 지시자를 생성할수 있다. 2개가 생성되는 이유는 읽기전용과 쓰기전용의 파이프를 생성하기 위함이다.

filedes[0]은 읽기 전용,

filedes[1]은 쓰기 전용의 파이프로 사용된다.

이들 파이프는 주로 부모프로세스와 자식프로세스간의 통신을 위한 목적으로 사용된다.


RETURN VALUES

[1]

     Upon successful completion, 0 is returned. Otherwise, -1  is
     returned and errno is set to indicate the error.

[2]

성공할경우 0을 실패했을경우에는 -1을 반환하며, 적당한 errno 값을 설정한다

 

ERRORS

[1]

     The pipe() function will fail if:

     EMFILE
           There  are   OPEN_MAX-1  or  more   file   descriptors
           currently open for this process.

     ENFILE
           A file table entry could not be allocated.

 

[2]

EMFILE

너무 많은 파일 디스크립터가 프로세스에 의해 사용되고 있다.

ENFILE

시스템 파일 테이블이 꽉찼을경우

EFAULT

filedes 가 유효하지 못하다.

ENOBUFS

시스템에 연산을 위해서 이용할수 있는 자원이 부족할때

 

 

ATTRIBUTES

[1]

  1.      See attributes(5) for descriptions of the  following  attri-
         butes:

         ____________________________________________________________
        |       ATTRIBUTE TYPE        |       ATTRIBUTE VALUE       |
        |_____________________________|_____________________________|
        | MT-Level                    | Async-Signal-Safe           |
        |_____________________________|_____________________________|

 

NOTES

[1]

     Since a pipe is bi-directional, there are two separate flows
     of data. Therefore, the size (st_size) returned by a call to
     fstat(2) with argument  fildes[0] or fildes[1] is the number
     of  bytes  available for reading from fildes[0] or fildes[1]
     respectively. Previously, the size (st_size) returned  by  a
     call  to fstat() with argument fildes[1] (the write-end) was
     the number of bytes available  for  reading  from  fildes[0]
     (the read-end).

 

예제코드

 [2]

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

    int main()
    {
         int n, fd[2];
         char buf[255];
         int pid;

         if (pipe(fd) < 0)
         {
              perror("pipe error : ");
              exit(0);
         }

         // 파이프를 생성한다. 
         if ((pid = fork()) < 0)
         {
              perror("fork error : ");
              exit(0);
         }
         // 만약 자식프로세스라면 파이프에 자신의 PID 정보를 쓴다.
         else if (pid == 0)
         {
              close(fd[0]);
              while(1)
              {
                   memset(buf, 0x00, 255);
                   sprintf(buf, "Hello : %d\n", getpid());
                   write(fd[1], buf, strlen(buf));
                   sleep(1);
              }
         }

         // 만약 부모프로세스라면 파이프에서 데이타를 읽어들인다. 
          else
         {
              close(fd[1]);
              while(1)
              {
                   memset(buf, 0x00, 255);
                   n = read(fd[0], buf, 255);
                   fprintf(stderr, "%s", buf);
              }
         }
    }

    // 위 프로그램은 파이프를 생성한후 만들어진 파이프를 통해서 자식과 부모가 서로 통신하는 예제이다. fork 하기전에 pipe 를 만들면 된다. fork() 는 특성상 열린파일지시자를 자식에게 상속하기 때문이다.

 

 

출처

  1. Unix manual page for pipe : 스프링노트
  2. 조인시 위키: pipe : 스프링노트

 

 

 

이 글은 스프링노트에서 작성되었습니다.

Comments