유닉스 소켓언어 read함수의 시간차? 관련 질문입니다.

is1472의 이미지

먼저 일부분 코드 1 ,2 를 보여드리면

 54     printf("read bef\n");
 55     read(sock,tmpM,6);
 56
 57     printf("read aft\n");
 58     printf("%s",tmpM);
 59     if(!strcmp(tmpT,tmpM)){
 60
 61         printf("ent strcmp\n");
 62         close(sock);
 63         error_handling("nofile err");
 64
 65     }

 54     printf("read bef\n");
 55     read(sock,tmpM,6);
 56
 57     printf("read aft\n");
 58     printf("%s",tmpM);
 59     if(!strcmp(tmpT,tmpM)){
 60
 61         
 62         close(sock);
 63         error_handling("nofile err");
 64
 65     }

두 코드의 차이는 61번라인의 printf의 있고 없고의 차이입니다.
여기서 tmpM에는 "nfile" 이 저장되어지고 tmpT는 "nfile"이 저장되어 있는 char배열변수입니다. error_handling은 nofile error 를 출력하고 exit 하는 함수 입니다.
여기
첫번째 코드를 실행하면 순서가
read bef
read aft
nfileent strcmp
nofile err
이렇게 출력 됩니다

그리고 두번째 코드는
read bef
read aft
nofile err
nfile
이렇게 출력 됩니다.

차이가 느껴지십니까? nfile 과 nofile err의 순서가 바뀝니다.

이는 read()가 읽어서tmpM에 저장하는 시간의 차이? 에서 발생하는 것인가요?

ymir의 이미지

아니오. 코드는 그냥 순서대로 실행됩니다.
tmpM 에 "nfile" 이 들어 있는 것 같은데, 코드 몇 줄과 결과로 보고 추정하건데..
error_handling() 은 stderr 로 찍었을 것 같군요.

기본적으로 stdout 은 line-buffered 입니다.
\n 을 만나거나, fflush() 를 호출하거나 해서 flush 조건을 만나면 그 때 출력합니다.
반면 stderr 는 unbuffered 라서 바로 출력되죠. (man stderr 의 NOTES 참고)

만약 error_handling() 을 stdout 으로 찍었다면..
"nfilenofile err" 라는 문자열이 붙어서 출력되었겠죠.

tmpM 을 출력할 때 뒤에 '\n' 을 넣어주거나, fflush() 를 호출하면 순서대로 화면에 보일겁니다.

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

is1472의 이미지

stdin stdout stderr 에 대해서 쓰이는 스트림? 방법 정도만 알았지 이러한 차이가 있는줄은 몰랐습니다.
죄송하지만 혹시 자세한 설명이나 또는 자세한 설명이있는 링크를 주실수 있나요?
line-buffered라는것도 처음 들어봅니다...

Yi Soo An@Google의 이미지

ppt라 그나마 쉽고 중요한 내용만 간추려 있네요.

https://users.soe.ucsc.edu/~sbrandt/105/Slides/Chapter5.pdf

---------------
Happy Hacking!

댓글 달기

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