LIBRARY FUNCTION: popen(); PROTOTYPE: FILE *popen ( char *command, char *type); RETURNS: ¼º°ø½Ã »õ·Î¿î ÆÄÀÏ ½ºÆ®¸²(stream) fork(),pipe()È£ÃâÀÌ ½ÇÆÐÇßÀ» ¶§ ³Î(NULL) NOTES: ¸í·É¾î("command")¸¦ »ç¿ëÇÏ¿© ÆÄÀÌÇÁ¸¦ ¸¸µé°í fork/exec¸¦ ¼öÇàÇÑ´Ù.
ÀÌ ¶óÀ̺귯¸® ÇÔ¼ö°¡ ´ç½ÅÀ» À§ÇØ ±î´Ù·Î¿î ÀÏÀ» ¼öÇàÇÏ´Â µ¿¾È, Áß¿äÇÑ ÈïÁ¤(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: ÆÄÀÌÇÁ ÇÁ·Î¼¼½º°¡ Á¾·áµÇ±â¸¦ ±â´Ù·È´Ù°¡ ½ºÆ®¸²À» ´Ý´Â´Ù.
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
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