Sistem Operasi: Cheatsheet Komprehensif
Cheatsheet lengkap untuk memahami Sistem Operasi (Operating System) dari konsep fundamental hingga implementasi modern.
1. Pengantar Sistem Operasi
Definisi Sistem Operasi
Sistem Operasi (OS) adalah perangkat lunak sistem yang bertindak sebagai perantara antara pengguna dan perangkat keras komputer. OS mengelola sumber daya hardware dan menyediakan layanan untuk program aplikasi.
Fungsi Utama:
- Resource Manager: Mengelola CPU, memori, storage, dan I/O devices
- Extended Machine: Menyediakan abstraksi untuk menyembunyikan kompleksitas hardware
- Control Program: Mengontrol eksekusi program dan mencegah error
Arsitektur Sistem Operasi
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ-โ
โ User Programs โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ-โค
โ System Libraries (libc) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ-โค
โ System Call Interface (API ke Kernel) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ-โค
โ KERNEL โ
โ โโโโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโ โ
โ โ Process โ Memory โ File System โ โ
โ โ Manager โ Manager โ Manager โ โ
โ โโโโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโค โ
โ โ Device โ Network โ Security โ โ
โ โ Drivers โ Stack โ Module โ โ
โ โโโโโโโโโโโโโดโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ-โค
โ Hardware (CPU, RAM, Disk) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ-โ
Tipe Kernel
| Tipe | Deskripsi | Contoh | Kelebihan | Kekurangan |
|---|---|---|---|---|
| Monolithic | Semua layanan OS berjalan di kernel space | Linux, Unix | Performa tinggi | Kurang modular, crash = sistem down |
| Microkernel | Kernel minimal, layanan di user space | Minix, QNX, L4 | Stabil, modular | Overhead IPC |
| Hybrid | Kombinasi monolithic + microkernel | Windows NT, macOS | Balance performa & modularitas | Kompleksitas |
| Exokernel | Kernel sangat minimal, aplikasi kelola hardware | MIT Exokernel | Fleksibel, efisien | Sulit diprogram |
Mode Eksekusi
Dual Mode Operation:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ USER MODE (Ring 3) โ
โ โข Aplikasi user berjalan di sini โ
โ โข Akses hardware terbatas โ
โ โข Tidak bisa eksekusi privileged instr. โ
โโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ System Call (trap)
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ KERNEL MODE (Ring 0) โ
โ โข Kernel dan driver berjalan di sini โ
โ โข Akses penuh ke hardware โ
โ โข Bisa eksekusi semua instruksi โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Privileged Instructions (hanya di kernel mode):
- I/O instructions
- Memory management (page table manipulation)
- Interrupt management
- Halt instruction
System Calls
System Call adalah mekanisme yang memungkinkan program user mode untuk meminta layanan dari kernel. System call adalah satu-satunya cara legal untuk aplikasi mengakses sumber daya sistem yang dilindungi.
Mengapa System Call Diperlukan?
Aplikasi user tidak bisa langsung mengakses hardware karena:
- Keamanan: Mencegah aplikasi merusak sistem atau mengakses data aplikasi lain
- Abstraksi: Menyembunyikan kompleksitas hardware dari programmer
- Portabilitas: Aplikasi bisa berjalan di hardware berbeda tanpa modifikasi
Mekanisme System Call:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ User Program โ
โ printf("Hello"); โ
โ โ โ
โ โผ โ
โ C Library (libc) โ
โ write(fd, buf, size) โ
โ โ โ
โ โผ โ
โ System Call Wrapper โ
โ mov $1, %rax # syscall number โ
โ mov $fd, %rdi # arg1 โ
โ mov $buf, %rsi # arg2 โ
โ mov $size, %rdx # arg3 โ
โ syscall # interrupt ke kernel โ
โ โ โ
โ โผ โ
โโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Trap/Interrupt
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Kernel Mode โ
โ System Call Handler โ
โ sys_write() { โ
โ // validasi parameter โ
โ // eksekusi operasi I/O โ
โ // return hasil โ
โ } โ
โ โ โ
โ โผ โ
โ Return ke User Mode โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Langkah-langkah System Call:
- User program memanggil library function (misal:
write()) - Library wrapper menyiapkan parameter dan system call number
- Trap instruction (
syscall,int 0x80,SVC) memicu mode switch - Kernel menyimpan context user (registers, stack pointer)
- System call handler mengeksekusi fungsi kernel sesuai nomor
- Kernel mengembalikan hasil ke user space
- Context switch kembali ke user mode dengan hasil
System Call Number:
Setiap system call memiliki nomor unik yang digunakan untuk mengidentifikasi layanan yang diminta:
// Linux x86-64 system call numbers
#define __NR_read 0
#define __NR_write 1
#define __NR_open 2
#define __NR_close 3
#define __NR_fork 57
#define __NR_execve 59
#define __NR_exit 60
Kategori System Calls:
| Kategori | Contoh | Deskripsi |
|---|---|---|
| Process Control | fork(), exec(), exit(), wait() | Membuat, menghentikan, dan mengontrol proses |
| File Management | open(), read(), write(), close(), stat() | Operasi file dan direktori |
| Device Management | ioctl(), read(), write() | Mengakses dan mengontrol device |
| Information Maintenance | getpid(), gettimeofday(), sysinfo() | Mendapatkan informasi sistem |
| Communication | pipe(), shmget(), msgget(), socket() | IPC dan komunikasi jaringan |
| Protection | chmod(), chown(), setuid() | Kontrol akses dan keamanan |
Contoh System Call di Linux:
#include <unistd.h>
#include <sys/syscall.h>
// Direct system call (tidak direkomendasikan)
long result = syscall(SYS_write, STDOUT_FILENO, "Hello\n", 6);
// Via library wrapper (direkomendasikan)
write(STDOUT_FILENO, "Hello\n", 6);
System Call Interface di Berbagai OS:
| OS | Instruction | Calling Convention | Register untuk syscall number |
|---|---|---|---|
| Linux x86-64 | syscall | RDI, RSI, RDX, R10, R8, R9 | RAX |
| Linux x86-32 | int 0x80 | Stack (EBX, ECX, EDX, โฆ) | EAX |
| Windows x64 | syscall | RCX, RDX, R8, R9, stack | RAX |
| macOS/BSD | syscall | RDI, RSI, RDX, R10, R8, R9 | RAX |
| ARM64 | svc #0 | X0-X7 | X8 |
Error Handling:
System call mengembalikan nilai negatif atau -1 jika terjadi error, dan errno di-set dengan kode error:
ssize_t bytes = write(fd, buffer, size);
if (bytes == -1) {
// Error occurred
perror("write"); // prints: write: Bad file descriptor
// atau
fprintf(stderr, "Error: %s\n", strerror(errno));
}
Overhead System Call:
System call memiliki overhead karena:
- Context switch (user โ kernel mode)
- Parameter validation di kernel
- Security checks (permissions, capabilities)
- Interrupt handling
Optimasi:
- VDSO (Virtual Dynamic Shared Object): System call tertentu (seperti
gettimeofday()) diimplementasikan di user space untuk menghindari context switch - Batching: Menggabungkan multiple system calls menjadi satu
- Async I/O: Menggunakan
epoll,kqueue, atauio_uringuntuk mengurangi blocking
System Call vs Library Function:
| System Call | Library Function |
|---|---|
| Interface langsung ke kernel | Wrapper di atas system call |
| Harus melalui trap | Bisa di user space |
| Lebih lambat (context switch) | Lebih cepat (no trap) |
Contoh: read(), write() | Contoh: printf(), strlen() |
Contoh: Perjalanan printf() ke System Call
printf("Hello World\n");
โ
โผ
// Di libc: printf() memformat string
fprintf(stdout, "Hello World\n");
โ
โผ
// Di libc: write() adalah wrapper
write(STDOUT_FILENO, "Hello World\n", 12);
โ
โผ
// Assembly: setup system call
mov $1, %rax // __NR_write = 1
mov $1, %rdi // fd = STDOUT_FILENO
mov $string, %rsi // buffer address
mov $12, %rdx // size
syscall // Trap ke kernel
โ
โผ
// Kernel: sys_write()
sys_write(fd, buf, count) {
// Validasi fd, buffer, permissions
// Copy data dari user space ke kernel
// Panggil device driver
// Return jumlah bytes written
}
Security Implications:
- Sandboxing: Membatasi system calls yang bisa dipanggil (seccomp, AppArmor, SELinux)
- Capabilities: Memberikan privilege tanpa full root
- System call filtering: Memblokir system calls berbahaya (seperti
ptrace,mount)
2. Manajemen Proses
Konsep Proses
Proses adalah program yang sedang dieksekusi. Proses adalah unit dasar dari eksekusi dalam sistem operasi.
Process vs Program:
| Program | Proses |
|---|---|
| Passive entity (file di disk) | Active entity (running) |
| Tidak punya state | Punya state (running, waiting, etc.) |
| Tidak punya resources | Memiliki resources (memory, files) |
Process Control Block (PCB)
PCB menyimpan semua informasi tentang proses:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ PROCESS CONTROL BLOCK โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Process ID (PID) โ
โ Process State โ
โ Program Counter โ
โ CPU Registers โ
โ CPU Scheduling Info (priority) โ
โ Memory Management Info โ
โ Accounting Info (CPU time used) โ
โ I/O Status Info โ
โ List of Open Files โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Process State Diagram
Diagram State Proses (Process State Diagram)
State Transitions:
- New โ Ready: Proses diadmit ke antrian ready
- Ready โ Running: Scheduler memilih proses (dispatch)
- Running โ Ready: Preemption (time slice habis)
- Running โ Waiting: Menunggu I/O atau event
- Waiting โ Ready: I/O selesai
- Running โ Terminated: Proses selesai atau kill
Context Switch
Context Switch adalah proses menyimpan state dari proses yang sedang berjalan dan memuat state proses lain.
Context Switch: Proses P0 โ P1
Overhead Context Switch:
- Waktu untuk save/restore registers
- Cache flush (TLB, CPU cache)
- Pipeline flush
Process Creation
Fork-Exec Model (Unix/Linux):
pid_t pid = fork(); // Duplikasi proses
if (pid == 0) {
// Child process
exec("/bin/ls", args); // Replace dengan program baru
} else if (pid > 0) {
// Parent process
wait(NULL); // Tunggu child selesai
} else {
// Fork failed
perror("fork");
}
Process Tree:
init (PID 1)
โโโ systemd
โ โโโ sshd
โ โ โโโ bash
โ โ โโโ vim
โ โโโ nginx
โ โโโ nginx worker
โ โโโ nginx worker
โโโ cron
Inter-Process Communication (IPC)
| Metode | Deskripsi | Use Case |
|---|---|---|
| Pipe | Unidirectional byte stream | Parent-child communication |
| Named Pipe (FIFO) | Pipe dengan nama di filesystem | Unrelated processes |
| Message Queue | Antrian pesan terstruktur | Producer-consumer |
| Shared Memory | Region memori yang di-share | High-speed data sharing |
| Semaphore | Signaling mechanism | Synchronization |
| Socket | Network-style communication | Local or network IPC |
| Signal | Asynchronous notification | Event notification |
3. Thread
Konsep Thread
Thread adalah unit terkecil dari eksekusi dalam proses. Thread berbagi address space dan resources dengan thread lain dalam proses yang sama.
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ PROSES โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ Shared Resources โโ
โ โ โข Code Section โข Open Files โโ
โ โ โข Data Section โข Heap Memory โโ
โ โ โข Global Variables โข Signal Handlers โโ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ โโโโโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโโ โ
โ โ Thread 1โ โ Thread 2โ โ Thread 3โ โ
โ โโโโโโโโโโโค โโโโโโโโโโโค โโโโโโโโโโโค โ
โ โ Stack โ โ Stack โ โ Stack โ โ
โ โ Regs โ โ Regs โ โ Regs โ โ
โ โ PC โ โ PC โ โ PC โ โ
โ โ ThreadIDโ โ ThreadIDโ โ ThreadIDโ โ
โ โโโโโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Thread vs Process
| Aspek | Process | Thread |
|---|---|---|
| Address Space | Terpisah | Shared |
| Communication | IPC (mahal) | Shared memory (murah) |
| Creation Cost | Tinggi | Rendah |
| Context Switch | Mahal | Murah |
| Isolation | Tinggi (crash terpisah) | Rendah (crash = semua mati) |
| Overhead | Lebih berat | Lebih ringan |
Model Threading
1. User-Level Threads (ULT):
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ User Space โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Thread Library (pthreads) โ โ
โ โ โโโโโ โโโโโ โโโโโ โโโโโ โ โ
โ โ โ T1โ โ T2โ โ T3โ โ T4โ โ โ
โ โ โโโโโ โโโโโ โโโโโ โโโโโ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Kernel Space โ
โ (1 Kernel Thread) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
- Kernel tidak tahu tentang user threads
- Context switch cepat (tidak perlu syscall)
- Jika satu thread block, semua thread block
2. Kernel-Level Threads (KLT):
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ User Space โ
โ โโโโโ โโโโโ โโโโโ โโโโโ โ
โ โ T1โ โ T2โ โ T3โ โ T4โ โ
โ โโโฌโโ โโโฌโโ โโโฌโโ โโโฌโโ โ
โโโโโโโโโโผโโโโโโผโโโโโโผโโโโโโผโโโโโโโโโโค
โ โผ โผ โผ โผ โ
โ โโโโโ โโโโโ โโโโโ โโโโโ โ
โ โKT1โ โKT2โ โKT3โ โKT4โ โ
โ โโโโโ โโโโโ โโโโโ โโโโโ โ
โ Kernel Space โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
- Kernel manage semua threads
- Satu thread block tidak mempengaruhi yang lain
- Overhead lebih tinggi
3. Hybrid (Many-to-Many):
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ User Threads: T1 T2 T3 T4 T5 T6 โ
โ \ | / \ | / โ
โ \ |/ \|/ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Kernel Threads: KT1 KT2 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
- Fleksibel: banyak user thread ke beberapa kernel thread
- Digunakan oleh Solaris, older Windows
POSIX Threads (pthreads)
#include <pthread.h>
void* thread_function(void* arg) {
int* num = (int*)arg;
printf("Thread received: %d\n", *num);
return NULL;
}
int main() {
pthread_t thread_id;
int arg = 42;
// Create thread
pthread_create(&thread_id, NULL, thread_function, &arg);
// Wait for thread to finish
pthread_join(thread_id, NULL);
return 0;
}
Fungsi pthreads penting:
| Fungsi | Deskripsi |
|---|---|
pthread_create() | Buat thread baru |
pthread_join() | Tunggu thread selesai |
pthread_exit() | Terminasi thread |
pthread_cancel() | Batalkan thread |
pthread_mutex_lock() | Lock mutex |
pthread_cond_wait() | Wait pada condition variable |
4. CPU Scheduling
Konsep Scheduling
CPU Scheduler memilih proses dari ready queue untuk dieksekusi di CPU.
Kapan scheduling terjadi:
- Proses switch dari running ke waiting (I/O)
- Proses switch dari running ke ready (interrupt)
- Proses switch dari waiting ke ready (I/O complete)
- Proses terminasi
Non-preemptive vs Preemptive:
| Non-preemptive | Preemptive |
|---|---|
| Proses jalan sampai selesai/block | Proses bisa diinterupsi |
| Sederhana | Lebih kompleks |
| Response time buruk | Response time baik |
| Contoh: FCFS, SJF | Contoh: Round Robin, SRTF |
Kriteria Scheduling
| Kriteria | Definisi | Goal |
|---|---|---|
| CPU Utilization | % waktu CPU sibuk | Maximize |
| Throughput | Jumlah proses selesai/waktu | Maximize |
| Turnaround Time | Waktu dari submit sampai selesai | Minimize |
| Waiting Time | Waktu menunggu di ready queue | Minimize |
| Response Time | Waktu dari submit sampai response pertama | Minimize |
Algoritma Scheduling
1. First-Come First-Served (FCFS)
Ready Queue: P1 โ P2 โ P3 โ CPU
Urutan kedatangan = urutan eksekusi
Contoh:
| Process | Burst Time |
|---|---|
| P1 | 24 |
| P2 | 3 |
| P3 | 3 |
Gantt Chart:
| P1 (24) | P2(3)| P3(3)|
0 24 27 30
Waiting Time: P1=0, P2=24, P3=27
Average WT = (0+24+27)/3 = 17
Convoy Effect: Proses pendek menunggu proses panjang.
Gantt Chart: FCFS vs SJF (P1=24, P2=3, P3=3)
2. Shortest Job First (SJF)
Proses dengan burst time terpendek dieksekusi duluan.
Non-preemptive SJF:
| Process | Arrival | Burst |
|---|---|---|
| P1 | 0 | 7 |
| P2 | 2 | 4 |
| P3 | 4 | 1 |
| P4 | 5 | 4 |
Gantt Chart:
| P1 (7) | P3(1)| P2 (4) | P4 (4) |
0 7 8 12 16
Average WT = (0+6+3+7)/4 = 4
Preemptive SJF (SRTF - Shortest Remaining Time First): Jika proses baru datang dengan burst lebih pendek dari remaining time proses yang sedang jalan, switch!
3. Priority Scheduling
Setiap proses punya priority. CPU diberikan ke proses dengan priority tertinggi.
Masalah: Starvation - Proses low priority mungkin tidak pernah jalan. Solusi: Aging - Tingkatkan priority proses yang sudah lama menunggu.
Priority(t) = BasePriority + (WaitingTime ร AgingFactor)
4. Round Robin (RR)
Setiap proses dapat time quantum (q) untuk eksekusi. Setelah q habis, proses dipindah ke belakang antrian.
Contoh dengan q = 4:
| Process | Burst Time |
|---|---|
| P1 | 24 |
| P2 | 3 |
| P3 | 3 |
Gantt Chart:
|P1(4)|P2(3)|P3(3)|P1(4)|P1(4)|P1(4)|P1(4)|P1(4)|
0 4 7 10 14 18 22 26 30
Queue evolution:
t=0: [P1, P2, P3]
t=4: [P2, P3, P1]
t=7: [P3, P1]
t=10: [P1]
...
Time Quantum Selection:
- q terlalu besar โ jadi FCFS
- q terlalu kecil โ overhead context switch tinggi
- Rule of thumb: 80% CPU bursts < q
5. Multilevel Queue
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Highest Priority โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ System Processes [RR] โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Interactive Processes [RR] โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Batch Processes [FCFS] โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ Lowest Priority โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
6. Multilevel Feedback Queue (MLFQ)
Proses bisa berpindah antar queue berdasarkan perilaku:
- CPU-bound โ turun ke queue priority rendah
- I/O-bound โ naik ke queue priority tinggi
Queue 0 (q=8): [Interactive jobs] โโโ New process
โ timeout
Queue 1 (q=16): [Mixed jobs]
โ timeout
Queue 2 (FCFS): [CPU-bound jobs]
5. Sinkronisasi Proses
Race Condition
Race Condition terjadi ketika hasil eksekusi bergantung pada urutan eksekusi thread yang tidak terprediksi.
Contoh:
// Shared variable
int counter = 0;
// Thread 1 // Thread 2
counter++; counter++;
// Expected: counter = 2
// Possible: counter = 1 (race condition!)
Kenapa bisa salah?
counter++ sebenarnya adalah:
1. LOAD counter โ register
2. ADD register, 1
3. STORE register โ counter
Thread 1: Thread 2:
LOAD (0)
LOAD (0)
ADD (1)
ADD (1)
STORE (1)
STORE (1) โ overwrites!
Hasil: counter = 1 (seharusnya 2)
Critical Section Problem
Critical Section adalah bagian kode yang mengakses shared resource.
Requirements untuk solusi:
- Mutual Exclusion: Hanya satu proses di critical section
- Progress: Jika tidak ada di CS, proses yang mau masuk tidak boleh diblock selamanya
- Bounded Waiting: Ada batas waktu tunggu untuk masuk CS
Petersonโs Solution
Solusi software untuk 2 proses:
// Shared variables
bool flag[2] = {false, false};
int turn;
// Process i (i = 0 atau 1, j = 1-i)
void enter_critical_section(int i) {
int j = 1 - i;
flag[i] = true; // Saya mau masuk
turn = j; // Tapi beri kesempatan dia dulu
while (flag[j] && turn == j)
; // busy wait
}
void exit_critical_section(int i) {
flag[i] = false;
}
Hardware Support
1. Test-and-Set (TAS):
// Atomic instruction
bool test_and_set(bool *target) {
bool old = *target;
*target = true;
return old; // Return nilai lama
}
// Penggunaan
bool lock = false;
void enter_cs() {
while (test_and_set(&lock))
; // busy wait
}
void exit_cs() {
lock = false;
}
2. Compare-and-Swap (CAS):
// Atomic instruction
int compare_and_swap(int *ptr, int expected, int new_value) {
int old = *ptr;
if (old == expected)
*ptr = new_value;
return old;
}
Mutex (Mutual Exclusion)
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void* thread_func(void* arg) {
pthread_mutex_lock(&mutex);
// Critical Section
counter++;
pthread_mutex_unlock(&mutex);
return NULL;
}
Semaphore
Semaphore adalah variabel integer dengan operasi atomic:
- wait(S) atau P(S): Decrement, block jika โค 0
- signal(S) atau V(S): Increment, wake up waiting process
// Binary Semaphore (Mutex)
sem_t mutex;
sem_init(&mutex, 0, 1); // Initial value = 1
sem_wait(&mutex); // P(mutex)
// Critical Section
sem_post(&mutex); // V(mutex)
// Counting Semaphore (Resource pool)
sem_t resources;
sem_init(&resources, 0, 5); // 5 resources available
Classic Synchronization Problems
1. Producer-Consumer (Bounded Buffer)
Bounded Buffer: Producer-Consumer Problem
full = jumlah item di buffer, empty = slot kosong. Producer menunggu jika buffer penuh, Consumer menunggu jika buffer kosong.
#define BUFFER_SIZE 10
int buffer[BUFFER_SIZE];
int in = 0, out = 0;
sem_t empty, full;
pthread_mutex_t mutex;
void init() {
sem_init(&empty, 0, BUFFER_SIZE); // Buffer slots available
sem_init(&full, 0, 0); // Items in buffer
}
void* producer(void* arg) {
int item;
while (1) {
item = produce_item();
sem_wait(&empty); // Wait for empty slot
pthread_mutex_lock(&mutex);
buffer[in] = item;
in = (in + 1) % BUFFER_SIZE;
pthread_mutex_unlock(&mutex);
sem_post(&full); // Signal item available
}
}
void* consumer(void* arg) {
int item;
while (1) {
sem_wait(&full); // Wait for item
pthread_mutex_lock(&mutex);
item = buffer[out];
out = (out + 1) % BUFFER_SIZE;
pthread_mutex_unlock(&mutex);
sem_post(&empty); // Signal empty slot
consume_item(item);
}
}
2. Readers-Writers Problem
sem_t rw_mutex; // For writers
sem_t mutex; // For read_count
int read_count = 0;
void* reader(void* arg) {
while (1) {
sem_wait(&mutex);
read_count++;
if (read_count == 1)
sem_wait(&rw_mutex); // First reader locks
sem_post(&mutex);
// Reading...
sem_wait(&mutex);
read_count--;
if (read_count == 0)
sem_post(&rw_mutex); // Last reader unlocks
sem_post(&mutex);
}
}
void* writer(void* arg) {
while (1) {
sem_wait(&rw_mutex);
// Writing...
sem_post(&rw_mutex);
}
}
3. Dining Philosophers
Dining Philosophers Problem
Solusi dengan asymmetric pickup:
sem_t fork[5]; // All initialized to 1
void philosopher(int id) {
while (1) {
think();
if (id % 2 == 0) {
sem_wait(&fork[id]);
sem_wait(&fork[(id + 1) % 5]);
} else {
sem_wait(&fork[(id + 1) % 5]);
sem_wait(&fork[id]);
}
eat();
sem_post(&fork[id]);
sem_post(&fork[(id + 1) % 5]);
}
}
Monitor
Monitor adalah high-level synchronization construct yang menggabungkan data, procedures, dan synchronization.
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ MONITOR โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Shared Data โ โ
โ โ (private) โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Condition Variables โ โ
โ โ x.wait() x.signal() โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ โโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Procedure 1โ โ Procedure 2 โ โ
โ โ (public) โ โ (public) โ โ
โ โโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ Only one thread at a time โ
โ (implicit mutual exclusion) โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ Entry Queue โโ
โ โ [T3] [T2] [T1] โ enter โโ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
6. Deadlock
Definisi Deadlock
Deadlock adalah situasi dimana dua atau lebih proses saling menunggu resource yang dipegang oleh proses lain, sehingga tidak ada yang bisa melanjutkan.
โโโโโโโโโโโโโ โโโโโโโโโโโโโ
โ Process A โโโโholdsโโ Resource 1โ
โ โ โโโโโโโโโโโโโ
โ โ โฒ
โ waits โ โ waits
โ for โ โ for
โ โผ โ โ
โ Resource 2โโโโโโholdsโโโโโถโ Process B โ
โโโโโโโโโโโโโ โโโโโโโโโโโโโ
A holds R1, waits for R2
B holds R2, waits for R1
โ DEADLOCK!
Kondisi Deadlock (Coffman Conditions)
Deadlock terjadi jika dan hanya jika keempat kondisi ini terpenuhi secara bersamaan:
| Kondisi | Deskripsi |
|---|---|
| 1. Mutual Exclusion | Resource hanya bisa digunakan satu proses pada satu waktu |
| 2. Hold and Wait | Proses memegang resource sambil menunggu resource lain |
| 3. No Preemption | Resource tidak bisa diambil paksa dari proses |
| 4. Circular Wait | Ada rantai circular: P1โP2โP3โโฆโP1 |
Resource Allocation Graph (RAG)
Resource Allocation Graph - Contoh Deadlock
Strategi Penanganan Deadlock
1. Deadlock Prevention
Cegah salah satu dari 4 kondisi:
| Kondisi | Cara Mencegah |
|---|---|
| Mutual Exclusion | Gunakan sharable resources (jarang bisa) |
| Hold and Wait | Request semua resource sekaligus |
| No Preemption | Paksa release jika request gagal |
| Circular Wait | Order resources, request sesuai urutan |
Contoh Circular Wait Prevention:
// Resource ordering: R1 < R2 < R3
// Selalu request dengan urutan ascending
// Correct:
lock(R1); lock(R2); lock(R3);
// Incorrect (bisa deadlock):
lock(R3); lock(R1); // Violates ordering
2. Deadlock Avoidance
Sistem mengevaluasi setiap request untuk memastikan tidak menuju unsafe state.
Bankerโs Algorithm:
State aman jika ada urutan eksekusi $\langle P_1, P_2, โฆ, P_n \rangle$ dimana setiap $P_i$ bisa selesai.
Data Structures:
Available[m]: Resource tersediaMax[n][m]: Maximum demand setiap prosesAllocation[n][m]: Resource yang sudah dialokasiNeed[n][m] = Max - Allocation: Kebutuhan sisa
Safety Algorithm:
1. Work = Available, Finish[i] = false
2. Find i where Finish[i] = false AND Need[i] โค Work
3. If found:
Work = Work + Allocation[i]
Finish[i] = true
Goto step 2
4. If all Finish[i] = true โ SAFE STATE
Contoh:
Proses Allocation Max Need Available
A B C A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3 3 3 2
P1 2 0 0 3 2 2 1 2 2
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
Safe sequence: <P1, P3, P4, P2, P0>
3. Deadlock Detection
Izinkan deadlock terjadi, lalu deteksi dan recover.
Detection Algorithm (mirip safety algorithm):
- Jalankan periodically
- Cari cycle di wait-for graph
Recovery:
- Process Termination: Kill proses di cycle
- Resource Preemption: Ambil resource dari proses
4. Ignore (Ostrich Algorithm)
Abaikan deadlock karena:
- Jarang terjadi
- Cost prevention/detection tinggi
- Restart lebih murah
Digunakan oleh: Unix, Linux, Windows (untuk sebagian resource)
7. Manajemen Memori
Memory Hierarchy
Hierarki Memori (Memory Hierarchy)
Address Binding
Kapan address ditentukan:
| Tahap | Deskripsi | Flexibility |
|---|---|---|
| Compile Time | Address absolut di-generate saat compile | Harus recompile jika pindah |
| Load Time | Address ditentukan saat program di-load | Relocatable code |
| Execution Time | Address ditentukan saat runtime | Perlu hardware support (MMU) |
Logical vs Physical Address
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ CPU (Process) โ โ Physical Memory โ
โ โ โ โ
โ Logical Address โ MMU โ Physical Addressโ
โ (Virtual) โโโโโโโโโโโถโ โ
โ 0x1234 โ Mapping โ 0xABCD โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
- Logical/Virtual Address: Dilihat oleh CPU/process
- Physical Address: Address sebenarnya di RAM
- MMU (Memory Management Unit): Hardware yang melakukan translasi
Contiguous Memory Allocation
Setiap proses mendapat blok memori yang berurutan.
Fixed Partitioning:
โโโโโโโโโโโโโโโโโโโโโโ
โ OS (Reserved) โ
โโโโโโโโโโโโโโโโโโโโโโค
โ Partition 1 (2MB) โ โ Process A (1.5MB) + 0.5MB fragmentation
โโโโโโโโโโโโโโโโโโโโโโค
โ Partition 2 (4MB) โ โ Process B (3MB) + 1MB fragmentation
โโโโโโโโโโโโโโโโโโโโโโค
โ Partition 3 (4MB) โ โ Empty
โโโโโโโโโโโโโโโโโโโโโโค
โ Partition 4 (6MB) โ โ Process C (6MB)
โโโโโโโโโโโโโโโโโโโโโโ
- Internal Fragmentation: Ruang tidak terpakai di dalam partisi
Variable Partitioning:
โโโโโโโโโโโโโโโโโโโโโโ
โ OS (Reserved) โ
โโโโโโโโโโโโโโโโโโโโโโค
โ Process A (1.5MB) โ
โโโโโโโโโโโโโโโโโโโโโโค
โ Hole (2MB) โ โ External fragmentation
โโโโโโโโโโโโโโโโโโโโโโค
โ Process B (3MB) โ
โโโโโโโโโโโโโโโโโโโโโโค
โ Hole (1MB) โ โ External fragmentation
โโโโโโโโโโโโโโโโโโโโโโค
โ Process C (4MB) โ
โโโโโโโโโโโโโโโโโโโโโโ
- External Fragmentation: Lubang-lubang kecil antar partisi
Allocation Strategies:
| Strategy | Deskripsi | Pros/Cons |
|---|---|---|
| First Fit | Cari hole pertama yang cukup | Cepat |
| Best Fit | Cari hole terkecil yang cukup | Minimize waste, tapi banyak small holes |
| Worst Fit | Cari hole terbesar | Leave larger remaining hole |
Paging
Paging membagi memori menjadi fixed-size blocks:
- Frame: Fixed-size block di physical memory
- Page: Fixed-size block di logical memory
- Ukuran umum: 4KB, 2MB, 1GB (huge pages)
Logical Memory Physical Memory
โโโโโโโโโโโโโโ โโโโโโโโโโโโโโ
โ Page 0 โโโโโโโโโโโโโโโโถโ Frame 2 โ
โโโโโโโโโโโโโโค โโโโโโโโโโโโโโค
โ Page 1 โโโโโ โ Frame 0 โโโโโ
โโโโโโโโโโโโโโค โ โโโโโโโโโโโโโโค โ
โ Page 2 โ โ โ Frame 1 โโโโโผโโโ
โโโโโโโโโโโโโโค โ โโโโโโโโโโโโโโค โ โ
โ Page 3 โ โโโโโโโโโโโถโ Frame 5 โ โ โ
โโโโโโโโโโโโโโ โโโโโโโโโโโโโโค โ โ
โ Frame 3 โ โ โ
Page Table โโโโโโโโโโโโโโค โ โ
โโโโโฌโโโโโโโโ โ Frame 4 โ โ โ
โ 0 โ 2 โ โโโโโโโโโโโโโโ โ โ
โโโโโผโโโโโโโโค โ โ
โ 1 โ 5 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโผโโโโโโโโค โ
โ 2 โ 1 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโผโโโโโโโโค
โ 3 โ 0 โ
โโโโโดโโโโโโโโ
Address Translation:
Translasi Alamat dengan Paging (Page Size = 4KB)
20 bits
12 bits
dari table
tetap sama
Page Table Entry (PTE):
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Valid โ Read โ Write โ Exec โ Dirty โ Accessed โ Frame โ
โ (V) โ (R) โ (W) โ (X) โ (D) โ (A) โ No. โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Translation Lookaside Buffer (TLB)
TLB adalah cache untuk page table entries.
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ CPU โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Logical Address โ โ
โ โ Page | Offset โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ TLB โ โ
โ โ Page โ Frame โ โ
โ โ 3 โ 7 โ TLB Hit! (fast) โ โ
โ โ 5 โ 2 โ โ
โ โโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ TLB Miss? โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Page Table (in RAM) โ โ
โ โ (slower, update TLB) โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Effective Access Time (EAT): $$\text{EAT} = h \times (t_{TLB} + t_{mem}) + (1-h) \times (t_{TLB} + 2 \times t_{mem})$$
Dimana:
- $h$ = TLB hit ratio
- $t_{TLB}$ = TLB access time
- $t_{mem}$ = Memory access time
Multi-level Page Tables
Untuk menghemat memori page table:
Two-Level Paging (x86):
โโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโ
โ Directory โ Page Table โ Offset โ
โ (10 bits) โ (10 bits) โ (12 bits) โ
โโโโโโโโโฌโโโโโโโดโโโโโโโโฌโโโโโโโโดโโโโโโโโโโโโโโโโโ
โ โ
โผ โ
โโโโโโโโโโโ โ
โ Page โ โผ
โDirectoryโ โโโโโโโโโโโ
โ โโโโโถโ Page โ
โ Entry โ โ Table โโโโโถ Physical Frame
โโโโโโโโโโโ โโโโโโโโโโโ
Four-Level Paging (x86-64):
PML4 (9 bits) โ PDPT (9 bits) โ PD (9 bits) โ PT (9 bits) โ Offset (12 bits)
โ โ โ โ
512 entries 512 entries 512 entries 512 entries 4KB page
Segmentation
Segmentation membagi program berdasarkan logical divisions:
Logical View: Physical Memory:
โโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Code โโโโโโโโโโโโถโ Code (at base 0x1000) โ
โ Segment โ โ limit 0x500 โ
โโโโโโโโโโโโโโโโโโค โโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Data โโโโโโโโโโโโถโ Data (at base 0x5000) โ
โ Segment โ โ limit 0x300 โ
โโโโโโโโโโโโโโโโโโค โโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Stack โโโโโโโโโโโโถโ Stack (at base 0x8000) โ
โ Segment โ โ limit 0x1000 โ
โโโโโโโโโโโโโโโโโโค โโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Heap โ
โ Segment โ
โโโโโโโโโโโโโโโโโโ
Segment Table:
โโโโโโโโโโโฌโโโโโโโโโโโฌโโโโโโโโโโ
โ Segment โ Base โ Limit โ
โโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโค
โ Code โ 0x1000 โ 0x500 โ
โ Data โ 0x5000 โ 0x300 โ
โ Stack โ 0x8000 โ 0x1000 โ
โ Heap โ 0x9000 โ 0x2000 โ
โโโโโโโโโโโดโโโโโโโโโโโดโโโโโโโโโโ
Paging vs Segmentation
| Aspek | Paging | Segmentation |
|---|---|---|
| Unit Size | Fixed (4KB, dll) | Variable |
| Visibility | Invisible to programmer | Visible (code, data, stack) |
| Fragmentation | Internal | External |
| Sharing | Page-level | Segment-level (lebih natural) |
| Protection | Per page | Per segment |
8. Virtual Memory
Konsep Virtual Memory
Virtual Memory memungkinkan eksekusi proses yang tidak sepenuhnya ada di memori fisik. Bagian yang tidak dipakai disimpan di disk.
Keuntungan:
- Program bisa lebih besar dari RAM fisik
- Lebih banyak proses bisa berjalan bersamaan
- Efisiensi memori (hanya load yang diperlukan)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Virtual Address Space โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Program besar (4GB) โ โ
โ โ Hanya sebagian di RAM โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ Pages in RAM โ Pages on Disk
โผ โผ
โโโโโโโโโโโโ โโโโโโโโโโโโโโโโ
โ Physical โ โ Swap Space โ
โ Memory โ โ (Disk) โ
โ (1GB) โ โ โ
โโโโโโโโโโโโ โโโโโโโโโโโโโโโโ
Demand Paging
Hanya load page ketika diperlukan (on-demand).
Page Fault Handling:
1. CPU generates virtual address
โ
โผ
2. Check page table
โโโโโโโโโโโโโโโโโโโโโโโโโ
โ Valid bit = 0? โโโโโ No โโโถ Access memory normally
โโโโโโโโโโโโโฌโโโโโโโโโโโโ
โ Yes (Page Fault!)
โผ
3. Trap to OS (page fault handler)
โ
โผ
4. Find page on disk (swap space)
โ
โผ
5. Find free frame in memory
(may need page replacement)
โ
โผ
6. Load page from disk to frame
โ
โผ
7. Update page table (valid = 1)
โ
โผ
8. Restart instruction
Effective Access Time dengan Page Fault: $$\text{EAT} = (1-p) \times t_{mem} + p \times t_{page_fault}$$
Dimana:
- $p$ = page fault rate
- $t_{page_fault}$ โ 10ms (disk access)
- $t_{mem}$ โ 100ns
Contoh: Jika p = 0.001: $\text{EAT} = 0.999 \times 100\text{ns} + 0.001 \times 10\text{ms} = 100\text{ns} + 10\mu\text{s} โ 10.1\mu\text{s}$
Page fault sangat mahal! Harus diminimalkan.
Page Replacement Algorithms
Ketika RAM penuh dan perlu load page baru, pilih victim page untuk di-swap out.
1. FIFO (First-In First-Out)
Replace page yang paling lama di memori.
Reference String: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2
Frames: 3
Step Ref Frame State Fault?
1 7 [7, -, -] โ
2 0 [7, 0, -] โ
3 1 [7, 0, 1] โ
4 2 [2, 0, 1] โ (replace 7)
5 0 [2, 0, 1]
6 3 [2, 3, 1] โ (replace 0)
7 0 [2, 3, 0] โ (replace 1)
8 4 [4, 3, 0] โ (replace 2)
...
Total Page Faults: 15
Beladyโs Anomaly: FIFO bisa menghasilkan MORE page faults dengan MORE frames!
2. Optimal (OPT)
Replace page yang tidak akan digunakan untuk waktu terlama.
Reference String: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2
Frames: 3
Step Ref Frame State Fault? Next Use
4 2 [2, 0, 1] โ 7 never used again
6 3 [2, 0, 3] โ 1 not used until later
8 4 [2, 4, 3] โ 0 not used until later
Total Page Faults: 9 (minimum possible)
- Optimal tapi tidak bisa diimplementasi (perlu future knowledge)
- Digunakan sebagai benchmark
3. LRU (Least Recently Used)
Replace page yang paling lama tidak diakses.
Reference String: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2
Frames: 3
Menggunakan timestamp atau stack
Step Ref Frame State Fault? LRU victim
4 2 [2, 0, 1] โ 7 (oldest access)
6 3 [2, 0, 3] โ 1 (oldest access)
8 4 [4, 0, 3] โ 2 (oldest access)
Total Page Faults: 12
Implementasi LRU:
- Counter-based: Setiap page punya timestamp
- Stack-based: Page diakses โ pindah ke top of stack
- Approximation: Reference bit (cheaper)
4. LRU Approximation Algorithms
Second Chance (Clock) Algorithm:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Circular Buffer โ
โ โ
โ โโโโโ โโโโโ โโโโโ โโโโโ โ
โ โ A โ โ โ B โ โ โ C โ โ โ D โ โ
โ โR=1โ โR=0โ โR=1โ โR=0โ โ
โ โโโโโ โโโฒโโ โโโโโ โโโโโ โ
โ โ โ
โ pointer โ
โ โ
โ Algorithm: โ
โ 1. If R=0: Replace this page โ
โ 2. If R=1: Set R=0, move pointer โ
โ 3. Repeat until victim found โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Enhanced Second Chance: Menggunakan (Reference bit, Modified bit):
| Class | (R, M) | Description | Priority |
|---|---|---|---|
| 0 | (0, 0) | Not recently used, not modified | Best victim |
| 1 | (0, 1) | Not recently used, modified | Need write to disk |
| 2 | (1, 0) | Recently used, not modified | Might be used again |
| 3 | (1, 1) | Recently used, modified | Worst victim |
Perbandingan FIFO vs LRU (3 Frames, Reference: 1,2,3,4,1,2,5,1,2,3)
Thrashing
Thrashing terjadi ketika sistem menghabiskan lebih banyak waktu untuk paging daripada eksekusi.
Thrashing: CPU Utilization vs Degree of Multiprogramming
Penyebab:
- Terlalu banyak proses
- Tidak cukup frame per proses
- Working set > available memory
Working Set Model: $$WS(t, \Delta) = \text{pages referenced in past } \Delta \text{ time units}$$
Jika $\sum WS_i > \text{total frames}$ โ thrashing
Solusi:
- Suspend processes
- Increase RAM
- Use local replacement (proses hanya affect frame-nya sendiri)
Memory-Mapped Files
Map file langsung ke virtual address space:
// Memory-mapped file I/O
int fd = open("file.txt", O_RDWR);
char *addr = mmap(NULL, file_size, PROT_READ|PROT_WRITE,
MAP_SHARED, fd, 0);
// Access file like memory
addr[0] = 'H';
addr[1] = 'i';
// Changes automatically written to file
munmap(addr, file_size);
close(fd);
Keuntungan:
- No explicit read/write system calls
- Shared memory between processes
- Efficient for random access
Copy-on-Write (COW)
Optimisasi untuk fork():
Before fork():
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ Parent โโโโโโถโ Pages โ
โ Process โ โ (shared) โ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
After fork() - COW:
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ Parent โโโโโโถโ Pages โโโโโโ
โ Process โ โ (read-only) โ โ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โ
โ
โโโโโโโโโโโโโโโ โ
โ Child โโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Process โ (shares same pages)
โโโโโโโโโโโโโโโ
After write by child:
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ Parent โโโโโโถโ Page A โ
โ Process โ โโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโ โ Page A' โ โ Copy created
โ Child โโโโโโถโ (modified) โ
โ Process โ โโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโ
Only copy page when modified โ save memory and time!
9. File System
Konsep File
File adalah named collection of related information yang disimpan di secondary storage.
File Attributes:
| Attribute | Deskripsi |
|---|---|
| Name | Human-readable identifier |
| Type | Extension (OS-dependent) |
| Location | Pointer ke lokasi di disk |
| Size | Ukuran dalam bytes |
| Protection | Access rights (rwx) |
| Time | Created, modified, accessed |
| Owner | User ID |
File Operations:
- Create, Delete, Open, Close
- Read, Write, Seek (reposition)
- Truncate, Append
- Get/Set attributes
Directory Structure
Single-Level Directory:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Root Directory โ
โโโโโโโฌโโโโโโฌโโโโโโฌโโโโโโฌโโโโโโฌโโโโโโฌโโโโโโโโโโค
โfile1โfile2โfile3โfile4โfile5โfile6โ ... โ
โโโโโโโดโโโโโโดโโโโโโดโโโโโโดโโโโโโดโโโโโโดโโโโโโโโโโ
- Sederhana, tapi naming collision
Two-Level Directory:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Master Directory โ
โโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ User 1 โ User 2 โ
โโโโโโโฌโโโโโโฌโโโโโโผโโโโโโฌโโโโโโฌโโโโโโโโโโโโโโโโค
โfile1โfile2โfile3โfile1โfile2โ ... โ
โโโโโโโดโโโโโโดโโโโโโดโโโโโโดโโโโโโดโโโโโโโโโโโโโโโโ
Hierarchical (Tree) Directory:
/
โโโโโโโโโโโผโโโโโโโโโโ
bin home etc
โ โโโโโดโโโโ โ
ls alice bob passwd
โ โ
documents code
โ โ
report.txt main.c
Acyclic-Graph Directory: Memungkinkan shared files/directories (hard links, symbolic links).
Path Names
Absolute Path: Dari root directory
/home/alice/documents/report.txt
Relative Path: Dari current directory
./documents/report.txt
../bob/code/main.c
File System Implementation
Disk Layout
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Boot โ Super โ Free Space โ Inode โ Data โ
โ Blockโ Block โ Management โ Table โ Blocks โ
โโโโโโโโดโโโโโโโโดโโโโโโโโโโโโโดโโโโโโโโโดโโโโโโโโโโโโโโโโโ
| Block | Deskripsi |
|---|---|
| Boot Block | Bootstrap code untuk boot OS |
| Super Block | Metadata filesystem (size, #blocks, #inodes) |
| Free Space | Bitmap atau linked list untuk free blocks |
| Inode Table | Array of inodes (file metadata) |
| Data Blocks | Actual file contents |
Inode (Index Node)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ INODE โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Mode (permissions) : rwxr-xr-x โ
โ Link count : 2 โ
โ Owner UID : 1000 โ
โ Group GID : 1000 โ
โ File size : 4096 bytes โ
โ Access time : ... โ
โ Modify time : ... โ
โ Change time : ... โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Direct blocks (12) : [5,8,12,...] โ
โ Single indirect : [ptrโblock] โ
โ Double indirect : [ptrโptrโb] โ
โ Triple indirect : [ptrโptrโ...]โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Block Addressing:
Inode Block Addressing (Direct, Single, Double, Triple Indirect)
Maximum File Size (block = 4KB, pointer = 4 bytes):
- Direct: $12 \times 4\text{KB} = 48\text{KB}$
- Single: $1024 \times 4\text{KB} = 4\text{MB}$
- Double: $1024^2 \times 4\text{KB} = 4\text{GB}$
- Triple: $1024^3 \times 4\text{KB} = 4\text{TB}$
Directory Implementation
Directory adalah file khusus yang berisi list of (name, inode number):
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Directory File โ
โโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโค
โ Name โ Inode Number โ
โโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโค
โ "." โ 42 โ (current dir)
โ ".." โ 15 โ (parent dir)
โ "report.txt" โ 128 โ
โ "code" โ 89 โ (subdirectory)
โ "image.png" โ 156 โ
โโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโ
File Allocation Methods
1. Contiguous Allocation
โโโโโโฌโโโโโฌโโโโโฌโโโโโฌโโโโโฌโโโโโฌโโโโโฌโโโโโฌโโโโโฌโโโโโ
โ โ A โ A โ A โ โ B โ B โ โ C โ C โ
โโโโโโดโโโโโดโโโโโดโโโโโดโโโโโดโโโโโดโโโโโดโโโโโดโโโโโดโโโโโ
0 1 2 3 4 5 6 7 8 9
Directory: file โ (start, length)
A โ (1, 3)
B โ (5, 2)
C โ (8, 2)
| Pros | Cons |
|---|---|
| Simple | External fragmentation |
| Sequential access cepat | Sulit grow file |
| Random access cepat | Perlu compaction |
2. Linked Allocation
โโโโโโฌโโโโโฌโโโโโฌโโโโโฌโโโโโฌโโโโโฌโโโโโฌโโโโโฌโโโโโฌโโโโโ
โ A3 โ โ B2 โ โ A1 โ B1 โ โ โ A2 โ โ
โโ8 โ โโ-1 โ โโ0 โโ2 โ โ โโ4 โ โ
โโโโโโดโโโโโดโโโโโดโโโโโดโโโโโดโโโโโดโโโโโดโโโโโดโโโโโดโโโโโ
0 1 2 3 4 5 6 7 8 9
Directory: file โ start
A โ 4 (4โ8โ0โend)
B โ 5 (5โ2โend)
| Pros | Cons |
|---|---|
| No external fragmentation | Random access lambat |
| File bisa grow easily | Pointer overhead |
| Reliability (broken link = lost data) |
FAT (File Allocation Table):
FAT Table Disk Blocks
โโโโโโโฌโโโโโโ โโโโโโโ
โ 0 โ 8 โโโโโโโโโโโโ A3 โ
โ 1 โ -1 โ โโโโโโโค
โ 2 โ EOF โ โ B2 โ
โ 3 โ -1 โ โโโโโโโค
โ 4 โ 0 โโโโโโโโโโโโ โ
โ 5 โ 2 โ โโโโโโโค
โ 6 โ -1 โ โ A1 โ
โ 7 โ -1 โ โโโโโโโค
โ 8 โ 4 โโโโโโโโโโโโ B1 โ
โโโโโโโดโโโโโโ โโโโโโโ
3. Indexed Allocation
Index Block for File A: Data Blocks:
โโโโโโโ โโโโโโโ
โ 4 โโโโโโโโโโโโโโโโโโโโโถโData0โ block 4
โ 8 โโโโโโโโโโโโโโโโโโโโโถโData1โ block 8
โ 0 โโโโโโโโโโโโโโโโโโโโโถโData2โ block 0
โ -1 โ โโโโโโโ
โโโโโโโ
Directory: file โ index block
| Pros | Cons |
|---|---|
| Random access cepat | Index block overhead |
| No external fragmentation | Limited file size per index block |
Free Space Management
1. Bitmap (Bit Vector):
Block: 0 1 2 3 4 5 6 7 8 9
Bitmap: 0 1 1 1 0 1 1 0 1 1
โ โ โ
free free free
2. Linked List:
Free List Head โ Block 0 โ Block 4 โ Block 7 โ NULL
Journaling File System
Problem: Crash during write โ inconsistent state
Solution: Write-ahead logging
Transaction:
1. Write to journal (log):
"Delete file X from directory Y"
"Mark blocks 5,6,7 as free"
2. Apply changes to disk
3. Mark transaction as complete in journal
4. (Periodically) Clear old journal entries
Recovery:
- Replay uncommitted transactions from journal
- Or rollback incomplete transactions
Journaling Modes:
| Mode | Journal Contains | Consistency | Performance |
|---|---|---|---|
| Writeback | Metadata only | Low | Fast |
| Ordered | Metadata (data first) | Medium | Medium |
| Journal | Metadata + Data | High | Slow |
Virtual File System (VFS)
VFS adalah abstraction layer yang menyediakan interface seragam untuk berbagai filesystem.
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ User Applications โ
โโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ POSIX API (open, read, write)
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Virtual File System (VFS) โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ vnode, dentry, superblock, file operations โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโฌโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโฌโโโโโโโโโโโโ
โ โ โ
โผ โผ โผ
โโโโโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโโ
โ ext4 โ โ NTFS โ โ NFS โ
โ driver โ โ driver โ โ driver โ
โโโโโโฌโโโโโ โโโโโโฌโโโโโ โโโโโโฌโโโโโ
โ โ โ
โโโโโโผโโโโโ โโโโโโผโโโโโ โโโโโโผโโโโโ
โ Disk โ โ Disk โ โ Network โ
โโโโโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโโ
Common File Systems
| File System | OS | Max File Size | Max Volume | Features |
|---|---|---|---|---|
| ext4 | Linux | 16 TB | 1 EB | Journaling, extents |
| XFS | Linux | 8 EB | 8 EB | High performance |
| Btrfs | Linux | 16 EB | 16 EB | COW, snapshots |
| NTFS | Windows | 16 EB | 16 EB | Journaling, ACL |
| APFS | macOS | 8 EB | 8 EB | COW, encryption |
| FAT32 | All | 4 GB | 2 TB | Simple, compatible |
| exFAT | All | 16 EB | 128 PB | Flash optimized |
10. Sistem I/O
I/O Hardware
Device Categories:
| Category | Examples | Characteristics |
|---|---|---|
| Block Devices | HDD, SSD, USB drive | Fixed-size blocks, seekable |
| Character Devices | Keyboard, mouse, serial | Stream of characters |
| Network Devices | Ethernet, WiFi | Packet-based |
I/O Port vs Memory-Mapped I/O:
Port-Mapped I/O: Memory-Mapped I/O:
โโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ
โ CPU โ โ CPU โ
โโโโโโโโโฌโโโโโโโโโ โโโโโโโโโฌโโโโโโโโโ
โ โ
โโโโโโดโโโโโ โโโโโโโโดโโโโโโโ
โ I/O Bus โ โ Memory Bus โ
โโโโโโฌโโโโโ โโโโโโโโฌโโโโโโโ
โ โ
โโโโโโโโโดโโโโโโโโ โโโโโโโโโโดโโโโโโโโโ
โ Device Ports โ โ Memory โ
โ (0x60, 0x64) โ โ โโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโ โ โ RAM โ โ
โ โโโโโโโโโโโโโโค โ
Special I/O instructions โ โ Device Regsโ โ
(IN, OUT) โ โ 0xF0000000 โ โ
โ โโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโ
Normal memory instructions
I/O Methods
1. Polling (Programmed I/O)
CPU terus-menerus check device status.
// Write character to device
while (status_register & BUSY)
; // Busy wait
data_register = character;
command_register = WRITE;
CPU Device
โ โ
โโโcheck statusโ
โโโโโโbusyโโโโโโ
โโโcheck statusโ
โโโโโโbusyโโโโโโ
โโโcheck statusโ
โโโโโโreadyโโโโโ
โโโwrite dataโโโโถ
โ โ
Pros: Simple, low latency untuk fast devices Cons: CPU waste time waiting
2. Interrupt-Driven I/O
CPU melakukan kerja lain, device interrupt ketika siap.
CPU Device
โ โ
โโโstart I/Oโโโโโโโโถ
โ โ (device working)
โ (do other work) โ
โ โ
โโโโโinterruptโโโโโ
โโโread dataโโโโโโโ
โ โ
Interrupt Handling:
1. Device raises interrupt (IRQ)
2. CPU finishes current instruction
3. CPU saves state (registers, PC)
4. CPU jumps to Interrupt Handler
5. Handler processes interrupt
6. Handler acknowledges interrupt
7. CPU restores state
8. CPU resumes execution
3. Direct Memory Access (DMA)
DMA controller transfers data directly between device and memory.
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ CPU โ
โ 1. Setup DMA 4. Handle interrupt โ
โโโโโโโโโโฌโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโ
โ โฒ
Setupโ Interrupt
โผ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ DMA Controller โ
โ โ
โ 2. Transfer data (no CPU involvement) โ
โ Device โโ Memory โ
โ โ
โ 3. Raise interrupt when done โ
โโโโโโโโโโฌโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโ
โ โ
โผ โผ
โโโโโโโโโโโโ โโโโโโโโโโโโ
โ Device โ โ Memory โ
โโโโโโโโโโโโ โโโโโโโโโโโโ
DMA Transfer Steps:
- CPU programs DMA: source, destination, count
- DMA takes over bus, transfers data
- DMA interrupts CPU when complete
- CPU handles interrupt
Perbandingan I/O Methods: CPU Utilization
Device Drivers
Driver adalah software yang mengontrol specific device.
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Application โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ System Call (read, write)
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Kernel I/O Subsystem โ
โ โข Buffering โข Caching โข Scheduling โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Uniform Driver Interface
โผ
โโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโ
โ Disk Driver โ Network Driverโ Keyboard Driver โ
โโโโโโโโโฌโโโโโโโโดโโโโโโโโฌโโโโโโโโดโโโโโโโโโฌโโโโโโโโโ
โ โ โ
โผ โผ โผ
โโโโโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโโ
โ Disk โ โ NIC โ โ Keyboardโ
โโโโโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโโ
Disk Scheduling
Untuk HDD dengan mechanical arm, urutan akses mempengaruhi performance.
Disk Structure:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Disk Platter โ
โ โ
โ โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ โ
โ โ Track 0 (outermost) โ โ
โ โ โญโโโโโโโโโโโโโโโโโโโโโโโฎ โ โ
โ โ โ Track 1 โ โ โ
โ โ โ โญโโโโโโโโโโโโโโโฎ โ โ โ
โ โ โ โ Track 2 โ โ โ โ
โ โ โ โ ... โ โ โ โ
โ โ โฐโโโโดโโโโโโโโโโโโโโโดโโโโฏ โ โ
โ โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ โ
โ โ
โ โ Head movement โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Disk Scheduling Algorithms:
Request Queue: 98, 183, 37, 122, 14, 124, 65, 67 Head starts at: 53
1. FCFS (First Come First Served)
Service order: 53โ98โ183โ37โ122โ14โ124โ65โ67
โ
|----45----|
|----85----|
|----146----|
|----85----|
|----108----|
|----110----|
|----59----|
|--2--|
Total head movement: 640 cylinders
2. SSTF (Shortest Seek Time First)
Pilih request terdekat dari posisi head saat ini.
53โ65โ67โ37โ14โ98โ122โ124โ183
Total head movement: 236 cylinders
Problem: Starvation untuk request jauh dari head
3. SCAN (Elevator Algorithm)
Head bergerak satu arah sampai ujung, lalu balik.
Direction: toward 0 first
53โ37โ14โ0โ65โ67โ98โ122โ124โ183
โ
0 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโถ 199
โ 14 37 53 65 67 98 122 124 183
โ โ โ โ โ โ โ โ โ โ
โ โโโโโดโโโโโ โ โ โ โ โ โ
โ โโโโดโโโโโโโดโโโโโโโดโโโโดโโโโโโโโโโโ
Total head movement: 236 cylinders
4. C-SCAN (Circular SCAN)
Saat sampai ujung, langsung jump ke ujung satunya.
53โ65โ67โ98โ122โ124โ183โ199โ0โ14โ37
โโโโโโโโโโโโโโโโโโโโโโโโโถ
53 65 67 98 122 124 183 199
โ โ โ โ โ โ โ โ
โ โโโโดโโโโดโโโโโโดโโโโดโโโโโโโดโโโโโ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ (jump back to 0)
0 14 37
โ โ โ
โโโโโโดโโโโ
Total head movement: 183 + 199 + 37 = 382 cylinders (but uniform wait)
5. LOOK / C-LOOK
Sama seperti SCAN/C-SCAN tapi tidak pergi sampai ujung disk, hanya sampai request terjauh.
C-LOOK:
53โ65โ67โ98โ122โ124โ183โ14โ37
Total head movement: 130 + 169 + 23 = 322 cylinders
Perbandingan Disk Scheduling (Start: 53, Requests: 98, 183, 37, 122, 14, 124, 65, 67)
Buffering and Caching
Buffer: Temporary storage during transfer Cache: Fast storage for frequently accessed data
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Application โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโ
โ User Buffer โ
โโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโ
โ copy_from_user()
โโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโ
โ Kernel Buffer โ
โ (Page Cache) โ
โโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโ
โ DMA
โโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโ
โ Device Controller โ
โ Buffer โ
โโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโ
โ Disk โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Double Buffering:
Time 1: DMA fills Buffer A โ CPU processes Buffer B
Time 2: DMA fills Buffer B โ CPU processes Buffer A
Time 3: DMA fills Buffer A โ CPU processes Buffer B
...
RAID (Redundant Array of Independent Disks)
| Level | Description | Redundancy | Performance | Capacity |
|---|---|---|---|---|
| RAID 0 | Striping | None | Read/Write โโ | N disks |
| RAID 1 | Mirroring | 1 disk fail | Read โ | N/2 disks |
| RAID 5 | Striping + Distributed Parity | 1 disk fail | Read โ, Write โ | N-1 disks |
| RAID 6 | Double Parity | 2 disk fail | Read โ, Write โโ | N-2 disks |
| RAID 10 | Mirror + Stripe | 1 per mirror | Read/Write โโ | N/2 disks |
Perbandingan RAID Levels
Recovery pada RAID 5: Jika Disk 1 gagal, data dapat direkonstruksi: D1 = Dp XOR D2 XOR D3
11. Security dan Protection
Protection vs Security
| Protection | Security |
|---|---|
| Internal mechanism | External threats |
| Control access to resources | Defend against attacks |
| Policies and mechanisms | Confidentiality, Integrity, Availability |
| Who can do what | How to ensure it |
Protection Domain
Domain = set of (object, access-rights) pairs
Domain D1: { (File1, read), (File2, read/write), (Printer1, print) }
Domain D2: { (File1, execute), (File3, read) }
Access Matrix:
โ File1 โ File2 โ File3 โ Printer1 โ
โโโโโโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโโค
Domain 1 โ read โ read โ โ print โ
โ โ write โ โ โ
โโโโโโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโโค
Domain 2 โ execute โ โ read โ โ
โโโโโโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโโค
Domain 3 โ read โ read โ execute โ print โ
โ execute โ โ โ โ
Access Control Implementation
1. Access Control List (ACL)
Per-object list of (subject, rights).
File1.acl:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ User: alice โ rwx โ
โ User: bob โ r-- โ
โ Group: dev โ r-x โ
โ Other โ --- โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Unix Permissions:
-rwxr-xr-- 1 alice dev 4096 Jan 24 12:00 file.txt
โโโ โโโ โโโ
โโโ โโโ โโดโดโ Other: r--
โโโ โโดโดโโโโโ Group: r-x
โโดโดโโโโโโโโโ Owner: rwx
Octal: 754
Special Permissions:
| Bit | Name | Effect on File | Effect on Directory |
|---|---|---|---|
| SetUID (4) | Set User ID | Run as owner | - |
| SetGID (2) | Set Group ID | Run as group | Inherit group |
| Sticky (1) | Sticky Bit | - | Only owner can delete |
# SetUID example
-rwsr-xr-x root root /usr/bin/passwd
^
SetUID - runs as root regardless of who executes
2. Capability List
Per-subject list of (object, rights).
Alice's Capabilities:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Cap 1: (File1, rwx, key123) โ
โ Cap 2: (Printer1, print, key456)โ
โ Cap 3: (Process2, signal, key789)โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
ACL vs Capability:
| ACL | Capability |
|---|---|
| Easy to see who has access to object | Easy to see what subject can access |
| Easy to revoke all access to object | Easy to transfer access rights |
| Hard to revoke userโs access to all | Hard to revoke all access to object |
Authentication
Something you:
- Know: Password, PIN
- Have: Token, smart card, phone
- Are: Fingerprint, face, iris
Multi-Factor Authentication (MFA): Combine 2+ factors
Password Security:
Storage: hash(password + salt)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Password: "secret123" โ
โ Salt: "x7Km2p" (random per user) โ
โ Hash: bcrypt("secret123" + "x7Km2p") โ
โ = "$2b$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqh"โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Common Security Threats
1. Buffer Overflow
Stack Buffer Overflow Attack
// Vulnerable code
void vulnerable(char *input) {
char buffer[64];
strcpy(buffer, input); // No bounds check!
}
Protections:
- Stack Canaries: Detect overflow before return
- ASLR: Randomize address layout
- DEP/NX: Non-executable stack
- Safe functions:
strncpy,snprintf
2. Privilege Escalation
Gaining higher privileges than authorized.
Normal User โ Root/Admin access
Methods:
- Exploit SetUID binaries
- Kernel vulnerabilities
- Misconfigured services
3. SQL Injection
Input: ' OR '1'='1' --
Query: SELECT * FROM users WHERE name='' OR '1'='1' --'
^^^^^^^^^
Always true!
4. Race Condition (TOCTOU)
Time-of-Check to Time-of-Use vulnerability.
// Vulnerable pattern
if (access("file", W_OK) == 0) { // Check
// Window of vulnerability here!
// Attacker can swap "file" with symlink
file = open("file", O_WRONLY); // Use
write(file, data, len);
}
Security Mechanisms
Sandboxing
Isolate untrusted code:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Host OS โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Sandbox โ โ
โ โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ โ
โ โ โ Untrusted Application โ โ โ
โ โ โ โ โ โ
โ โ โ Limited access to: โ โ โ
โ โ โ - Filesystem (chroot/jail) โ โ โ
โ โ โ - Network (filtered) โ โ โ
โ โ โ - System calls (seccomp) โ โ โ
โ โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Technologies:
- chroot: Filesystem isolation
- seccomp: System call filtering
- Containers: Docker, LXC
- VMs: Full isolation
Mandatory Access Control (MAC)
System-enforced policies (vs discretionary/DAC).
SELinux/AppArmor:
# SELinux policy example
allow httpd_t httpd_content_t : file { read getattr };
^ ^ ^ ^
source target class permissions
domain type
Encryption
At Rest: Disk encryption (LUKS, BitLocker, FileVault) In Transit: TLS/SSL, VPN
Full Disk Encryption:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Boot โ Enter passphrase โ Decrypt โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Encrypted Partition โ โ
โ โ (all data encrypted with key) โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Principle of Least Privilege
โEvery program and user should operate using the least set of privileges necessary to complete the job.โ
Examples:
- Web server doesnโt need root
- User programs shouldnโt access kernel memory
- Services run as dedicated users, not root
12. Virtualisasi
Konsep Virtualisasi
Virtualisasi memungkinkan menjalankan beberapa OS/environment di satu hardware fisik.
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Hardware โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Hypervisor (VMM) โ
โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโค
โ VM 1 โ VM 2 โ VM 3 โ
โ โโโโโโโโโโโโโ โ โโโโโโโโโโโโโ โ โโโโโโโโโโโโโโโโโโโโโ โ
โ โGuest OS โ โ โGuest OS โ โ โGuest OS โ โ
โ โ (Linux) โ โ โ (Windows) โ โ โ (FreeBSD) โ โ
โ โโโโโโโโโโโโโค โ โโโโโโโโโโโโโค โ โโโโโโโโโโโโโโโโโโโโโค โ
โ โ Apps โ โ โ Apps โ โ โ Apps โ โ
โ โโโโโโโโโโโโโ โ โโโโโโโโโโโโโ โ โโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโ
Tipe Hypervisor
Type 1 (Bare-Metal)
Langsung di atas hardware.
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ VM 1 โ VM 2 โ
โโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโค
โ Type 1 Hypervisor โ
โ (VMware ESXi, Xen, Hyper-V) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Hardware โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Examples: VMware ESXi, Xen, Microsoft Hyper-V, KVM
Type 2 (Hosted)
Berjalan di atas Host OS.
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ VM 1 โ VM 2 โ
โโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโค
โ Type 2 Hypervisor โ
โ (VirtualBox, VMware Workstation)โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Host OS โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Hardware โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Examples: VirtualBox, VMware Workstation, Parallels
Virtualization Techniques
1. Full Virtualization
Guest OS tidak perlu dimodifikasi.
Guest OS instruction (privileged)
โ
โผ
โโโโโโโโโโโ
โ VMM โโโโ Binary Translation atau
โ traps โ Hardware Virtualization
โโโโโโฌโโโโโ
โผ
Execute safely
Hardware Support:
- Intel VT-x
- AMD-V
2. Paravirtualization
Guest OS dimodifikasi untuk bekerja sama dengan hypervisor.
Guest OS (modified)
โ
โ Hypercall (instead of privileged instruction)
โผ
โโโโโโโโโโโ
โ VMM โ
โโโโโโโโโโโ
Examples: Xen (paravirtualized guests)
Pros: Better performance Cons: Need modified guest OS
Memory Virtualization
Guest Virtual โ Guest Physical โ Host Physical
(GVA) (GPA) (HPA)
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ Guest Page โ โ Shadow Page โ โ Physical โ
โ Table โโโโโถโ Table โโโโโถโ Memory โ
โ โ โ (maintained โ โ โ
โ โ โ by VMM) โ โ โ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
EPT/NPT (Extended/Nested Page Tables): Hardware support for two-level translation (GVAโGPAโHPA).
I/O Virtualization
Emulation:
Guest โ Virtual Device โ VMM โ Real Device
(slow)
Paravirtualized I/O (virtio):
Guest (virtio driver) โ VMM โ Real Device
(faster)
Device Passthrough (SR-IOV):
Guest โ Direct access to physical device
(native performance)
Container Virtualization
Containers share host kernel, isolate processes.
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Host OS Kernel โ
โโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโค
โ Container 1 โ Container 2 โ Container 3 โ Container 4โ
โ โโโโโโโโโโโโ โ โโโโโโโโโโโโ โ โโโโโโโโโโโโ โ โโโโโโโโโโ โ
โ โ App A โ โ โ App B โ โ โ App C โ โ โ App D โ โ
โ โโโโโโโโโโโโค โ โโโโโโโโโโโโค โ โโโโโโโโโโโโค โ โโโโโโโโโโค โ
โ โ Libs โ โ โ Libs โ โ โ Libs โ โ โ Libs โ โ
โ โโโโโโโโโโโโ โ โโโโโโโโโโโโ โ โโโโโโโโโโโโ โ โโโโโโโโโโ โ
โโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโดโโโโโโโโโโโโโ
VM vs Container:
| Aspect | Virtual Machine | Container |
|---|---|---|
| Isolation | Full (separate kernel) | Process-level (shared kernel) |
| Startup | Minutes | Seconds |
| Size | GBs | MBs |
| Performance | Near-native | Native |
| Security | Strong isolation | Weaker isolation |
Arsitektur: Virtual Machine vs Container
Linux Container Technologies:
- Namespaces: Isolate system resources
- PID, Network, Mount, User, UTS, IPC, Cgroup
- cgroups: Limit and account resources
- CPU, Memory, I/O, Network
- UnionFS/OverlayFS: Layered filesystem
Docker Image Layers:
โโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Writeable Layer โ โ Container changes
โโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Application Code โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Dependencies โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Base Image (Ubuntu) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโ
13. Sistem Operasi Modern
Real-Time Operating Systems (RTOS)
Hard Real-Time: Deadline HARUS dipenuhi
- Contoh: Pacemaker, ABS brakes
- Missing deadline = catastrophic failure
Soft Real-Time: Deadline sebaiknya dipenuhi
- Contoh: Video streaming, gaming
- Missing deadline = degraded quality
RTOS Characteristics:
- Deterministic scheduling
- Priority-based preemption
- Minimal interrupt latency
- No virtual memory (usually)
Examples: FreeRTOS, VxWorks, QNX, RTLinux
Distributed Operating Systems
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ Node 1 โ โ Node 2 โ โ Node 3 โ
โ โโโโโโโโโ โ โ โโโโโโโโโ โ โ โโโโโโโโโ โ
โ โ App โ โ โ โ App โ โ โ โ App โ โ
โ โโโโโโโโโ โ โ โโโโโโโโโ โ โ โโโโโโโโโ โ
โ โโโโโโโโโ โ โ โโโโโโโโโ โ โ โโโโโโโโโ โ
โ โ OS โ โ โ โ OS โ โ โ โ OS โ โ
โ โโโโโฌโโโโ โ โ โโโโโฌโโโโ โ โ โโโโโฌโโโโ โ
โโโโโโโโผโโโโโโโ โโโโโโโโผโโโโโโโ โโโโโโโโผโโโโโโโ
โ โ โ
โโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโ
Network
Challenges:
- No shared memory
- Network latency
- Partial failures
- Clock synchronization
Mobile Operating Systems
Characteristics:
- Power efficiency critical
- Touch interface
- Limited resources
- App sandboxing
Examples:
- Android: Linux kernel + ART runtime
- iOS: XNU kernel (Darwin-based)
Android Architecture:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Applications โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Application Framework โ
โ (Activity Manager, etc.) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Android Runtime (ART) โ
โ Native Libraries (C/C++) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Hardware Abstraction Layer โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Linux Kernel โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Embedded Operating Systems
- Resource constrained (KB of RAM)
- Specific hardware
- Often no MMU
- Examples: Embedded Linux, Zephyr, NuttX
Cloud and Serverless
Cloud Computing Models:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ IaaS (Infrastructure) โ
โ Virtual Machines, Storage, Network โ
โ (AWS EC2, Google Compute Engine) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ PaaS (Platform) โ
โ Runtime, Database, Development Tools โ
โ (Heroku, Google App Engine) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ FaaS/Serverless โ
โ Function execution only โ
โ (AWS Lambda, Cloud Functions) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
14. Linux Commands Cheatsheet
Process Management
# View processes
ps aux # List all processes
top / htop # Interactive process viewer
pstree # Process tree
# Process control
kill PID # Send SIGTERM
kill -9 PID # Send SIGKILL (force)
killall name # Kill by name
nice -n 10 command # Run with priority
renice -n 5 -p PID # Change priority
# Background/Foreground
command & # Run in background
jobs # List background jobs
fg %1 # Bring job 1 to foreground
bg %1 # Resume job 1 in background
nohup command & # Run immune to hangups
Memory
free -h # Memory usage
vmstat 1 # Virtual memory stats
cat /proc/meminfo # Detailed memory info
pmap PID # Process memory map
Disk & File System
df -h # Disk space usage
du -sh directory # Directory size
lsblk # List block devices
mount /dev/sdb1 /mnt # Mount filesystem
umount /mnt # Unmount
fdisk -l # Partition table
# File permissions
chmod 755 file # rwxr-xr-x
chmod u+x file # Add execute for owner
chown user:group file # Change ownership
System Information
uname -a # Kernel info
cat /etc/os-release # OS info
uptime # System uptime
dmesg # Kernel messages
lscpu # CPU info
lspci # PCI devices
lsusb # USB devices
Network
ip addr # Network interfaces
ip route # Routing table
ss -tuln # Listening ports
netstat -tuln # (legacy) Listening ports
ping host # Test connectivity
traceroute host # Trace route
curl -I url # HTTP headers
Service Management (systemd)
systemctl start service # Start service
systemctl stop service # Stop service
systemctl restart service
systemctl status service # Check status
systemctl enable service # Enable at boot
journalctl -u service # Service logs
15. Rangkuman Cepat
Formula Penting
Effective Access Time (EAT): $$\text{EAT} = h \times t_{cache} + (1-h) \times t_{memory}$$
Page Fault EAT: $$\text{EAT} = (1-p) \times t_{mem} + p \times t_{page_fault}$$
Disk Access Time: $$T_{access} = T_{seek} + T_{rotation} + T_{transfer}$$
CPU Utilization (n processes, I/O wait = p): $$\text{Utilization} = 1 - p^n$$
Comparison Tables
Scheduling Algorithms:
| Algorithm | Preemptive | Starvation | Overhead |
|---|---|---|---|
| FCFS | No | No | Low |
| SJF | No | Yes | Medium |
| SRTF | Yes | Yes | High |
| Priority | Both | Yes | Medium |
| Round Robin | Yes | No | High |
| MLFQ | Yes | No | High |
Page Replacement:
| Algorithm | Optimal | Practical | Beladyโs Anomaly |
|---|---|---|---|
| FIFO | No | Yes | Yes |
| OPT | Yes | No | No |
| LRU | Near-optimal | Expensive | No |
| Clock | Approximation | Yes | No |
Key Concepts Summary
- Kernel Mode vs User Mode: Privilege separation
- Process vs Thread: Heavyweight vs lightweight execution
- Deadlock Conditions: Mutual exclusion, hold & wait, no preemption, circular wait
- Virtual Memory: Address abstraction, demand paging
- File System: Hierarchy, inodes, journaling
- Security: Authentication, authorization, encryption
- Virtualization: Hypervisors, containers, isolation