Split View: [운영체제] 01. 운영체제란 무엇인가
[운영체제] 01. 운영체제란 무엇인가
운영체제란 무엇인가
운영체제(Operating System)는 컴퓨터 하드웨어를 관리하고, 응용 프로그램에게 실행 환경을 제공하는 소프트웨어다. 사용자와 하드웨어 사이의 중재자 역할을 수행한다.
운영체제의 네 가지 구성 요소
컴퓨터 시스템은 크게 네 가지 요소로 구성된다.
- 하드웨어: CPU, 메모리, I/O 장치 등 기본 자원을 제공한다
- 운영체제: 하드웨어를 제어하고 응용 프로그램 간 자원을 조율한다
- 응용 프로그램: 워드 프로세서, 컴파일러, 웹 브라우저 등 사용자 문제를 해결한다
- 사용자: 사람, 다른 기계, 또는 다른 컴퓨터
운영체제의 역할
운영체제는 관점에 따라 두 가지로 정의할 수 있다.
- 자원 할당자(Resource Allocator): CPU 시간, 메모리 공간, 저장 장치, I/O 장치 등 모든 자원의 관리자
- 제어 프로그램(Control Program): 프로그램 실행을 제어하여 오류와 부적절한 사용을 방지
더 보편적인 정의로는, 운영체제는 컴퓨터에서 항상 실행 중인 프로그램인 **커널(kernel)**이다.
컴퓨터 시스템 구성
인터럽트(Interrupt)
인터럽트는 하드웨어가 CPU에게 이벤트 발생을 알리는 메커니즘이다. I/O 장치가 작업을 완료하면 인터럽트를 통해 CPU에 신호를 보낸다.
[인터럽트 처리 과정]
1. I/O 장치가 작업 완료
2. 장치 컨트롤러가 인터럽트 신호 발생
|
v
3. CPU가 현재 작업 중단
4. 현재 상태(PC, 레지스터)를 스택에 저장
|
v
5. 인터럽트 벡터를 통해 ISR 주소 확인
6. ISR(Interrupt Service Routine) 실행
|
v
7. 저장된 상태 복원
8. 중단된 작업 재개
인터럽트의 종류는 다음과 같다.
- 하드웨어 인터럽트: I/O 장치, 타이머 등에 의해 발생
- 소프트웨어 인터럽트(트랩): 오류(0으로 나누기)나 시스템 콜에 의해 발생
// 인터럽트 벡터 테이블 개념도
// 각 인터럽트 번호에 대응하는 ISR 주소를 저장
typedef void (*isr_handler_t)(void);
isr_handler_t interrupt_vector_table[256];
// 인터럽트 번호 0: 0으로 나누기 예외
// 인터럽트 번호 1: 디버그 예외
// 인터럽트 번호 14: 페이지 폴트
// ...
// 인터럽트 번호 32~255: 사용자 정의(I/O 장치 등)
void handle_interrupt(int interrupt_number) {
// 인터럽트 벡터에서 핸들러를 찾아 실행
isr_handler_t handler = interrupt_vector_table[interrupt_number];
if (handler != NULL) {
handler();
}
}
저장 장치 계층 구조
저장 장치는 속도, 비용, 휘발성에 따라 계층을 이룬다.
[저장 장치 계층]
빠름 / 비쌈 / 작음
+-----------+
| 레지스터 | < 1ns
+-----------+
| 캐시 (L1) | ~1ns
+-----------+
| 캐시 (L2) | ~4ns
+-----------+
| 캐시 (L3) | ~10ns
+-----------+
| 메인 메모리 | ~100ns (휘발성)
+-----------+
| SSD | ~100us (비휘발성)
+-----------+
| HDD | ~10ms
+-----------+
느림 / 저렴 / 큼
캐싱(Caching)은 느린 저장소의 데이터를 빠른 저장소에 임시로 복사하는 기법이다. 동일한 데이터에 다시 접근할 때 빠른 저장소에서 읽으므로 성능이 향상된다.
I/O 구조
I/O 작업은 CPU와 장치 컨트롤러 간의 상호작용으로 이루어진다.
- 프로그래밍된 I/O: CPU가 직접 데이터를 한 바이트씩 전송 (비효율적)
- 인터럽트 기반 I/O: 장치가 준비되면 인터럽트로 CPU에 알림
- DMA(Direct Memory Access): 대량 데이터 전송 시 CPU 개입 없이 장치 컨트롤러가 직접 메모리에 접근
[DMA 동작 과정]
CPU DMA 컨트롤러 메모리
| | |
|-- DMA 전송 요청 --------->| |
| (소스, 목적지, 크기) | |
| |--- 데이터 전송 --->|
| |--- 데이터 전송 --->|
| |--- 데이터 전송 --->|
|<-- 전송 완료 인터럽트 ----| |
| | |
컴퓨터 시스템 아키텍처
멀티프로세서 시스템
현대 컴퓨터는 대부분 멀티프로세서 시스템이다. 장점은 다음과 같다.
- 처리량 증가: N개의 프로세서가 있으면 처리 속도가 향상된다 (단, N배는 아님)
- 규모의 경제: 여러 단일 프로세서 시스템보다 비용 효율적
- 신뢰성 향상: 하나가 고장나도 전체 시스템은 계속 동작 (graceful degradation)
멀티프로세서 시스템에는 두 가지 유형이 있다.
- 비대칭 멀티프로세싱(AMP): 하나의 마스터 프로세서가 시스템을 제어하고, 나머지는 지시를 따름
- 대칭 멀티프로세싱(SMP): 각 프로세서가 동등한 위치에서 OS 기능을 수행
[SMP 아키텍처]
CPU 0 CPU 1 CPU 2
[레지스터] [레지스터] [레지스터]
[캐시] [캐시] [캐시]
| | |
+------+------+------+------+
| |
[공유 메모리] [I/O 시스템]
멀티코어 시스템
멀티코어 프로세서는 하나의 칩에 여러 코어를 포함한다. 같은 칩 안에서 통신하므로 별도 프로세서 간 통신보다 빠르고 전력 소비도 적다.
[멀티코어 프로세서 구조]
+--------------------------------------+
| 하나의 CPU 칩 |
| +--------+ +--------+ +--------+ |
| | 코어 0 | | 코어 1 | | 코어 2 | |
| |[L1캐시] | |[L1캐시] | |[L1캐시] | |
| +--------+ +--------+ +--------+ |
| +----------+ |
| | L2 캐시 | |
| +----------+ |
+--------------------------------------+
|
[메인 메모리]
NUMA (Non-Uniform Memory Access)
CPU 수가 많아지면 시스템 버스가 병목이 된다. NUMA는 각 CPU에 자체 로컬 메모리를 제공하여 이 문제를 해결한다.
[NUMA 아키텍처]
+------------------+ 인터커넥트 +------------------+
| 노드 0 |<================>| 노드 1 |
| CPU 0 CPU 1 | | CPU 2 CPU 3 |
| [로컬 메모리 0] | | [로컬 메모리 1] |
+------------------+ +------------------+
로컬 메모리 접근: 빠름 (~100ns)
원격 메모리 접근: 느림 (~300ns)
운영체제 운영 방식
이중 모드(Dual-Mode) 운영
OS는 사용자 프로그램의 잘못된 동작으로부터 시스템을 보호해야 한다. 이를 위해 두 가지 실행 모드를 제공한다.
- 사용자 모드(User Mode): 응용 프로그램 실행. 특권 명령어 실행 불가
- 커널 모드(Kernel Mode): OS 코드 실행. 모든 명령어 실행 가능
[모드 전환 과정]
사용자 모드 커널 모드
| |
|-- 시스템 콜 호출 ---------------->|
| (트랩 발생, 모드 비트 0) |
| |-- 시스템 콜 처리
| |
|<-- 시스템 콜 반환 ---------------|
| (모드 비트 1로 복원) |
| |
// 시스템 콜 예시: 파일 읽기
// 사용자 프로그램에서 read() 호출
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
int main() {
char buffer[1024];
// open()은 시스템 콜을 통해 커널 모드로 전환
int fd = open("data.txt", O_RDONLY);
// read()도 시스템 콜 - 커널이 I/O 수행
ssize_t bytes = read(fd, buffer, sizeof(buffer));
printf("읽은 바이트: %zd\n", bytes);
close(fd);
return 0;
}
타이머(Timer)
타이머는 프로그램이 CPU를 독점하는 것을 방지한다. 일정 시간이 지나면 인터럽트를 발생시켜 OS에 제어를 넘긴다.
- 고정 타이머: 일정 간격으로 인터럽트 발생
- 가변 타이머: 고정 클럭 + 카운터로 구현. 카운터가 0이 되면 인터럽트 발생
자원 관리
프로세스 관리
프로세스는 실행 중인 프로그램이다. OS는 다음을 수행한다.
- 프로세스 생성 및 삭제
- 프로세스 일시 정지 및 재개
- 프로세스 동기화 메커니즘 제공
- 프로세스 간 통신(IPC) 메커니즘 제공
- 교착 상태(Deadlock) 처리 메커니즘 제공
메모리 관리
메인 메모리는 CPU가 직접 접근할 수 있는 유일한 대용량 저장소다. OS는 다음을 관리한다.
- 메모리의 어느 부분이 사용 중인지, 누가 사용하는지 추적
- 어떤 프로세스와 데이터를 메모리에 올리고 내릴지 결정
- 필요에 따라 메모리 공간 할당 및 해제
저장 장치 관리
OS는 저장 장치의 물리적 특성을 추상화하여 논리적 파일 개념을 제공한다.
- 파일 시스템 관리: 파일 생성/삭제, 디렉터리 관리, 접근 제어
- 대용량 저장 장치 관리: 디스크 스케줄링, 빈 공간 관리, 저장 장치 할당
- 캐싱: 자주 접근하는 데이터를 더 빠른 저장소에 유지
I/O 서브시스템
OS의 I/O 서브시스템은 하드웨어 장치의 차이를 숨긴다.
- 버퍼링(Buffering): 전송 중 데이터를 임시 저장
- 캐싱(Caching): 자주 사용하는 데이터의 복사본 유지
- 스풀링(Spooling): 프린터 같은 장치의 출력을 대기열에 저장
보호와 보안
- 보호(Protection): OS가 정의한 자원에 대한 프로세스/사용자의 접근을 제어하는 메커니즘
- 보안(Security): 외부 또는 내부의 공격을 방어. 인증, 권한 부여, 감사 등을 포함
각 사용자에게는 고유 ID(UID)가 부여되고, 그룹 ID(GID)를 통해 그룹 단위 접근 제어도 가능하다. 권한 상승(privilege escalation)은 일시적으로 더 높은 권한을 얻는 메커니즘으로, Unix에서는 setuid 비트를 통해 구현한다.
정리
[운영체제 핵심 개념 요약]
+------------------------------------------+
| 응용 프로그램 |
+------------------------------------------+
| 운영체제 (커널) |
| +------+ +--------+ +--------+ +------+ |
| |프로세스| |메모리 | |파일 | |I/O | |
| |관리 | |관리 | |시스템 | |관리 | |
| +------+ +--------+ +--------+ +------+ |
+------------------------------------------+
| 하드웨어 |
| CPU / 메모리 / 디스크 / I/O 장치 |
+------------------------------------------+
운영체제는 하드웨어와 사용자 사이에서 자원을 관리하고, 프로그램 실행 환경을 제공하며, 시스템을 보호하는 핵심 소프트웨어다. 이중 모드 운영, 인터럽트, 타이머 등의 하드웨어 지원을 활용하여 안전하고 효율적인 시스템 운영을 보장한다.
[OS Concepts] 01. What is an Operating System
What is an Operating System
An operating system (OS) is software that manages computer hardware and provides an execution environment for application programs. It acts as an intermediary between the user and the hardware.
Four Components of a Computer System
A computer system consists of four major components.
- Hardware: Provides basic resources such as CPU, memory, and I/O devices
- Operating System: Controls hardware and coordinates resources among application programs
- Application Programs: Word processors, compilers, web browsers, etc., that solve user problems
- Users: People, other machines, or other computers
Roles of the Operating System
An operating system can be defined from two perspectives.
- Resource Allocator: Manager of all resources including CPU time, memory space, storage devices, and I/O devices
- Control Program: Controls program execution to prevent errors and improper use
In a more universal definition, the operating system is the one program running at all times on the computer, called the kernel.
Computer System Organization
Interrupts
An interrupt is a mechanism for hardware to notify the CPU that an event has occurred. When an I/O device completes an operation, it sends a signal to the CPU through an interrupt.
[Interrupt Handling Process]
1. I/O device completes operation
2. Device controller raises interrupt signal
|
v
3. CPU suspends current operation
4. Saves current state (PC, registers) to stack
|
v
5. Looks up ISR address through interrupt vector
6. Executes ISR (Interrupt Service Routine)
|
v
7. Restores saved state
8. Resumes interrupted operation
The types of interrupts are as follows.
- Hardware Interrupts: Triggered by I/O devices, timers, etc.
- Software Interrupts (Traps): Triggered by errors (division by zero) or system calls
// Interrupt vector table conceptual diagram
// Stores ISR addresses corresponding to each interrupt number
typedef void (*isr_handler_t)(void);
isr_handler_t interrupt_vector_table[256];
// Interrupt number 0: Division by zero exception
// Interrupt number 1: Debug exception
// Interrupt number 14: Page fault
// ...
// Interrupt numbers 32~255: User-defined (I/O devices, etc.)
void handle_interrupt(int interrupt_number) {
// Find and execute handler from interrupt vector
isr_handler_t handler = interrupt_vector_table[interrupt_number];
if (handler != NULL) {
handler();
}
}
Storage Device Hierarchy
Storage devices form a hierarchy based on speed, cost, and volatility.
[Storage Device Hierarchy]
Fast / Expensive / Small
+-----------+
| Registers | < 1ns
+-----------+
| Cache (L1) | ~1ns
+-----------+
| Cache (L2) | ~4ns
+-----------+
| Cache (L3) | ~10ns
+-----------+
| Main Memory| ~100ns (Volatile)
+-----------+
| SSD | ~100us (Non-volatile)
+-----------+
| HDD | ~10ms
+-----------+
Slow / Cheap / Large
Caching is a technique that temporarily copies data from slower storage to faster storage. When the same data is accessed again, it is read from the faster storage, improving performance.
I/O Structure
I/O operations are performed through interactions between the CPU and device controllers.
- Programmed I/O: CPU directly transfers data byte by byte (inefficient)
- Interrupt-driven I/O: Device notifies CPU via interrupt when ready
- DMA (Direct Memory Access): For bulk data transfers, device controller accesses memory directly without CPU intervention
[DMA Operation Process]
CPU DMA Controller Memory
| | |
|-- DMA transfer request -->| |
| (source, dest, size) | |
| |--- Data transfer ->|
| |--- Data transfer ->|
| |--- Data transfer ->|
|<-- Transfer complete int -| |
| | |
Computer System Architecture
Multiprocessor Systems
Most modern computers are multiprocessor systems. The advantages are as follows.
- Increased Throughput: N processors improve processing speed (but not by N times)
- Economy of Scale: More cost-effective than multiple single-processor systems
- Increased Reliability: If one fails, the overall system continues to operate (graceful degradation)
There are two types of multiprocessor systems.
- Asymmetric Multiprocessing (AMP): One master processor controls the system, and the rest follow instructions
- Symmetric Multiprocessing (SMP): Each processor performs OS functions on an equal basis
[SMP Architecture]
CPU 0 CPU 1 CPU 2
[Registers] [Registers] [Registers]
[Cache] [Cache] [Cache]
| | |
+------+------+------+------+
| |
[Shared Memory] [I/O System]
Multicore Systems
A multicore processor contains multiple cores on a single chip. Since they communicate within the same chip, it is faster than communication between separate processors and consumes less power.
[Multicore Processor Structure]
+--------------------------------------+
| Single CPU Chip |
| +--------+ +--------+ +--------+ |
| | Core 0 | | Core 1 | | Core 2 | |
| |[L1Cache]| |[L1Cache]| |[L1Cache]| |
| +--------+ +--------+ +--------+ |
| +----------+ |
| | L2 Cache | |
| +----------+ |
+--------------------------------------+
|
[Main Memory]
NUMA (Non-Uniform Memory Access)
When the number of CPUs increases, the system bus becomes a bottleneck. NUMA solves this problem by providing each CPU with its own local memory.
[NUMA Architecture]
+------------------+ Interconnect +------------------+
| Node 0 |<================>| Node 1 |
| CPU 0 CPU 1 | | CPU 2 CPU 3 |
| [Local Memory 0] | | [Local Memory 1] |
+------------------+ +------------------+
Local memory access: Fast (~100ns)
Remote memory access: Slow (~300ns)
Operating System Operations
Dual-Mode Operation
The OS must protect the system from erroneous user program behavior. To this end, it provides two execution modes.
- User Mode: Application program execution. Privileged instructions cannot be executed
- Kernel Mode: OS code execution. All instructions can be executed
[Mode Transition Process]
User Mode Kernel Mode
| |
|-- System call invocation -------->|
| (trap occurs, mode bit 0) |
| |-- System call processing
| |
|<-- System call return ------------|
| (mode bit restored to 1) |
| |
// System call example: File read
// Calling read() from a user program
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
int main() {
char buffer[1024];
// open() transitions to kernel mode via system call
int fd = open("data.txt", O_RDONLY);
// read() is also a system call - kernel performs I/O
ssize_t bytes = read(fd, buffer, sizeof(buffer));
printf("Bytes read: %zd\n", bytes);
close(fd);
return 0;
}
Timer
A timer prevents a program from monopolizing the CPU. After a set period, it generates an interrupt to return control to the OS.
- Fixed timer: Generates interrupts at regular intervals
- Variable timer: Implemented with a fixed clock + counter. Generates an interrupt when the counter reaches 0
Resource Management
Process Management
A process is a program in execution. The OS performs the following tasks.
- Process creation and deletion
- Process suspension and resumption
- Providing process synchronization mechanisms
- Providing inter-process communication (IPC) mechanisms
- Providing deadlock handling mechanisms
Memory Management
Main memory is the only large storage directly accessible by the CPU. The OS manages the following.
- Tracks which parts of memory are in use and by whom
- Decides which processes and data to load into and remove from memory
- Allocates and deallocates memory space as needed
Storage Management
The OS abstracts the physical characteristics of storage devices to provide the logical concept of files.
- File System Management: File creation/deletion, directory management, access control
- Mass Storage Management: Disk scheduling, free space management, storage allocation
- Caching: Keeping frequently accessed data in faster storage
I/O Subsystem
The OS I/O subsystem hides the differences between hardware devices.
- Buffering: Temporarily stores data during transfer
- Caching: Maintains copies of frequently used data
- Spooling: Stores output for devices like printers in a queue
Protection and Security
- Protection: Mechanisms for controlling process/user access to resources defined by the OS
- Security: Defense against external or internal attacks. Includes authentication, authorization, auditing, etc.
Each user is assigned a unique ID (UID), and group-level access control is also possible through group IDs (GID). Privilege escalation is a mechanism for temporarily gaining higher privileges, implemented in Unix through the setuid bit.
Summary
[Operating System Key Concepts Summary]
+------------------------------------------+
| Application Programs |
+------------------------------------------+
| Operating System (Kernel) |
| +------+ +--------+ +--------+ +------+ |
| |Process| |Memory | |File | |I/O | |
| |Mgmt | |Mgmt | |System | |Mgmt | |
| +------+ +--------+ +--------+ +------+ |
+------------------------------------------+
| Hardware |
| CPU / Memory / Disk / I/O Devices |
+------------------------------------------+
The operating system is essential software that manages resources between hardware and users, provides a program execution environment, and protects the system. It leverages hardware support such as dual-mode operation, interrupts, and timers to ensure safe and efficient system operation.