Skip to content
Go back

[Computer Science] Kiến thức về OS cơ bản

Published:  at  11:21 PM

Bài viết tổng hợp câu trả lời cho các câu hỏi về OS cơ bản trong computer science được fork từ repo: https://github.com/vietnakid/learning-material.git

Table of contents

Open Table of contents

What is process, thread? What are the differents between them?


What data process, thread need to live? Why they said that Thread is a lightweight process?

Why thread is a lightweight process?

How CPU switch (context switch) between processes/threads? How data is ensured safety?

What is multi-process and multi-thread? When should we use which one?

Process has how many states? How does it change between each state?

Scheduling algorithm

What will happen if a process is waiting? Or a thread is sleeping?

How CPU detects that a thread is sleeping? Or detect when it wants to run?

What is thread-pool? How to use it? Describe how to create a thread-pool in your programming language.


How to use a Thread Pool?

Describe how to create a Thread Pool in Golang


Can 2 different processes access or change data of each other address space?


Can 2 processes use the same library (e.g., libc is required for every process)? How?

How does debugger work? How it can attach to a running process and change data of that process?

How 2 processes can communicate with each other?

Phương phápHướng dữ liệuĐồng bộVí dụ Golang
PipesMột chiềuos.Pipe()
Shared MemoryHai chiềuKhôngunix.ShmOpen()
Message QueuesHai chiềuMô phỏng qua file (CGO đầy đủ)
SocketsHai chiềunet.Listen("unix")
SignalsKhông dữ liệuKhôngos/signal.Notify()

Pipes (Ống dẫn)

Shared Memory (Bộ nhớ chia sẻ)

Message Queues (Hàng đợi tin nhắn)

Sockets (Unix Domain Sockets)

Signals (Tín hiệu)

What is a child-process? How to create a child-process?


What data a child-process have when we create it?

Can it read/write data on its parent process?

What is Copy on Write (COW)?

What will happen when child-process changes a variable of parent process?

If file descriptor also be inherited by the child process, what if 2 processes handle the same file descriptor or socket?

Concurrency vs Parallels? (in case single CPU core and multiple CPU cores)

What is Critical Zone?

What is Race Condition and How to Handle This Case?

What is Locking Mechanism? Mutex? Semaphore? Spinlock? Read Lock vs Write Lock?

Locking Mechanism (Cơ chế khóa): Là cách bảo vệ critical zone, đảm bảo chỉ một thread/process truy cập tài nguyên chung tại một thời điểm. Có các cơ chế khoá sau:

What is Deadlock and How to Avoid Deadlock?

How does memory is managed in the OS?


What is virtual memory? Why do we need it? How does it work?

image.png

How large virtual memory is?

What is paging?

Can 2 processes map to the same physical address? How and in which case?

What is heap space and stack space?

What will happen with memory when you open an application?


Ví dụ cụ thể - khi bạn click vào icon Chrome

  1. OS tạo process (PID: 1234).
  2. Cấp không gian ảo (ví dụ: 128TB, nhưng chỉ dùng vài MB ban đầu).
  3. Nạp chrome từ ổ cứng:
    • Code: Hàm hiển thị giao diện.
    • Data: Biến như version = "120.0".
  4. Liên kết libc.so (đã có trong RAM từ process khác).
  5. Tạo stack cho main() (lưu biến cục bộ).
  6. Chuẩn bị heap (trống, chờ Chrome mở tab).
  7. Chạy main(), Chrome hiện lên màn hình.

Trạng thái bộ nhớ sau khi mở:

Hình dung đơn giản:

Điều gì xảy ra tiếp theo?

What will happen when you call another function (with parameters) or return from a function?

Ví dụ code:

#include <stdio.h>
void add(int a, int b) {
    int sum = a + b; // Biến cục bộ
    printf("Sum: %d\n", sum);
}
int main() {
    int x = 5;
    add(x, 3); // Gọi hàm
    return 0;
}

Hình dung stack như chồng đĩa: Stack là một chồng đĩa, mỗi lần gọi hàm là đặt một đĩa mới lên trên. Khi hàm xong, đĩa đó được lấy ra.

Bước 1: Trước khi gọi hàm add

Bước 2: Gọi hàm add(x, 3)

Bước 3: Thực thi trong hàm add

Bước 4: Thoát khỏi hàm add (return)

Ví dụ mở rộng (có heap):

#include <stdio.h>
#include <stdlib.h>
int* createArray() {
    int *arr = malloc(3 * sizeof(int)); // Heap
    arr[0] = 1;
    return arr; // Trả về con trỏ
}
int main() {
    int x = 5; // Stack
    int *arr = createArray(); // Gọi hàm
    printf("Arr[0]: %d\n", arr[0]);
    free(arr); // Dọn heap
    return 0;
}

What causes stack-over-flow?

What is dynamic allocating? How does it work?

How does deallocation work?

What happens when your computer is full of memory?

Why you do not need to “deallocate” local variable?

How does Garbage Collection work?

What is a pointer? What difference between pass by value and pass by reference?

Where global variable will be saved?

Biến toàn cục (global variables) được lưu trong một vùng cụ thể của không gian địa chỉ ảo của process, gọi là Data Segment. Dưới đây là chi tiết:

Why in Linux everything is "file"?

How does mouse/keyboard/monitor communicate with your computer?

Các thiết bị ngoại vi (mouse, keyboard, monitor) giao tiếp với máy tính qua driver (trình điều khiển) và interrupt (ngắt) do kernel xử lý.

What is file descriptor?

What is buffer? Why do we need buffer?

What will happen if 2 processes read/write to the same file?

Tùy vào ngữ cảnh thì 2 process đọc/ghi cùng file có thể gây xung đột nếu không đồng bộ. Kết quả phụ thuộc vào cách mở file và cơ chế khóa.

What is system call (syscall)?

How to do a syscall?

What happens with CPU when we execute a syscall?

What is user space and kernel space?

Caching

What is in-memory cache? (memcached/redis)

LRU? implement LRU in your program language! (How about multi-thread?)

Triển khai LRU trong Python

How to migrate Cache stampede?

Quicksort(O(n²) in worst case) vs Merge sort (O(n log n) in worst case). Which is faster? Why? How they use these 2 sorting algorithms in real life?


Suggest Changes

Previous Post
[Computer Science] Kiến thức về Database cơ bản
Next Post
[Computer Science] Kiến thức về networking cơ bản