_pipe

읽기 및 쓰기에 대 한 파이프를 만듭니다.

중요중요

이 API를 실행 하는 응용 프로그램에서 사용할 수 있는 Windows 런타임.자세한 내용은 /zw에 지원 되는 CRT 함수.

int _pipe( int *pfds, unsigned int psize, int textmode );

매개 변수

  • pfds[2]
    파일 설명자를 쓰기 읽기 보유 하는 배열입니다.

  • psize
    예약 하는 메모리의 양입니다.

  • textmode
    파일 모드입니다.

반환 값

성공 하면 0을 반환 합니다.오류를 나타내기 위해-1을 반환 합니다.오류, errno 이러한 값 중 하나로 설정 됩니다.

  • EMFILE없음 자세한 파일 설명자를 사용할 수 있음을 나타냅니다.

  • ENFILE시스템 파일 테이블 넘침을 나타냅니다.

  • EINVAL않음을 나타냅니다 어느 배열 pfds 는 null 포인터 또는 잘못 된 값에 대 한 textmode 전달 되었습니다.

이러한 및 다른 반환 코드에 대 한 자세한 내용은 참조 하십시오 errno, _doserrno, _sys_errlist, _sys_nerr.

설명

_pipe 함수를 만듭니다는 파이프, 다른 프로그램으로 정보를 전달 하는 프로그램을 사용 하 여 인위적인 I/O 채널입니다.파이프 파일 포인터는 파일 설명자 또는 둘 다를 하 고 읽기 또는 표준 라이브러리를 사용 하 여 입력 및 출력 함수에 쓸 수 있으므로 파일을 유사 합니다.그러나 특정 파일 또는 장치로 파이프 표시 하지 않습니다.대신, 프로그램의 메모리에 독립적 이며 전적으로 운영 체제에 의해 제어 되는 메모리의 임시 저장소를 나타냅니다.

_pipe유사한 _open 있지만 읽기 및 쓰기에 대 한 파이프를 열고 설명자를 하나가 아니라 두 개의 파일을 반환 합니다.프로그램 파이프의 양쪽 모두를 사용 하거나 닫이 필요가 없는 것입니다.같은 명령이 실행 될 때 파이프 명령 프로세서 Windows에서 생성 합니다. 예를 들어, PROGRAM1 | PROGRAM2.

표준 출력 설명자의 PROGRAM1 파이프의 쓰기 설명자에 연결 되어 있습니다.표준 입력된 설명자의 PROGRAM2 파이프의 읽기 설명자에 연결 되어 있습니다.이 정보를 다른 프로그램으로 전달 하는 임시 파일을 만들 필요가 없습니다.

_pipe 함수에 파이프 두 파일 설명자를 반환 된 pfds 인수.요소 pfds[0] 읽기 설명자 및 요소에 포함 된 pfds[1] 쓰기 설명자를 포함 합니다.파이프 파일 설명자 같은 방식으로 다른 파일 설명자가 사용 됩니다.(저수준 입력과 출력 함수 _read_write 에서 읽고 파이프에 쓸 수 있습니다.) 확인 파이프의 종료 상태를 발견 하는 _read 읽은 바이트 수 만큼 앞으로 0을 반환 하는 요청입니다.

psize 인수는 메모리의 양, 파이프에 예약 (바이트)를 지정 합니다.textmode 인수는 파이프에 대 한 변환 모드를 지정 합니다.매니페스트 상수 _O_TEXT 텍스트 변환과 상수를 지정 합니다. _O_BINARY 이진 변환을 지정 합니다.(참조 하십시오 통해, _wfopen 에 대 한 텍스트 및 이진 모드.) 경우는 textmode 인수는 0에서 _pipe 기본 모드 변수를 통해 지정 된 기본 변환 모드를 사용 하 여 _fmode.

잠금 없음 다중 스레드 프로그램에서 수행 됩니다.반환 된 파일 설명자가 새로 연 후 때까지 모든 스레드에서 참조할 수 없습니다는 _pipe 호출이 완료 됩니다.

