Understanding Processes, Threads, Memory & File Management in Operating Systems
This article explains the differences between processes and threads, outlines process states, describes inter‑process communication methods, details memory allocation strategies and page‑replacement algorithms, covers file allocation techniques, disk I/O scheduling, and outlines deadlock conditions and prevention methods.
Processes and Threads
Threads share many characteristics of traditional processes and are often called lightweight processes, while traditional processes are heavyweight and usually contain a single thread. Modern operating systems typically allocate multiple threads to a single process.
Fundamental difference : a process is the basic unit of OS resource allocation, whereas a thread is the basic unit of CPU scheduling and execution.
Resource overhead : each process has its own code and data space, leading to higher context‑switch cost; threads share code and data, have separate stacks and program counters, and incur lower switching overhead.
Containment relationship : multiple threads run within one process, so a thread is a part of a process.
Memory allocation : threads share the address space of their parent process, while separate processes have independent address spaces.
Impact relationship : a crash in one process does not affect others, but a thread crash terminates its entire process.
Execution : processes have independent entry and exit points; threads cannot execute independently and must be managed by the application.
Process State Transitions
Running : occupying the CPU and executing.
Ready : ready to run but waiting for CPU availability.
Blocked : waiting for an event.
Created : being created and allocated resources.
Terminated : resources are reclaimed and the PCB is removed.
Interprocess Communication (IPC)
IPC is needed because processes may share limited resources, and uncontrolled access can cause data inconsistency and race conditions. Critical sections must be protected so that only one process accesses shared memory at a time.
Purpose of IPC : data transfer, event notification, resource sharing, and process control.
Pipe communication : half‑duplex byte‑stream; anonymous pipes work only between related processes, while named pipes can be used by unrelated processes.
Message queues : kernel‑resident linked list of messages, overcoming the limitations of signals and pipes.
Signals : asynchronous notifications (e.g., Ctrl+C, kill command).
Semaphores : counting locks to control access to shared resources.
Shared memory : fastest IPC method, often combined with semaphores for synchronization.
Sockets : enable communication between processes on the same or different hosts.
Memory Management
Memory management provides allocation and reclamation of main memory, address translation, memory expansion, and protection.
Allocation Methods
Continuous allocation : assigns a single contiguous block (e.g., 1 GB) to a process.
Fixed partition allocation : divides memory into fixed-size partitions; each partition holds one job.
Dynamic partition allocation : partitions are created on‑the‑fly to fit process sizes.
Allocation Algorithms
First Fit
Best Fit
Worst Fit
Next Fit
Non‑continuous Allocation
Programs can be placed in non‑adjacent partitions.
Paging
Memory is divided into equal‑size frames (e.g., 4 KB). Each process’s logical pages are mapped to physical frames.
Segmentation
Logical address space is divided into variable‑size segments, each with its own base address.
Page‑Replacement Algorithms
Optimal (OPT) : theoretical best, replaces the page that will not be used for the longest time.
FIFO : replaces the oldest loaded page.
LRU : replaces the least recently used page.
Clock (NRU) : uses a reference bit and a circular list to approximate LRU.
File Management
Files are stored on disk in blocks, similar to memory paging.
Allocation Strategies
Contiguous allocation : files occupy a sequence of consecutive blocks (fast sequential access, but low space utilization).
Linked allocation : each block contains a pointer to the next; supports easy extension but only sequential access.
Indexed allocation : an index block stores pointers to all data blocks, allowing random access.
* 显示链接:把用于链接文件各物理块的指针显示的存放在一张表中,即文件分配表。文件目录只需要记录起始块号。一个磁盘只需要设置一张分配表,开机时,将分配表读入内存,并常驻内存
* 优点:支持顺序访问,也支持随机访问,方便文件的扩展,不会产生碎片问题,地址转换不需要访问磁盘,因此文件的访问效率更高
* 缺点:文件分配表需要占据一定的存储空间Storage Space Management
Disks are divided into logical volumes, each containing a directory area (stores file control blocks) and a file area (stores file data).
I/O Management
Disk Scheduling Algorithms
Disk access time = seek time + latency + transfer time.
FCFS (First‑Come‑First‑Served)
SSTF (Shortest Seek Time First)
SCAN (Elevator algorithm)
C‑SCAN (Circular SCAN)
Deadlock
Understanding Deadlock
A deadlock occurs when each process in a set is waiting for an event that only another process in the same set can trigger.
Necessary Conditions
Mutual exclusion
Hold and wait
No preemption
Circular wait
Deadlock Avoidance – Banker’s Algorithm
The algorithm tentatively allocates resources, checks system safety, and rolls back if the state would be unsafe.
Deadlock Recovery
Resource preemption
Process termination
Process rollback
Breaking any one of the four necessary conditions eliminates deadlock.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
