Created
December 21, 2021 13:36
-
-
Save cho0h5/149c167b79b2fad5c214955d9fcfd152 to your computer and use it in GitHub Desktop.
dup and dup2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <unistd.h> | |
int main() { | |
char buf[] = "dummy string\n"; | |
write(1, buf, 14); // write to stdout | |
int duplicated_fd = dup(1); | |
printf("duplicated fd: %d\n", duplicated_fd); | |
write(duplicated_fd, buf, 14); // write to stdout | |
return 0; | |
} | |
// dummy string | |
// duplicated fd: 3 | |
// dummy string | |
// int dup(int oldfd); | |
// dup -> file discriptor 복제 (같은 대상을 가리키는 새로운 fd생성) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <unistd.h> | |
int main() { | |
char buf[] = "dummy string\n"; | |
write(1, buf, 14); // write to stdout | |
int new_fd = 372; // random number (which i want) | |
dup2(1, new_fd); | |
write(new_fd, buf, 14); // write to stdout | |
return 0; | |
} | |
// dummy string | |
// dummy string | |
// int dup2(int oldfd, int newfd); | |
// dup2 -> dup이랑 같은데, 사용 안 된 가장 작은 fd가 아니라 새로운 fd를 직접 명시 한다는 점이 다름. 만약 newfd가 이미 열려있다면, 암묵적으로 닫은 다음에 재사용됨 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment