운영체제/컴퓨터 시스템 구조

시스템 호출(System Call)

Dev.Andy 2023. 4. 16. 16:14

수정일: 2023-12-27

목차

    머리말

    들어가기 전에

    운영체제에 대한 전반적인 내용과 인터럽트에 대해 알아 보았으니 이번에는 시스템 호출에 대해 알아 보자.

    복습 1. 운영체제의 정의와 폰 노이만 구조

    복습 2. 인터럽트(Interrupt)와 실행 과정, 역할

    시스템 호출(System Call)

    정의

    시스템 호출(system call)은 프로세스가 커널에서 제공하는 서비스나 자원을 사용하도록 요청하는 메커니즘이다.

    또한 시스템 호출은 프로세스(실행 중인 프로그램)와 운영체제(정확히는 kernel) 사이의 인터페이스(API) 역할을 하며, 주로 C 언어 계열(C/C++)의 언어로 작성된다.

    open() 함수에 의한 시스템 호출의 작동 과정 이미지
    open() 함수에 의한 시스템 호출의 작동 과정 이미지

    Silbershatz, Abraham, Peter B. Galvin, and Greg Gagne. Operating System Concepts. 10th ed., (New Jersery: John Wiley & Sons, 2018), 65.

    위처럼 사용자(user)가 응용 프로그램(application)을 통해 파일을 읽어야 할 때, 유저 모드에서 커널 모드로 전환되어 C 언어의 open()에 해당 하는 커널 모드의 함수가 실행되어 이에 대한 반환값을 반환한다.

    모드 비트(mode bit)

    듀얼 모드 (유저 모드 & 커널 모드)

    유저 모드와 커널 모드로 이루어져 있으며 비트(bit)로 이를 구분한다. 크게 이렇게 두 가지의 모드로 분리한 이유는 시스템을 보호하기 위해서이다.

    듀얼 모드에 의한 시스템 호출 작동 과정 이미지
    듀얼 모드에 의한 시스템 호출 작동 과정 이미지

    Silbershatz, Galvin, and Gagne. Operating System Concepts. 10th ed., 25.

    프로세스가 실행 되다가 시스템 호출이 호출되면 유저 모드 상태의 모드 비트(1)는 커널 모드의 모드 비트(0)으로 바뀌어 시스템 호출은 실행한다. 다시 유저 모드 상태의 모드 비트(1)로 되돌아 오며 시스템 호출의 반환값을 반환한다.

     

    예시

    C언어 함수 - `printf()` & `write()`

    C 언어의 printf() 함수에 의한 시스템 호출 작동 과정 이미지
    C언어 printf()에 의한 시스템 호출 작동 과정

    Silbershatz, Galvin, and Gagne. Operating System Concepts. 10th ed., 69.

    C언어로 작성한 프로그램에서 `printf()`를 호출하면, 시스템 호출에 의해 운영체제가 `write()`를 호출한다.

    시스템 호출로부터 `write()`의 반환 값을 받아서, 해당 프로그램은 `print()`의 반환 값으로 변환하여 반환한다.

    실습

    `printf()` in user mode

    아래의 코드로 작성한 프로그램을 실행하면 출력 함수 printf()을 실행해야 한다.

    #include <stdio.h>
    
    int main(void) {
        printf("Hello World!\n"); // 유저 모드에서 커널 모드로 변경
    
        return 0;
    }

    `write()`in kernel mode

    커널 모드로 전환되어 printf()에 해당하는 커널 모드의 write() 출력 함수를 통해 반환값으로 "Hello, world!\n"이 출력된다.

    #include <unistd.h>
    #include <string.h>
    #include <stdio.h>
    
    int main(void) {
        char* message = "Hello, world!\n";
        int result = write(STDOUT_FILENO, message, strlen(message));
        if (result == -1) {
            perror("Error writing message");
            return 1;
        }
    
        return 0;
    }

    꼬리말

    프로세스의 종류

    시스템 호출은 크게 6가지의 그룹으로 나뉘어진다.

    1. 프로세스 제어(Process Control)

    • create/terminate process
    • load, execute
    • get/set process attributes
    • wait/signal event
    • allocate and free memory

    2. 파일 관리(File Management)

    • create/delete file
    • open, close
    • read, write, reposition
    • get/set file attributes

    3. 장치 관리(Device Management)

    • request device, release device
    • read, write, reposition
    • get/set device attributes
    • logically attach or detach devices

    4. 정보 유지(Information Maintenance)

    • get/set time or date
    • get/set system data
    • get/set process, file, or device attributes

    5. 통신(Communications)

    • create/delete communication connection
    • send/receive messages
    • transfer status information
    • attach/detach remote devices

    6. 보호(Protection)

    • get/set file permissions