´ÙÀ½ À§·Î ÀÌÀü ¸ñÂ÷ ¸®´ª½º ÇÁ·Î±×·¡¸Ó¸¦ À§ÇÑ °¡À̵å

6.2.3 ÆÄÀÌÇÁ ½¬¿î ¹æ¹ý! (pipes the Easy Way!)

¾ÕÀÇ µÎ¼­¾ø´Â ±ÛµéÀÌ ÆÄÀÌÇÁ¸¦ ¸¸µé°í »ç¿ëÇÏ´Â ¸Å¿ì ¹«³­ÇÑ ¹æ¹ýó·³ º¸¿´´Ù¸é, ¶Ç ´Ù¸¥ °ÍÀÌ ÀÖ´Ù.
  LIBRARY FUNCTION: popen();                                                    

  PROTOTYPE: FILE *popen ( char *command, char *type);                          
    RETURNS: ¼º°ø½Ã »õ·Î¿î ÆÄÀÏ ½ºÆ®¸²(stream)                                       
             fork(),pipe()È£ÃâÀÌ ½ÇÆÐÇßÀ» ¶§ ³Î(NULL)

  NOTES: ¸í·É¾î("command")¸¦ »ç¿ëÇÏ¿© ÆÄÀÌÇÁ¸¦ ¸¸µé°í fork/exec¸¦ ¼öÇàÇÑ´Ù.

ÀÌ Ç¥ÁØ ¶óÀ̺귯¸® ÇÔ¼ö´Â ³»ºÎÀûÀ¸·Î pipe()¸¦ È£ÃâÇÏ¿© ¹ÝÀÌÁß ÆÄÀÌÇÁ¶óÀÎÀ» ¸¸µç´Ù. ½©¿¡¼­ ÀÚ½Ä ÇÁ·Î¼¼½º¸¦ »ý¼º(fork)ÇÏ°í, º» ½©(Bourne ahell)À» execÇÏ°í "command" ¾Æ±Ô¸ÕÆ®¸¦ ½ÇÇàÇÑ´Ù. ÀÚ·á È帧ÀÇ ¹æÇâÀº µÎ¹ø° ¾Æ¹Â¸ÕÆ®ÀÎ "type"¿¡ ÀÇÇØ °áÁ¤µÈ´Ù. "read"À̳ª "write"¿¡ ´ëÇØ "r"À̳ª "w"°¡ µÉ ¼ö ÀÖ´Ù. µÎ°¡Áö°¡ ¸ðµÎ µÉ ¼ö´Â ¾ø´Ù. ¸®´ª½º¿¡¼­, ÆÄÀÌÇÁ´Â "type"ÀÇ Ã¹¹ø° ±ÛÀÚ¿¡ ÀÇÇØ ÁöÁ¤µÈ ¸ðµå·Î ¿­·ÁÁø´Ù. µû¶ó¼­, "rw"¸¦ ³Ñ±ä´Ù¸é Àбâ("read") ¸ðµå·Î ¿­·ÁÁø´Ù.

ÀÌ ¶óÀ̺귯¸® ÇÔ¼ö°¡ ´ç½ÅÀ» À§ÇØ ±î´Ù·Î¿î ÀÏÀ» ¼öÇàÇÏ´Â µ¿¾È, Áß¿äÇÑ ÈïÁ¤(tradeoff)ÀÌ ÀϾ´Ù. pipe() ½Ã½ºÅÛ È£ÃâÀ» »ç¿ëÇÏ°í fork/exec¸¦ Ãë±ÞÇÏ´Â °Í¿¡ ÀÇÇØ ´ç½ÅÀº Àá½Ã ÅëÁ¦±ÇÀ» ÀÒ¾î¹ö¸°´Ù. º» ½©ÀÌ Á÷Á¢ »ç¿ëµÇ¹Ç·Î, "command" ¾Æ±Ô¸ÕÆ® ³»¿¡¼­ ¿ÍÀϵå Ä«µå(wildcard)¸¦ Æ÷ÇÔÇÑ ½© ¸ÞŸ¹®ÀÚ È®Àå(shell metacharacter expansion)ÀÌ °¡´ÉÇÏ´Ù.