사용 하 여 _pipe 부모 프로세스와 자식 프로세스 간에 통신 하는 기능, 각 프로세스 파이프에 열 설명자가 하나만 있어야 합니다.설명자 opposites 이어야 합니다: 열기는 읽기 설명자는 부모가 있는 경우 자식 열에서는 쓰기 설명자가 있어야 합니다.하는 것이이 작업을 수행 하는 가장 쉬운 방법은 OR (|) the _O_NOINHERIT flag with textmode.그런 다음 사용 _dup 또는 _dup2 에 자식 전달할 파이프 설명자는 상속 가능한 복사본을 만듭니다.원래 설명자를 닫고 자식 프로세스를 생성 합니다.폰 호출에서 반환, 부모 프로세스에서 중복 된 설명자를 닫습니다.자세한 내용은이 문서의 뒷부분에 나오는 예제 2를 참조 하십시오.

모든 해당 설명자를 닫을 때 Windows 운영 체제에서 파이프 소멸 됩니다.(모든 읽기 설명자는 파이프를 닫은 경우 다음 파이프에 쓰기 오류가 발생 합니다.) 모든 읽기 및 쓰기 파이프 대기 작업 버퍼 공간이 부족 하 여 I/O 요청을 완료할 수 없습니다 또는 데이터가 충분히 없을 때까지.

요구 사항

루틴

필수 헤더

선택적 헤더

_pipe

<io.h>

<fcntl.h> 1 2 <errno.h>

1 _O_BINARY_O_TEXT 정의 합니다.

2 errno 정의 합니다.

자세한 호환성에 대 한 내용은 호환성.

라이브러리

모든 버전의 C 런타임 라이브러리.

예제 1

// crt_pipe.c
/* This program uses the _pipe function to pass streams of
 * text to spawned processes.
 */

#include <stdlib.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <process.h>
#include <math.h>

enum PIPES { READ, WRITE }; /* Constants 0 and 1 for READ and WRITE */
#define NUMPROBLEM 8

int main( int argc, char *argv[] )
{

   int fdpipe[2];
   char hstr[20];
   int pid, problem, c;
   int termstat;

   /* If no arguments, this is the spawning process */
   if( argc == 1 )
   {

      setvbuf( stdout, NULL, _IONBF, 0 );

      /* Open a set of pipes */
      if( _pipe( fdpipe, 256, O_BINARY ) == -1 )
          exit( 1 );


      /* Convert pipe read descriptor to string and pass as argument 
       * to spawned program. Program spawns itself (argv[0]).
       */
      _itoa_s( fdpipe[READ], hstr, sizeof(hstr), 10 );
      if( ( pid = _spawnl( P_NOWAIT, argv[0], argv[0], 
            hstr, NULL ) ) == -1 )
          printf( "Spawn failed" );

      /* Put problem in write pipe. Since spawned program is 
       * running simultaneously, first solutions may be done 
       * before last problem is given.
       */
      for( problem = 1000; problem <= NUMPROBLEM * 1000; problem += 1000)
      {

         printf( "Son, what is the square root of %d?\n", problem );
         _write( fdpipe[WRITE], (char *)&problem, sizeof( int ) );

      }

      /* Wait until spawned program is done processing. */
      _cwait( &termstat, pid, WAIT_CHILD );
      if( termstat & 0x0 )
         printf( "Child failed\n" );


      _close( fdpipe[READ] );
      _close( fdpipe[WRITE] );

   }

   /* If there is an argument, this must be the spawned process. */
   else
   {

      /* Convert passed string descriptor to integer descriptor. */
      fdpipe[READ] = atoi( argv[1] );

      /* Read problem from pipe and calculate solution. */
      for( c = 0; c < NUMPROBLEM; c++ )
      {

        _read( fdpipe[READ], (char *)&problem, sizeof( int ) );
        printf( "Dad, the square root of %d is %3.2f.\n",
                 problem, sqrt( ( double )problem ) );

      }
   }
}

샘플 출력

