pipe와 waitpid 관련 질문입니다.

익명 사용자의 이미지

스티븐아저씨책에 파이프부분 처음 나오는 예제를 옮겨실행했습니다.

#include
#include
#include
#include
#include
#define DEF_PAGER "/usr/bin/more"
#define STDIN_FILENO 0

int
main( int argc , char *argv[] )
{
int n , fd[2];
pid_t pid;
char line[255] , *pager , *argv0;
FILE *fp;

if ( argc != 2 )
{
exit(2 );
}
if ( (fp = fopen( argv[1] , "r" ) ) == NULL )
{
perror("");
exit(3 );
}

if ( pipe( fd ) < 0 )
{
perror("");
exit(3 );
}
if ( ( pid = fork() ) < 0 )
perror("");
else if ( pid > 0 )
{
close( fd[0] );
while ( fgets( line , sizeof line , fp ) != NULL )
{
n = strlen( line );
if ( write( fd[1] , line , n ) != n )
{
perror("");
}
}
if ( ferror( fp ) )
{
fprintf( stderr , "fgets error\n" );
}

if ( waitpid( pid , NULL , 0 ) < 0 )
{
perror("");
}
}
else
{
close( fd[1] );
if ( STDIN_FILENO != fd[0] )
{
if ( dup2( fd[0] , STDIN_FILENO ) != STDIN_FILENO )
{
perror("dup2");
}
close( fd[0] );
}
if ( ( pager = getenv("PAGER") ) == NULL )
pager = DEF_PAGER;
if ( ( argv0 = strrchr( pager , '/' ) ) != NULL )
argv0++;
else
argv0 = pager;
if ( execl( pager , argv0 , (char*) 0 ) < 0 )
perror("");
}
return 0;
}

내용은 부모에서 파이프를 생성한후 fork한다.
이때 부모에서 특정파일을 오픈하여 fd[1]로 write한다.
자식은 fd[0]을 0( stdin ) 으로 리다이렉션(dup2)한후
/usr/bin/more 프로그램을 execl 시킨다.
결과는 부모에서 읽어들여 보낸 파일의 내용이 자식을 통해
more 형식으로 보여지게 되는것인데요.

문제는
부모프로세스 waitpid에서 자식이 수행이 끝났음에도
리턴을 하지 않고 계속기다리는 상태에 있습니다.
할수없이 Ctrl+C 로 종료를 했습니다만
원인을 알고싶습니다.
(솔라리스환경)

익명 사용자의 이미지


원인은
부모가 close(fd[1]) 를 하지 않아서
자식(more)이 read 에서 blocking 된것입니다.
close(fd[1]) 할경우 자식은 read 함수에서 error 를
리턴 받아서 끝나게 되고 그러면 부모는 waitpid 로
자식을 처리하고 끝나게 됩니다.
waitpid 전에 sleep(10);close(fd[1]) 넣어보세요

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.