STAT 사용시 st_atime, st_mtime, st_ctime의 차이점은?

ssik425의 이미지

- 제목과 같습니다. 분명 각각의 용도에 맞게 설정이 될텐데..
테스트를 해봐도 큰 차이를 알 수 가 없더군요...

의문 1. st_atime, st_mtime, st_ctime의 정확한 용도 및 차이점은?

의문 2. ls -al로 보여지는 날자와 stat상의 날자와의 관계, 저장되는 위치는?

의문2의 경우 ls로 보여지는 날자와 stat상의 날자가 확연히 차이가 있더군요..

ddoman의 이미지

struct stat {
                  dev_t         st_dev;      /* device */
                  ino_t         st_ino;      /* inode */
                  mode_t        st_mode;     /* protection */
                  nlink_t       st_nlink;    /* number of hard links */
                  uid_t         st_uid;      /* user ID of owner */
                  gid_t         st_gid;      /* group ID of owner */
                  dev_t         st_rdev;     /* device type (if inode device) */
                  off_t         st_size;     /* total size, in bytes */
                  blksize_t     st_blksize;  /* blocksize for filesystem I/O */
                  blkcnt_t      st_blocks;   /* number of blocks allocated */
                  time_t        st_atime;    /* time of last access */
                  time_t        st_mtime;    /* time of last modification */
                  time_t        st_ctime;    /* time of last status change */
              };

man 2 stat 해서 나오는걸 긁어붙여봤습니다.
access, modfication, status change 라네요..
man 페이지 보시면 좀 더 자세한 설명이 나와있습니다.

익명 사용자의 이미지

ㅠ.ㅠ 제질문이 man 페이지도 못보는 초보같이 보였나보군요..

그정도는 아니구요... ^^

                The value st_blocks 값은 512-bytes 블록에 있는 파일의 크기
                 를 준다.  st_blksize 값은 효율적인 파일 시스템 I/O를 위 해
                 "선택된" 블록 크기를 준다.  (더 작은 공간에 파일을 쓰는 것
                 은 쓰고-수정하고-다시 쓰는데 비효율적이다.)4

                 모든 리눅스 파일시스템이 모든 시간 영역에서 실행하는 것 은
                 아 니 다.    전 통 적으로, st_atime 는 mknod(2), utime(2),
                 read(2), write(2),와 truncate(2).  로 바뀌었다.  전통적 으
                 로, st_mtime 는 mknod(2), utime(2),와 write(2).  로 바뀌었
                 다.  st_mtime 는 소유자, 그룹, 하드링크 카운트, 혹은 모 드
                 의 변화에 따라 바뀌지 않았다.

                 전 통 적으로, st_ctime 는 inode 정보를 쓰거나 설정함으로써
                 바뀌었다.(i.e., owner, group,link count, mode,등등).

위와같은 설명만으로는 부족하지 않나요?

그리고 간단히 테스트를 해본결과 위 설명처럼 되지 않더군요..

3개의 값이 모두 변경이 되더군요..
그리고 stat 상의 time 과 ls 상의 시간이 다르더군요...

그러면 ls 상에 표시되는 날자는 대체 어디에 있는건가 하는 의문에서 위과

같이 질문을 한겁니다.

익명 사용자의 이미지

Anonymous wrote:

...

테스트가 잘못된것으로 보이외다.

ls에서 보이는 시간은 분(minute)단위 이므로 테스트를 분단위로 해야하오.

그렇지 않다면(초단위로 해보겠다면), struct stat{}내의 시간값들을
정수로 찍어보시기 바라오.

access/modification/status change를 도대체 어떤 시스템 호출로 하는가?는 "그 정도는 아니구요...^^"에 써 있구료.

익명 사용자의 이미지

코드 치기 귀찮을까봐 내가 친 코드 올리오.

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
main()
{
   struct stat st;
   char filename[1024];

   sprintf(filename, "./DATA.TXT");
   lstat(filename, &st);
   printf("%d  %d  %d \n", st.st_atime, st.st_mtime, st.st_ctime);
   printf("%s %s %s \n", ctime(&st.st_atime), ctime(&st.st_mtime), ctime
(&st.st_ctime));
  /* time을 인쇄하는 다양한 방법에 대해 고민해보시요 */
}

Quote:
$ gcc stat stat.c
$ cat > DATA.TXT
1
^D
$ ls -al DATA.TXT
$ ./stat
$ cat >> DATA.TXT # >>에 유의
2
^D
$ more DATA.TXT
...
...
ssik425의 이미지

손님으로 접속하여 비꼬는 듯한 말투는 듣기 별로 좋지 않군요..

MORE 를 좀더 자세히 테스트 보기 바람...

stat 값과 ls 상의 값이 동일한지...

개발자들의 궁극적 비전은 ?

익명 사용자의 이미지

ssik425 wrote:
손님으로 접속하여 비꼬는 듯한 말투는 듣기 별로 좋지 않군요..

MORE 를 좀더 자세히 테스트 보기 바람...

stat 값과 ls 상의 값이 동일한지...

기분이 상했다면 미안하오.

테스트를 더 해봤소이다.

첫번째, 두번째, 세번째 값을 비교해야하는데,
1970년부터 1초에 1씩 증가시킨 정수값으로는 구분이 확실히 되오.
이를 날짜로 변환하는 함수호출에 미숙함이 보이오.
(미안하오. 미숙해서..써본지 너무 오래되서 다 까먹었소이다.)

궁여지책으로 대충 다음과 같이 코드를 변경했소이다.
그런데, ...

Quote:
printf("%s%s%s", ctime(&a), ctime(&b), ctime(&c));

스타일과
Quote:
printf("%s", ctime(&a));
printf("%s", ctime(&b));
printf("%s", ctime(&c));
스타일 기술의 결과가 왜 틀린지는 모르겠소이다.
후자로 테스트해본 코드를 보시오.
아.. time을 인쇄하는 문제에는 또 :evil: 버그가 있을지도 모르겠구료.

그리고, 답변하려고 노력했으니, 노여움 풀고 용서해주기 바라오.
안해줘도 어쩔수없구료.

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>

void print_time(long sec)
{
   struct timeval tv;
   struct tm*ptm;
   char time_string[40];
   long miliseconds;

   gettimeofday(&tv, NULL);
   tv.tv_sec = sec;
   tv.tv_usec=0;
   ptm = localtime(&tv.tv_sec);
   strftime( time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", ptm);
   miliseconds = tv.tv_usec/1000;
   printf("%s.%031ld\n", time_string, miliseconds);
}

main()
{
   struct stat st;
   char filename[1024];

   sprintf(filename, "./DATA.TXT");
   lstat(filename, &st);
   printf("%d\t\t %d\t\t %d \n", st.st_atime, st.st_mtime, st.st_ctime);
   printf("%s", ctime(&st.st_atime));
   printf("%s", ctime(&st.st_mtime));
   printf("%s", ctime(&st.st_ctime));
   print_time(st.st_atime);
   print_time(st.st_mtime);
   print_time(st.st_ctime);
/* time을 인쇄하는 다양한 방법에 대해 고민해보시요 X 2  */ 
}

댓글 달기

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