Son, what is the square root of 1000?
Son, what is the square root of 2000?
Son, what iDad, the square root of 1000 is 31.62.
Dad, the square root of 2000 is 44.72.
s the square root of 3000?
Dad, the square root of 3000 is 54.77.
Son, what is the square root of 4000?
Dad, the square root of 4000 is 63.25.
Son, what is the square root of 5000?
Dad, the square root of 5000 is 70.71.
Son, what is the square root of 6000?
SonDad, the square root of 6000 is 77.46.
, what is the square root of 7000?
Dad, the square root of 7000 is 83.67.
Son, what is the square root of 8000?
Dad, the square root of 8000 is 89.44.

예제 2

이 기본 필터 응용 프로그램입니다.필터 생성 된 응용 프로그램의 stdout을 지시 하는 파이프를 만든 후 응용 프로그램 crt_pipe_beeper을 생성 합니다.문자가 ASCII 7 (경고음) 필터를 제거합니다.

// crt_pipe_beeper.c

#include <stdio.h>
#include <string.h>

int main()
{
   int   i;
   for(i=0;i<10;++i)
      {
         printf("This is speaker beep number %d...\n\7", i+1);
      }
   return 0;
}

실제 필터 응용 프로그램:

// crt_pipe_BeepFilter.C
// arguments: crt_pipe_beeper.exe

#include <windows.h>
#include <process.h>
#include <memory.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>

#define   OUT_BUFF_SIZE 512
#define   READ_FD 0
#define   WRITE_FD 1
#define   BEEP_CHAR 7

char szBuffer[OUT_BUFF_SIZE];

int Filter(char* szBuff, ULONG nSize, int nChar)
{
   char* szPos = szBuff + nSize -1;
   char* szEnd = szPos;
   int nRet = nSize;

   while (szPos > szBuff)
   {
      if (*szPos == nChar)
         {
            memmove(szPos, szPos+1, szEnd - szPos);
            --nRet;
         }
      --szPos;
   }
   return nRet;
}

int main(int argc, char** argv)
{
   int nExitCode = STILL_ACTIVE;
   if (argc >= 2)
   {
      HANDLE hProcess;
      int fdStdOut;
      int fdStdOutPipe[2];

      // Create the pipe
      if(_pipe(fdStdOutPipe, 512, O_NOINHERIT) == -1)
         return   1;

      // Duplicate stdout file descriptor (next line will close original)
      fdStdOut = _dup(_fileno(stdout));

      // Duplicate write end of pipe to stdout file descriptor
      if(_dup2(fdStdOutPipe[WRITE_FD], _fileno(stdout)) != 0)
         return   2;

      // Close original write end of pipe
      _close(fdStdOutPipe[WRITE_FD]);

      // Spawn process
      hProcess = (HANDLE)_spawnvp(P_NOWAIT, argv[1], 
       (const char* const*)&argv[1]);

      // Duplicate copy of original stdout back into stdout
      if(_dup2(fdStdOut, _fileno(stdout)) != 0)
         return   3;

      // Close duplicate copy of original stdout
      _close(fdStdOut);

      if(hProcess)
      {
         int nOutRead;
         while   (nExitCode == STILL_ACTIVE)
         {
            nOutRead = _read(fdStdOutPipe[READ_FD], 
             szBuffer, OUT_BUFF_SIZE);
            if(nOutRead)
            {
               nOutRead = Filter(szBuffer, nOutRead, BEEP_CHAR);
               fwrite(szBuffer, 1, nOutRead, stdout);
            }

            if(!GetExitCodeProcess(hProcess,(unsigned long*)&nExitCode))
               return 4;
         }
      }
   }
   return nExitCode;
}

Output

This is speaker beep number 1...
This is speaker beep number 2...
This is speaker beep number 3...
This is speaker beep number 4...
This is speaker beep number 5...
This is speaker beep number 6...
This is speaker beep number 7...
This is speaker beep number 8...
This is speaker beep number 9...
This is speaker beep number 10...

해당 .NET Framework 항목

해당 사항 없음. 표준 C 함수를 호출 하려면 PInvoke. 자세한 내용은 플랫폼 호출 예제을 참조하십시오.

참고 항목

참조

프로세스 및 환경 제어

_open, _wopen