[질문]pipe 크기

SilverWhalle의 이미지

int count;
void alrm_action(int);

int main(void)
{
int p[2];
int pipe_size;
char c = 'x';
static struct sigaction act;

act.sa_handler = alrm_action;
sigfillset(&(act.sa_mask));

if(pipe(p)==-1){
perror("pipe call");
exit(1);
}

pipe_size = fpathconf(p[0], _PC_PIPE_BUF);
printf("Maximum size of write to pipe: %d bytes\n", pipe_size);

sigaction(SIGALRM, &act, NULL);

while(1){
alarm(20);
write(p[1], &c, 1);
alarm(0);
if((++count % 1024) == 0)
printf("%d characters in pipe\n", count);
}
}

void alrm_action(int signo)
{
printf("write blocked after %d characters\n", count);
exit(0);
}

소스는 위와 같습니다.
책에 설명되어 있기로 파이프의 크기만큼 데이타가 다 차면 파이프에 쓰는 작업을
잠시 중단한다고 했습니다.이 프로그램은 작업이 중단되면 alarm에 의해서
alrm_action()이 호출됩니다.
그런데 실행결과가 좀 이상합니다.
$./block
Maximun size of write to pipe: 4096bytes
1024 characters in pipe
...
...
65536 characters in pipe
write blocked after 65536 characters

이런 결과가 나옵니다. 파이프 크기는 4096인데 왜 65536까지 진행이 되는거지요?
4096바이트까지 파이프에 쓰기작업을 하고나면 파이프에 빈 공간이 없어서
수행을 멈추게 되고 alarm(20)에 의해 20초가 흐른뒤엔 alrm_action()이
호출되어야 하며, read()시스템 호출이 없으므로 파이프에 빈 공간을 만들수 없어
.... ?? 여기까지입니다.. 이해한 바로는...
일단 책에는 마지막 출력 write blocked ..를 끝으로 출력이 끝나는 걸로 나오는데
왜 끝나는지는 모르겠습니다. 제 생각에는 파이프 공간이 생길때까지 무한루프를 돌아
야 하는게 아닌가 싶은데..

추가질문) read()가 파이프의 내용을 읽어온 만큼 읽은 내용은 비워버리는건가요?

사용 리눅스 : ubuntu edgy

M.W.Park의 이미지

프로그램에서 파이프 사이즈 구하는 것은 안해봐서 잘 모르겠지만,
ulimit -a 하면 명령행에서 알 수 있습니다. 한번 비교해보시길...
-----
오늘 나의 취미는 끝없는, 끝없는 인내다. 1973 法頂

-----
오늘 의 취미는 끝없는, 끝없는 인내다. 1973 法頂

SilverWhalle의 이미지

ulimit -a라고 하니깐..
pipe size (512 bytes, -p) 8
라고 나오네요. 512 * 8 = 4096이네요. 그러면
프로그램 실행 결과에서 4096으로 나오는건 분명
우분투에서 파이프 사이즈를 4096으로 잡긴 잡는가본데요.
왜 4096에서 멈추지 않고 65536까지 진행이 되는걸까요?

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.