dup() 함수 관련 문의 사항

hernian의 이미지

안녕하세요 개발 중 문의 사항이 있어서 이렇게 글을 올려 봅니다.

dup() 함수는 인자로 들어온 파일디스크립터를 커널이 가장작은 수의 디스크립터로 복사해서 return 해주는 역할을 한다고 알고 있습니다.


fd = open("temp.txt".....);
tempfd = dup(fd);

위의 코드를 처음 부분에서 실행하면
fd 는 처음 open을 썻으므로 3이 return 되고
tempfd 는 4가 리턴되는데, 두개의 디스크립터가 같은 파일테이블을 보고 있으므로
fd 에 write 하나 tempfd 에 write 하나 temp.txt 파일에 써지는 것을 확인 했습니다.

그런데 여기서 문의 사항 입니다.
redirect 공부 중 도무지 이해가 가지를 않네요

int main(int argc, char* argv[]){
int fd,ld;

ld = open("output", O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR| S_IWUSR);

close(1);
dup(ld);

printf("asdfwefwefasdf");
printf("--->%d\n", ld);

close(fd);
close(ld);

return 0;
}

위의 코드에서 디스크립터1번을 close 하고
dup(ld) 로 파일디스크립터를 복사하면 1번(표준출력) 이 리턴이 될거라고 생각합니다.
그리고 printf 문으로 출력을 하면
printf 는 표준출력에 write 를 하고 표준출력은 현재 ld로 복사가 되어있어서 실제 output 파일에 내용을 쓰는것 까지 확인 했습니다.
(제가 생각하는게 맞는지 모르겠네요)

그런데 위의 코드 중
dup(ld) 부분을
fd = dup(ld);
로 수정을 하게 되면
아무것도 출력이 안되고 파일에도 아무것도 없습니다.

제 생각으로는
fd = dup(ld); 하는순간
fd 디스크립터는 1번이고
파일테이블은 같은 테이블을(output파일)을 바라보게 되어
printf함수로 어떤 문자열을 출력하면
printf 함수는 표준 출력(1)에 write 하므로 output 파일에 써져야 한다고 생각하는데
아무것도 출력 없네요...

혹시 잘 알고 계시는 분 있다면 설명 부탁좀 드릴게요~

읽어 주셔서 감사합니다.

cinsk의 이미지

STREAM(FILE *)은 일반적으로 buffered IO를 수행합니다. 아마 buffer의 내용이 flush되기 전에 close()로 닫혀서 그런 것 같다는 생각이 드네요. fd를 쓰는 low-level IO와 STREAM IO를 번갈아가며 쓰는 것은 별로 좋지 않습니다. 위 예제와 비슷한 일을 하려면, 1) fdopen()으로 stdout을 다시 설정하던지, 2) setvbuf() 등으로 buffer 크기를 0으로 하던지, 3) 적절하게 fflush()를 불러 주던지 해야 할 것 같네요.

댓글 달기

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