IPC로 버퍼의 문자열을 교환하려고 하던 중에 문제가 발생합니다.

dltkddyd의 이미지

#include <unistd.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
 
 
int main() {
  int fdforpipe[2];
  char buffer[]={"This is parent."};
  printf("dd %s\n",buffer);
  int writinglength=0;
  int readinglength=0;
  if(pipe(fdforpipe)==-1) {
    printf("The error message : pipe call error\n");
    exit(0);
  }
  pid_t pid=0;
  switch(pid=fork()) {
    case -1:
      printf("The error message : fork call error\n");
      exit(0);
      break;
    case 0:
      close(fdforpipe[1]);
      while((readinglength=read(fdforpipe[0],buffer,256))!=0 || readinglength!=-1) {
        printf("%s",buffer);
      }
      break;
    default:
printf("in parent\n");//test
 
      printf("%d\n",strlen(buffer));//test
      printf("%s",buffer);//여기서 출력이 안됩니다.
      //cout<<buffer<<endl;//cout으로는 출력이 됩니다.
      close(fdforpipe[0]);
      writinglength=write(fdforpipe[1],buffer,strlen(buffer));
      break;
  }
 
  while(1) { }
  return 0;
}

위와 같이 소스코드를 만들었습니다. 문제는 부모와 자식프로세스가 분기되는 지점의 switch 블록 안의 부모프로세스에서 buffer가 가리키는 내용이 printf로 출력되지 않습니다. 그러나 cout으로는 출력이 되는데요, 왜 이런 문제가 발생하는 건가요? printf에 무슨 문제가 있는 건가요?

ymir의 이미지

printf 쪽의 출력은 buffering 된 것 같네요.
printf 에도 \n 을 붙여 주시거나.. printf 이후에 fflush(stdout) 넣어주시거나.. 해보시죠.

참고 -
http://www.gnu.org/software/libc/manual/html_node/Controlling-Buffering.html

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

dltkddyd의 이미지

그 이유가 잘 이해되지 않습니다. bufferring은 표준입력시에 발생하는 게 아닌가요? 한 문자의 입력을 받아오는 함수를 사용했는데, 여러개의 문자가 입력된 경우말입니다. 이런 건 표준입력스트림과 관련있는 것으로 아는데요. 표준출력에도 bufferring이 있다는 것은 잘 모르겠습니다. 왜 \n을 붙이면, 언급했던 문제가 해결되는 것인가요?

본인 맞습니다.
인증샷
우헤헤헤... 로 대신합니다.

ymir의 이미지

buffered i/o 로 검색해 보세요.

stream buffer 를 이용하는 i/o 함수들은 direct 로 출력되지 않고, buffer 에 저장되었다가..
\n 과 같은 flush 조건을 만나거나, 강제로 flush 시켰을 때에 출력됩니다.

그래서 위에 setvbuf 와 관련된 링크를 첨부한 것이구요.
그 상위 문서인 Stream Buffering 과 I/O on streams 도 살펴 보시면 이해하시는 데 도움이 될겁니다.

http://www.gnu.org/software/libc/manual/html_node/Stream-Buffering.html

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

댓글 달기

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