계속지나가기
코딩스뮤
계속지나가기
전체 방문자
오늘
어제
  • 코딩스뮤:)
    • Algorithm
      • 백준 문제풀이
      • 프로그래머스 문제풀이
      • 알고리즘 이론
      • 자료구조
      • SW Expert Academy
    • 인공지능(AI)
      • LLMs
      • 자연어처리(NLP)
      • 컴퓨터비전(CV)
      • 딥러닝(DL)
      • 머신러닝(ML)
      • 인공지능기초수학
      • 선형대수학
    • 컴퓨터 세팅
    • Computer Science
      • 유닉스프로그래밍
      • 프로그래밍언어론
      • 디자인패턴
      • 클린코드
      • SW 영어
      • 리눅스
      • 논리회로
    • Server
      • Docker

블로그 메뉴

  • 홈
  • Who Am I(CV)
  • 태그

공지사항

인기 글

태그

  • 네트워크플로우
  • ComputerVision
  • 경사하강법
  • 컴퓨터비전
  • 지도학습
  • DIP
  • MaximumFlow
  • 파이썬 클린코드
  • machinelearning
  • 언어모델
  • SIFT
  • 비지도학습
  • f1-score
  • 패턴인식
  • 최대유량
  • 손실함수
  • 선형회귀
  • 에지검출
  • 결정경계
  • ML
  • networkflow
  • 군집화
  • 디지털이미지처리
  • DigitalImageProcessing
  • 머신러닝
  • 알고리즘
  • NLP
  • 기계학습
  • 비용함수
  • LM

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
계속지나가기

코딩스뮤

[유닉스 시스템 프로그래밍] Ch04. 시스템 정보
Computer Science/유닉스프로그래밍

[유닉스 시스템 프로그래밍] Ch04. 시스템 정보

2020. 10. 19. 21:16
반응형

(본 강의 노트는 한빛 아카데미의 <유닉스 시스템 프로그래밍> 책을 기반으로 하고 있습니다)

Ch04. 시스템 정보

학습목표

  • 유닉스 시스템 정보를 검색하는 함수를 사용할 수 있다.
  • 사용자 관련 정보를 함수를 이용해 검색할 수 있다.
  • 시스템의 시간을 관리하는 함수를 사용할 수 있다

목차

  1. 유닉스 시스템 관련 정보
  2. 사용자 정보 검색
  3. 시간 관리 함수

01. 유닉스 시스템 관련 정보

  • 시스템에 설치된 OS에 관한 정보
  • 호스트명 정보
  • 하드웨어 종류에 관한 정보
  • 하드웨어에 따라 사용할 수 있는 자원의 최대 값
    : 최대 프로세스 개수, 프로세스당 열 수 있는 최대 파일 개수, 메모리 페이지 크기 등

OS 기본 정보 검색

  • 시스템에 설치된 OS에 대한 기본 정보 검색
    • 시스템은 인텔 PC, 솔라리스 10 운영체제 설치, 호스트 명은 hanbit

  • OS 정보 검색 함수 : uname( )
#include <sys/utsname.h>
int uname(struct utsname *name);

시스템 정보 검색과 설정

  • 시스템 정보 검색과 설정 : sysinfo()
    #include <sys/sysinfo.h> 
    int sysinfo(struct sysinfo *info); 
    

    -   sysinfo()는 전체적인 시스템 통계 정보를 가져오기 위해서 사용한다
    -   가져온 정보는 struct sysinfo에 저장됨

 

시스템 자원 정보 검색


: 하드웨어에 따라 사용할 수 있는 자원들의 최댓값 검색

-   시스템 자원 정보 검색 : sysconf( )

: 검색할 정보를 나타내는 상수를 사용해야 함, 상수 정보는 각 표준 테이블 참고

#include <unistd.h>
long sysconf(int name);


-   파일과 디렉토리 관련 자원 검색 : fpathconf( ), pathconf( )

#include <unistd.h>
long pathconf(const char *path, int name);
long fpathconf(int fildes, int name);

   -   경로나 파일기술자에 지정된 파일에 설정된 자원값이나 옵션값 리턴
    -   name 사용할 상수

 

02. 사용자 정보 검색

사용자 정보 검색


-   사용자 정보, 그룹정보, 로그인 기록 검색
: /etc/passwd, /etc/shadow, /etc/group, /var/adm/utmpx

1. 로그인 명 검색 : getlogin()

- /var/adm/utmpx 파일을 검색해 현재 프로세스를 실행한 사용자의 로그인명을 리턴

#include <unistd.h>
char *getlogin(void);

2. 로그인 명 검색 : cuserid()

- 현재 프로세스의 소유자 정보로 로그인명을 찾아 리턴

#include <stdio.h>

char *cuserid(char *s);



3. UID 검색

