유닉스에서 시간

Jongwon·2021년 12월 18일
0

Linux Programming

목록 보기
25/25

시간

벽시계 시간

현실세계의 시간과 날짜

프로세스 시간

프로세스가 main부터 실행하면서 걸린시간. 사용자 영역과 커널 영역에서 소비한 시간의 합

  • 사용자 영역: main함수 등 앞에서 실행되는 코드들에 걸린 시간
  • 커널 영역: 시스템 호출로 인한 내부적인 처리에 걸린 시간

단조 시간

시스템 부팅 후 단조적으로 증가하는 시간. 사건과 사건 사이 상대적 시간 간격 측정 시 사용(사건 후 5초 뒤에 깨워달라 등)
=> 커널 내에 jiffies는 부팅 시 0으로 초기화되고, 타이머 인터럽트 발생 시마다 증가



각각의 시간에는 ID가 존재한다.

clockid_t

  • CLOCK_REALTIME
  • CLOCK_MONOTONIC
  • CLOCK_PROCESS_CPUTIME_ID
  • CLOCK_THREAD_CPUTIME_ID




시간의 표현

  • 상대시간 : 특정 시점 기준 몇초 후
  • 절대 시간 : 절대값으로 표현
  • 유닉스는 Epoch(1970.01.01 00:00:00) 이후 현재 시간까지 경과된 초 형태로 시간 유지
  • 2.6 커널부터는 초 형태 유지 위해 64비트 카운터 변수 사용
    => typedef long time_t : epoch 이후 현재 시간까지 경과한 초를 숫자로 저장
  • 리눅스 시스템이 부팅 시 현재 시간을 계산함(컴퓨터에 시간 혹은 서버시간). 그 뒤로는 타이머 인터럽트가 발생할 때마다 현재 시간 갱신
  • 타이머 인터럽트 주파수(HZ): 1초에 발생하는 타이머 인터럽트 수
    => 타이머 인터럽트의 주기를 기준으로 모든 동작이 일어남
    => HZ가 클수록 overhead가 커져 보통 250사용
    => typedef int(or long) clock_t : 틱, 타이머 인터럽트 발생 수


<sys/time.h> 헤더

초 이하 단위를 위한 구조체가 정의되어있다.

struct timeval {
    time_t tv_sec;
    useconds_t tv_usec;   //마이크로초
}
struct timespec {
    time_t tv_sec;
    long tv_nsec;   //나노초
}
  • clock_t times(struct tms *buf)
    현재 프로세스와 자식 프로세스의 수행 시간을 틱 단위로 반환
struct tms {
    clock_t tms_utime;   //user mode 실행시간
    clock_t tms_stime;   //kernel mode 실행 시간
    clock_t tms_cutime;  //종료된 자식 프로세스의 user mode 실행시간
    clock_t tms_cstime;  //종료된 자식 프로세스의 kernel mode 실행시간
  • int gettimeofday(struct timeval tv, struct timezone tz)
    마이크로초와 timezone(지역)도 알려줌
  • int settimeofday(struct timeval tv, struct timezone tz)
    tv와 tz에 있는 대로 시간 설정
  • int gettimer(int which, struct itimerval *value)
  • int settimer(int which, const struct itimerval value, struct itimerval ovalue)
    alarm()에서 발전한 형태로, 옵션으로 자동 재장전 기능이 있다. ovalue는 이전 타이머의 값 저장
int which
- ITIMER_REAL: 실제 시간이 경과하면 커널이 프로세스에 시그널 전송
- ITIMER_VIRTUAL: 사용자 영역 실행 시간 경과시
- ITIMER_PROF: 사용자 영역+커널 영역 시간 경과시

struct itimerval {
    struct timeval it_value;      //초기 시간
    struct timeval it_interval;   //재장전할 시간



<time.h> 헤더

C에서 현재 시간 표현

struct tm {
    int tm_sec;
    int tm_min;
    int tm_hour;
    int tm_mday;
    int tm_mon;
    int tm_year;
    int tm_wday;
    int tm_yday;
    int tm_isdst;
}
  • time_t time(time_t *t)
    epoch 이후 현재시간까지 경과한 초 t에 넣고 반환
  • time_t stime(time_t *t)
    시스템 시간을 t가 가리키는 시간으로 설정
    따라서 유닉스의 time_t와 호환을 맞추기 위한 함수 존재
  • struct tm gmtime(const time_t timep)
    tm 구조체를 스스로 할당하고, 유닉스의 시간 구조체를 UTC기준의 tm으로 받아옴
  • struct tm gmtime_r(const time_t timep, struct tm *result)
    result에 UTC시간 저장
  • struct tm localtime(const time_t timep)
    tm 구조체를 스스로 할당하고, 유닉스의 시간 구조체를 기반으로 tm에 로컬 시간(서울)을 저장
  • struct tm localtime_r(const time_t timep, struct tm *result)
    result에 로컬 시간을 저장
  • double difftime(time_t time1, time_t time0)
    두 시간 차이를 반환
  • time_t mktime(struct tm *tm)
    tm 구조체 시간을 time_t로 변환
  • char ctime(const time_t timep)
    ctime 포인터를 스스로 할당하고 time_t 자료형을 문자열 형태로 반환
  • char ctime_r(const time_t timep, char *buf)
    buf에 문자열 형태로 저장
  • char asctime(const struct tm tm)
    ctime에서 매개변수가 tm일 경우
  • char asctime_r(const struct tm tm, char *buf)
    buf에 문자열 형태로 저장
  • int clock_gettime(clockid_t clock_id, struct timespec *ts)
    해당 id의 클럭의 현재시간을 ts에 저장

  • int clock_settime(clockid_t clock_id, struct timespec *ts)
    해당 id의 클럭의 시간을 ts로 설정

  • void nanosleep(const struct timespec req, struct timespec rem)
    나노초 단위로 잠들기. 중간에 깨면 rem에 남은 시간 저장

  • int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec req, struct timespec rem)
    `특정 시계를 이용하여 sleep하겠다. flags는 절대시간(TIMER_ABSTIME) or 상대시간(0)

<unistd.h> 헤더

  • unsigned int sleep(unsigned int seconds)
    seconds동안 sleep, 시그널로 깨어나지 않으면 0, 깨어나면 남은 시간 리턴
  • void usleep(unsigned long usec)
    마이크로 초 단위로 잠들기(BSD버전)
  • void usleep(usecond_t usec)
    마이크로 초 단위로 잠들기(SUSv2 버전)
  • unsigned int alarm(unsigned int seconds)
    타이머 기능, 몇초후에 alarm 시그널 발생시켜라
profile
Backend Engineer

0개의 댓글