c언어/시스템 프로그래밍

시스템프로그래밍 - 4일차

2023. 4. 8. 18:37
목차
  1. 시스템프로그래밍 - 4일차
  2. Duplicating FD - dup(2) / dup2(2)
  3. oldfd (old file descriptor)
  4. newfd (old file descriptor)
  5. Return: oldfd를 복사한 새로운 fd
  6. 예제
  7. fcntl
  8. fd (file descriptor)
  9. cmd (command)
  10. arg (argument)
  11. return: cmd에 따라 다름
  12. fcntl 예제

시스템프로그래밍 - 4일차

강의 링크

Duplicating FD - dup(2) / dup2(2)

  • 복사하는 시스템 콜
$ man -s 2 dup

#include <unistd.h>

int dup(int oldfd);
int dup2(int oldfd, int newfd);

oldfd (old file descriptor)

  • 복사하려는 file descriptornewfd (old file descriptor)

newfd (old file descriptor)

  • 새로운 fd 지정
  • dup()의 경우 할당 가능한 fd 중 가장 작은 값 할당

Return: oldfd를 복사한 새로운 fd

  • -1: error

예제

#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main(void) {
  int fd, fd1;

  fd = open("tmp.aaa", O_CREAT | O_WRONLY | O_TRUNC, 0644);
  if (fd == -1) {
          perror("Create tmp.aaa");
          exit(1);
  }
  close(1);         # stdout을 close

  fd1 = dup(fd);    # tmp.aaa 파일을 파일 디스크립터(1) 과 연결

  printf("DUP FD=%d\n", fd1);
  printf("Standard Output Redirection\n");
  close(fd);

  return 0;
}

실행

$ cat tmp.aaa 
DUP FD=1
Standard Output Redirection

stdout을 close 하고, dup 시스템콜을 이용하여 tmp.aaa를 stdout과 연결했다. 결과는 printf()가 모니터에 출려되지 않고, tmp.aaa에 저장이 되었다. fd를 잘 다루면 이런 것도 가능하다는 것을 알수 있었다.

fcntl

  • file control
  • 파일 디스크립터를 제어한다.
$ man -s 2 fcntl 
#include <unistd.h> 
#include <fcntl.h>
int fcntl(int fd, int cmd, ... /\* arg \*/ );

fd (file descriptor)

  • 대상 file descriptor

cmd (command)

  • 수행할 명령
  • F_GETFL (상태 flag 읽기), F_STEFL (상태 flag 설정) 등

arg (argument)

  • cmd에 필요한 인자들

return: cmd에 따라 다름

fcntl 예제

#include <sys/types.h>  
#include <fcntl.h>  
#include <unistd.h>  
#include <stdlib.h>  
#include <stdio.h>

int main(void) {  
int fd, flags;

fd = open("linux.txt", O_RDWR); #linux.txt 생성  
if (fd == -1) {  
perror("open");  
exit(1);  
}

if ((flags = fcntl(fd, F_GETFL)) == -1) {  
perror("fcntl");  
exit(1);  
}

flags |= O_APPEND; // change to append mode #append mode or로 추가

if (fcntl(fd, F_SETFL, flags) == -1) { #fcntl 시스템 콜로 추가  
perror("fcntl");  
exit(1);  
}

if (write(fd, "KOREATECH", 9) != 9) perror("write"); #linux.txt 파일에 KOREATECH 단어 추가  
close(fd);

return 0;  
}

결과

$ cat linux.txt  
Linux system programming

$ ./fcntl.out  
$ cat linux.txt  
Linux system programming  
KOREATECHroot

fcntl 시스템콜을 이용하여 append offset을 추가했다. 실행 결과 맨뒤에 추가한 단어 KOREATECH가 linux.txt에 추가 되는 것을 알 수 있었다.

'c언어 > 시스템 프로그래밍' 카테고리의 다른 글

시스템 프로그래밍 3일차  (0) 2023.03.31
시스템 프로그래밍 2일차  (0) 2023.03.30
시스템 프로그래밍 1일차  (0) 2023.03.27
  1. 시스템프로그래밍 - 4일차
  2. Duplicating FD - dup(2) / dup2(2)
  3. oldfd (old file descriptor)
  4. newfd (old file descriptor)
  5. Return: oldfd를 복사한 새로운 fd
  6. 예제
  7. fcntl
  8. fd (file descriptor)
  9. cmd (command)
  10. arg (argument)
  11. return: cmd에 따라 다름
  12. fcntl 예제
'c언어/시스템 프로그래밍' 카테고리의 다른 글
  • 시스템 프로그래밍 3일차
  • 시스템 프로그래밍 2일차
  • 시스템 프로그래밍 1일차
시스템 엔지니어
시스템 엔지니어
공부, 스터디, 내용 정리 Linux / k8s / kubernets / c언어 등등
시스템 엔지니어
썸업
시스템 엔지니어
전체
오늘
어제
  • 분류 전체보기 (24)
    • 테라폼 (7)
      • T101[3기] (7)
    • c언어 (4)
      • 시스템 프로그래밍 (4)
    • NHN Cloud (3)
    • 스터디 (7)
      • DOIK (5)
      • PKOS (2)
    • 기타 (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • terraform
  • PKOS
  • k8s
  • 테라폼
  • Kubernetes
  • 가시다
  • 스터디
  • MongoDB
  • 시스템프로그래밍
  • nhn cloud
  • cloud
  • 시스템 프로그래밍
  • T101
  • C언어
  • operator

최근 댓글

최근 글

hELLO · Designed By 정상우.
시스템 엔지니어
시스템프로그래밍 - 4일차
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.