#include <sys/types.h>
#include <unistd.h>

uid_t getuid(void);
uid_t geteuid(void);

 

패스워드 파일  검색

-   UID로 passwd 파일 읽기 : getpwuid( )

#include <pwd.h>

struct passwd *getpwuid(uid_t uid);

-   이름으로 passwd 파일 읽기 : getpwnam( )

#include <pwd.h>

struct passwd *getpwnam(const char *name);

- passwd 구조체

struct passwd {
    char *pw_name;
    char *pw_passwd;
    uid_t pw_uid;
    gid_t pw_gid;
    char *pw_age;
    char *pw_comment;
    char *pw_gecos;
    char *pw_dir;
    char *pw_shell;
};

-   /etc/passwd 파일 순차적으로 읽기

#include <pwd.h>

struct passwd *getpwent(void);
void setpwent(void);
void endpwent(void);
struct passwd *fgetpwent(FILE *fp);

 

섀도우 파일  검색

- /etc/shadow 파일의 구조

- /etc/shadow 파일 읽기 : getspnam()

#include <shadow.h>

struct spwd *getspnam(const char *name);

- /etc/shadow 파일 순차적으로 읽기

#include <shadow.h>

struct spwd *getspent(void);
void setspent(void);
void endspent(void);
struct spwd *fgetspent(FILE *fp);

그룹 정보 검색

- 그룹 ID 검색하기 : getgid(), getegid()

#include <sys/types.h>
#include <unistd.h>

gid_t getgid(void);
gid_t getegid(void);

- /etc/group 파일 검색 : getgrnam(3), getgrgid(3)

#include <grp.h>

struct group *getgrnam(const char *name);
struct group *getgrgid(gid_t gid);

- /etc/group 파일 순차적으로 읽기

#include <grp.h>

struct group *getgrent(void);
void setgrent(void);
void endgrent(void);
struct group *fgetgrent(FILE *fp);

3. 시간 관리 함수

시간 관리 함수

  • 유닉스 시스템에서 시간 관리

    : UTC(1970/01/01 0시 0분 0초)를 기준으로 현재까지 경과한 시간을 초단위로 저장하고 이를 기준으로 시간 정보 관리

  • 초 단위로 현재 시간 정보 얻기 : time( )

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

time_t time(time_t *tloc);
  • 마이크로 초 단위로 시간 정보 얻기 : gettimeofday( )
#include <sys/time.h>

int gettimeofday(struct timeval *tp, void *tzp);
int settimeofday(struct timeval *tp, void *tzp);

 

  • 시간대 정보 : tzset( )

    • 현재 지역의 시간대로 시간을 설정
#include <time.h>

void tzset(void);

시간의 형태 변환

  • 초 단위 시간 정보 분해 : gmtime( ), localtime( )
    • 초를 인자로 받아 tm구조 리턴, gmtime은 UTC기준, localtime은 지역시간대 기준
#include <time.h>

struct tm *localtime(const time_t *clock);
struct tm *gmtime(const time_t *clock);

- tm 구조체

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;
};

 

  • 초 단위 시간으로 역산 : mktime( )
#include <time.h>

time_t mktime(struct tm *timeptr);

 

형식 지정 시간 출력

  • 초 단위 시간을 변환해 출력하기 : ctime( )
#include <time.h>

char *ctime(const time_t *clock);
  • tm 구조체 시간을 변환해 출력하기 : asctime( )
#include <time.h>

char *asctime(const struct tm *tm);

 

  • 출력 형식 기호 사용 : strftime( )
#include <time.h>

size_t strftime(char *restrict s, size_t maxsize,
const char *restrict format, const struct tm *restrict timeptr);

- 지정자 테이블 참고

반응형

'Computer Science > 유닉스프로그래밍' 카테고리의 다른 글

[유닉스 시스템 프로그래밍] Ch06. 프로세스 생성과 실행  (0) 2020.10.19
[유닉스 시스템 프로그래밍] Ch05. 프로세스 정보  (0) 2020.10.19
[유닉스 이론과 실습] ch06. 파일 접근 권한 관리하기  (0) 2020.10.19
[유닉스 이론과 실습] ch13. 배시쉘 프로그래밍  (0) 2020.10.18
[유닉스 이론과 실습] ch04. vi 사용법 익히기  (0) 2020.10.18
    'Computer Science/유닉스프로그래밍' 카테고리의 다른 글
    • [유닉스 시스템 프로그래밍] Ch06. 프로세스 생성과 실행
    • [유닉스 시스템 프로그래밍] Ch05. 프로세스 정보
    • [유닉스 이론과 실습] ch06. 파일 접근 권한 관리하기
    • [유닉스 이론과 실습] ch13. 배시쉘 프로그래밍
    계속지나가기
    계속지나가기
    NLP Engineer

    티스토리툴바