Linux 시스템 호출(syscalls)
Linux 시스템 호출을 사용하여 파일 입출력, 프로세스 관리, 메모리 할당과 같은 일반적인 운영 체제 작업을 수행하는 방법을 학습합니다.
Linux 시스템 호출(syscalls)은(는) CoddyKit의 무료 Assembly Language & x86 Low-Level Systems Programming 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Assembly Language & x86 Low-Level Systems Programming 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Assembly Language & x86 Low-Level Systems Programming 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
What are System Calls?
Welcome! In this lesson, we'll dive into Linux System Calls (syscalls). These are the fundamental way user-space programs request services from the operating system's kernel.
Think of them as a special set of functions that your program can call to do powerful things, like interacting with files, managing processes, or allocating memory.
User vs. Kernel Mode
Modern operating systems operate in different privilege levels. Typically, there's user mode (where your applications run) and kernel mode (where the OS core runs).
- User Mode: Limited access to hardware and critical memory.
- Kernel Mode: Full access, handles system resources securely.
System calls are the controlled gateway for user-mode programs to temporarily switch to kernel mode and ask the OS to perform privileged operations on their behalf.
Invoking Syscalls in x86-64
On x86-64 Linux, system calls are primarily invoked using the syscall instruction. Before calling syscall, you load specific registers with values:
- RAX: Holds the system call number.
- RDI, RSI, RDX, R10, R8, R9: Hold the system call arguments (up to 6).
- The return value is placed back in RAX.
Finding Syscall Numbers
How do you know which number corresponds to which system call? You can look them up!
- Use the
man syscallscommand in your terminal. - Consult header files like
/usr/include/asm/unistd_64.h.
For example, sys_write is system call number 1, and sys_exit is number 60.
Basic File I/O: sys_write
One of the most common syscalls is sys_write, used to write data to a file descriptor. Its parameters are:
- RDI: File descriptor (e.g., 1 for stdout).
- RSI: Pointer to the buffer containing data.
- RDX: Number of bytes to write.
Standard file descriptors are 0 (stdin), 1 (stdout), and 2 (stderr).
Hello, Syscall! Example
Let's write a simple program that prints "Hello, Syscall!" to the console using sys_write and then exits using sys_exit.
Try running this example:
section .data
msg db "Hello, Syscall!", 0xa ; Our string + newline
len equ $ - msg ; Length of our string
section .text
global _start
_start:
; sys_write(fd=1, buf=msg, count=len)
mov rax, 1 ; syscall number for sys_write
mov rdi, 1 ; fd=1 (stdout)
mov rsi, msg ; buffer (our string)
mov rdx, len ; count (length of string)
syscall ; Invoke kernel
; sys_exit(status=0)
mov rax, 60 ; syscall number for sys_exit
mov rdi, 0 ; exit status 0
syscall ; Invoke kernelBasic File I/O: sys_read
The counterpart to sys_write is sys_read, which reads data from a file descriptor into a buffer. Its parameters are:
- RDI: File descriptor (e.g., 0 for stdin).
- RSI: Pointer to the buffer to store data.
- RDX: Maximum number of bytes to read.
sys_read returns the number of bytes actually read, or an error code.
Echoing Input Example
Here's a program that reads up to 256 bytes from standard input (stdin) and then writes whatever it read back to standard output (stdout).
Try running this example:
section .bss
buffer resb 256 ; A buffer to store input
section .text
global _start
_start:
; sys_read(fd=0, buf=buffer, count=256)
mov rax, 0 ; syscall number for sys_read
mov rdi, 0 ; fd=0 (stdin)
mov rsi, buffer ; buffer to store input
mov rdx, 256 ; max bytes to read
syscall ; Invoke kernel
mov rbp, rax ; Store bytes read in rbp
; sys_write(fd=1, buf=buffer, count=rbp)
mov rax, 1 ; syscall number for sys_write
mov rdi, 1 ; fd=1 (stdout)
mov rsi, buffer ; buffer (our read input)
mov rdx, rbp ; count (actual bytes read)
syscall ; Invoke kernel
; sys_exit(status=0)
mov rax, 60 ; syscall number for sys_exit
mov rdi, 0 ; exit status 0
syscall ; Invoke kernelSyscall Arguments Check
Let's quickly check your understanding of how arguments are passed for x86-64 Linux system calls.
Recap: Linux System Calls
You've learned the basics of Linux system calls!
- Syscalls are how user programs request kernel services.
- The
syscallinstruction (x86-64) initiates the call. - RAX holds the syscall number, and RDI-R9 hold arguments.
- We explored
sys_write(to stdout),sys_read(from stdin), andsys_exit.
Mastering syscalls is key to understanding low-level Linux programming and operating system interaction.
자주 묻는 질문
“Linux 시스템 호출(syscalls)” 강의는 무료인가요?
네 — “Linux 시스템 호출(syscalls)” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Assembly Language & x86 Low-Level Systems Programming 강의 전체를 잠금 해제할 수 있습니다. Assembly Language & x86 Low-Level Systems Programming 강의에는 총 4개의 강의가 포함되어 있습니다.
“Linux 시스템 호출(syscalls)”에서 뭘 배우나요?
Linux 시스템 호출을 사용하여 파일 입출력, 프로세스 관리, 메모리 할당과 같은 일반적인 운영 체제 작업을 수행하는 방법을 학습합니다. 브라우저에서 직접 실행하는 실습 코드로 Assembly Language & x86 Low-Level Systems Programming을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Assembly Language & x86 Low-Level Systems Programming을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Assembly Language & x86 Low-Level Systems Programming은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.
“Linux 시스템 호출(syscalls)” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Assembly Language & x86 Low-Level Systems Programming 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Assembly Language & x86 Low-Level Systems Programming 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 가상 메모리 개념
- Linux 시스템 호출(syscalls)
- Windows API 상호 작용
- 동적 메모리: 어셈블리에서 힙 할당하기