popen()¿¡ ÀÇÇØ ¸¸µé¾îÁø ÆÄÀÌÇÁ´Â pclose()·Î ´Ý¾Æ¾ß¸¸ ÇÑ´Ù. popen/pclose°¡ Ç¥ÁØ ÆÄÀÏ ½ºÆ®¸² I/O ÇÔ¼öÀÎ fopen(),fclose()¿Í ¸Å¿ì ºñ½ÁÇÏ´Ù´Â °ÍÀ» ¾Ë¾ÒÀ» °ÍÀÌ´Ù.


 LIBRARY FUNCTION: pclose();                                                   

  PROTOTYPE: int pclose( FILE *stream );                                        
    RETURNS: wait4() È£ÃâÀÇ Å»Ãâ »óÅÂ(exit status)
             -1 ½ºÆ®¸²("stream")ÀÌ À¯È¿ÇÏÁö ¾Ê°Å³ª wait4()°¡ ½ÇÆÐÇßÀ¸¸é 

  NOTES: ÆÄÀÌÇÁ ÇÁ·Î¼¼½º°¡ Á¾·áµÇ±â¸¦ ±â´Ù·È´Ù°¡ ½ºÆ®¸²À» ´Ý´Â´Ù.

pclose() ÇÔ¼ö´Â popen()¿¡ ÀÇÇØ »ý¼º(fork)µÈ ÇÁ·Î¼¼½º¿¡ ´ëÇØ wait4()¸¦ ¼öÇàÇÑ´Ù. wait4()·ÎºÎÅÍ ¹ÝȯµÉ ¶§, ÆÄÀÌÇÁ¿Í ÆÄÀÏ ½ºÆ®¸²À» Æı«ÇÑ´Ù. ÀϹÝÀûÀÎ ½ºÆ®¸²¿¡ ±âÃÊÇÑ ÆÄÀÏ I/O¿¡ ´ëÇÑ fclose() ÇÔ¼ö¿Í µ¿ÀǾî¶ó ÇÒ ¼ö ÀÖ´Ù.

sort¸í·É¾î·Î ÆÄÀÌÇÁ¸¦ ¿­¾î ¹®ÀÚ¿­ÀÇ ¹è¿­À» Á¤·Äó¸®ÇÏ´Â ¿¹Á¦¸¦ »ìÆ캸ÀÚ:

/*****************************************************************************
 ¸®´ª½º ÇÁ·Î±×·¥¸Ó¸¦ À§ÇÑ °¡À̵å - 6Àå ¿¡¼­ ¹ßÃé
 (C)opyright 1994-1995, Scott Burkett
 ***************************************************************************** 
 MODULE: popen1.c
 *****************************************************************************/
  
#include <stdio.h>

#define MAXSTRS 5

int main(void)
{
        int  cntr;
        FILE *pipe_fp;
        char *strings[MAXSTRS] = { "echo", "bravo", "alpha",
                                  "charlie", "delta"};

	/*popen() È£ÃâÀ» »ç¿ëÇÏ¿© ´Ü¹æÇâ ÆÄÀÌÇÁ¸¦ ¸¸µç´Ù*/
        if (( pipe_fp = popen("sort", "w")) == NULL)
        {
                perror("popen");
                exit(1);
        }

	/*¹Ýº¹ ó¸®*/
        for(cntr=0; cntr<MAXSTRS; cntr++) {
                fputs(strings[cntr], pipe_fp);
                fputc('\n', pipe_fp);
        }

	/*ÆÄÀÌÇÁ¸¦ ´Ý´Â´Ù*/
        pclose(pipe_fp);
        
        return(0);
}
popen()´Â ÀÚ½ÅÀÇ ¸í·ÉÀ» ¼öÇàÇϴµ¥ ½©À» »ç¿ëÇÔÀ¸·Î, ¸ðµç ½© È®Àå ¹®ÀÚµé°ú ¸ÞŸ¹®ÀÚÀÇ »ç¿ëÀÌ °¡´ÉÇÏ´Ù. ´õ±º´Ù³ª redirection°ú °°Àº º¸´Ù Áøº¸µÈ ±â¼ú°ú ÆÄÀÌÇÁÀÇ Ãâ·ÂÁ¶Â÷ popen()¿¡¼­ »ç¿ëµÉ ¼ö ÀÖ´Ù. ´ÙÀ½ÀÇ °£´ÜÇÑ È£ÃâÀ» »ìÆ캸ÀÚ:
        popen("ls ~scottb", "r");
        popen("sort > /tmp/foo", "w");
        popen("sort | uniq | more", "w");
popen()ÀÇ ¶Ç ´Ù¸¥ ¿¹ÀÎ, µÎ°³ÀÇ ÆÄÀÌÇÁ(Çϳª´Â ls, ´Ù¸¥ Çϳª´Â sort)¸¦ ¿©´Â ÀÛÀº ÇÁ·Î±×·¥À» »ìÆ캸ÀÚ:

/*****************************************************************************
 ¸®´ª½º ÇÁ·Î±×·¥¸Ó¸¦ À§ÇÑ °¡À̵å - 6Àå ¿¡¼­ ¹ßÃé
 (C)opyright 1994-1995, Scott Burkett
 ***************************************************************************** 
 MODULE: popen2.c
 *****************************************************************************/

#include <stdio.h>

int main(void)
{
        FILE *pipein_fp, *pipeout_fp;
        char readbuf[80];

	/*popen() È£ÃâÀ» »ç¿ëÇÏ¿© ´Ü¹æÇâ ÆÄÀÌÇÁ¸¦ ¸¸µç´Ù*/
        if (( pipein_fp = popen("ls", "r")) == NULL)
        {
                perror("popen");
                exit(1);
        }

	/*popen() È£ÃâÀ» »ç¿ëÇÏ¿© ´Ü¹æÇâ ÆÄÀÌÇÁ¸¦ ¸¸µç´Ù*/
        if (( pipeout_fp = popen("sort", "w")) == NULL)
        {
                perror("popen");
                exit(1);
        }

	/*¹Ýº¹ ó¸®*/
        while(fgets(readbuf, 80, pipein_fp))
                fputs(readbuf, pipeout_fp);

	/*ÆÄÀÌÇÁ¸¦ ´Ý´Â´Ù*/
        pclose(pipein_fp);
        pclose(pipeout_fp);

        return(0);
}
popen()ÀÇ ¸¶Áö¸· ¿¹Á¦¸¦ À§ÇØ, ³Ñ°Ü¹ÞÀº ¸í·É¾î¿Í ÆÄÀÏ¸í°£ÀÇ ÆÄÀÌÇÁ¶óÀÎÀ» ¿©´Â ÀϹÝÀûÀÎ ÇÁ·Î±×·¥À» ÀÛ¼ºÇØ º¸ÀÚ:
/*****************************************************************************
 ¸®´ª½º ÇÁ·Î±×·¡¸Ó¸¦ À§ÇÑ °¡À̵å - 6Àå ¿¡¼­ ¹ßÃé
 (C)opyright 1994-1995, Scott Burkett
 ***************************************************************************** 
 MODULE: popen3.c
 *****************************************************************************/

#include <stdio.h>

int main(int argc, char *argv[])
{
        FILE *pipe_fp, *infile;
        char readbuf[80];

        if( argc != 3) {
                fprintf(stderr, "USAGE:  popen3 [command] [filename]\n");       
                exit(1);
        }

	/*ÀÔ·Â ÆÄÀÏÀ» ¿¬´Ù*/
        if (( infile = fopen(argv[2], "rt")) == NULL)
        {
                perror("fopen");
                exit(1);        
        }

	/*popen() È£ÃâÀ» »ç¿ëÇÏ¿© ´Ü¹æÇâ ÆÄÀÌÇÁ¸¦ ¸¸µç´Ù*/
        if (( pipe_fp = popen(argv[1], "w")) == NULL)
        {
                perror("popen");
                exit(1);
        }

	/*¹Ýº¹ ó¸®*/
        do { 
                fgets(readbuf, 80, infile);
                if(feof(infile)) break;

                fputs(readbuf, pipe_fp);
        } while(!feof(infile));

        fclose(infile); 
        pclose(pipe_fp);

        return(0);
}
´ÙÀ½ÀÇ ¿¹¸¦ °¡Áö°í ÀÌ ÇÁ·Î±×·¥À» ¼öÇà½ÃÄѺ¸ÀÚ:
        popen3 sort popen3.c
        popen3 cat popen3.c
        popen3 more popen3.c
        popen3 cat popen3.c | grep main


ÀÌÀü:6.2.2 C·Î ÆÄÀÌÇÁ ¸¸µé±â ´ÙÀ½:6.2.4 ÆÄÀÌÇÁÀÇ Atomic µ¿ÀÛ

Copyright (c) 1996,1997 by Euibeom.Hwang & SangEun.Oh All Rights Reserved

Email To:Webmaster , Another address
LAST UPDATE Nov 18,1997
Created Nov 15,1997