Core Operating System Concepts Explained
Operating System Fundamentals
System Calls and System Programs
System calls provide an interface between a process and the operating system. They allow user-level processes to request services from the operating system which the process itself is not allowed to perform. In handling the trap, the operating system enters kernel mode, where it has access to privileged instructions and can perform the desired service on behalf of the user-level process. Due to the critical nature of these operations, the operating system performs them every time they are needed. For example, for I/O, a process involves a system call telling the operating system to read or write a particular area, and this request is satisfied by the operating system.
System programs provide basic functionality to users so that they do not need to write their own environment for program development (e.g., editors, compilers) and program execution (e.g., shells). In some sense, they are bundles of useful system calls.
Redundant Array of Independent Disks (RAID)
RAID is an acronym for Redundant Array of Independent (or Inexpensive) Disks. RAID is a method of combining several independent and relatively small disks into a single, large storage unit. The disks included in the array are called array members. Disks can be combined into an array in different ways, which are known as RAID levels. Each RAID level has its own characteristics regarding:
- Fault-tolerance: The ability to survive one or several disk failures.
- Performance: Shows the change in read and write speed of the entire array compared to a single disk.
- Capacity: The amount of user data that can be written to the array. The array capacity depends on the RAID level and does not always match the sum of the sizes of the RAID member disks. To calculate the capacity of a particular RAID type and a set of member disks, you can use a free online RAID calculator.
Deadlock Conditions
Coffman (1971) identified four (4) conditions that must hold simultaneously for a deadlock to occur:
Mutual Exclusion Condition
The resources involved are non-shareable.
Explanation: At least one resource (thread) must be held in a non-shareable mode; that is, only one process at a time claims exclusive control of the resource. If another process requests that resource, the requesting process must be delayed until the resource has been released.
Hold and Wait Condition
A requesting process holds resources already allocated to it while waiting for additional requested resources.
Explanation: There must exist a process that is holding a resource already allocated to it while waiting for additional resources that are currently being held by other processes.
No-Preemption Condition
Resources already allocated to a process cannot be preempted.
Explanation: Resources cannot be removed from processes; they are used to completion or released voluntarily by the process holding them.
Circular Wait Condition
The processes in the system form a circular list or chain where each process in the list is waiting for a resource held by the next process in the list.
Thrashing in Operating Systems
Thrashing is computer activity that makes little or no progress, usually because memory or other resources are exhausted or too limited for needed operations. When this happens, a pattern typically develops in which a process or program makes a request to the operating system, which then tries to find resources by taking them from another process. This, in turn, leads to new requests that cannot be satisfied. In a virtual storage system (an operating system that manages its logical storage or memory in units called pages), thrashing is a condition in which excessive paging operations are taking place.
A system that is thrashing can be perceived as either a very slow system or one that has come to a halt.
Buffering
A link has a capacity that determines the number of messages that can temporarily reside in it. This property can be viewed as a queue of messages attached to the link.
- Zero capacity: The queue has a maximum length of 0; thus, the link cannot have any messages waiting.
- Bounded capacity: The queue has a finite length n; thus, at most n messages can reside in it.
- Unbounded capacity: The queue has potentially infinite length; thus, any number of messages can wait.
Advantages of Round Robin and FCFS Scheduling
Advantages of Round Robin (RR):
- Fairness, as every process gets an equal share of the CPU.
- Newly created processes are added to the end of the ready queue.
- A Round Robin scheduler generally employs time-sharing, giving each job a time slot or quantum.
Advantages of First-Come, First-Served (FCFS):
- Very easy to implement.
- Simplest form of disk scheduling algorithm.
- Easy to program.
- Intrinsically fair.
Dynamic Loading vs. Dynamic Linking
Dynamic loading refers to loading a library (or any other binary) into memory during load time or run time.
Dynamic loading can be compared to plugins, where an executable can begin execution before the dynamic loading occurs (e.g., using a LoadLibrary
call in C or C++).
Dynamic linking refers to the linking process that occurs during load time or run time, rather than when the executable is created.
With dynamic linking, the linker performs minimal work when creating the executable. For the dynamic linker to function, it must also load the libraries, hence it is sometimes called a linking loader.
Deadlock Detection
If deadlock prevention and avoidance are not properly implemented, a deadlock may occur. In such cases, the only remaining actions are to detect and recover from the deadlock.
If all resource types have only a single instance, then we can use a graph called a wait-for-graph, which is a variant of the resource allocation graph. Here, vertices represent processes, and a directed edge from P1 to P2 indicates that P1 is waiting for a resource held by P2. Similar to a resource allocation graph, a cycle in a wait-for-graph indicates a deadlock. So, the system can maintain a wait-for-graph and check for cycles periodically to detect any deadlocks.
The wait-for-graph is not as useful if there are multiple instances for a resource, as a cycle may not necessarily imply a deadlock. In such cases, an algorithm similar to Banker’s algorithm can be used to detect deadlocks. This involves checking if further allocations can be made based on current allocations.
Kernel-Level Threads: Advantages and Disadvantages
In this case, thread management is done by the kernel. There is no thread management code in the application area. Kernel threads are supported directly by the operating system. Any application can be programmed to be multithreaded. All of the threads within an application are supported within a single process.
Advantages:
- The kernel can simultaneously schedule multiple threads from the same process on multiple processors.
- If one thread in a process is blocked, the kernel can schedule another thread of the same process.
- Kernel routines themselves can be multithreaded.
Disadvantages:
- Kernel threads are generally slower to create and manage than user threads.
- Transfer of control from one thread to another within the same process requires a mode switch to the kernel.
Understanding a Process
In computing, a process is an instance of a computer program that is being executed. It contains the program code and its current activity. Depending on the operating system (OS), a process may be made up of multiple threads of execution that execute instructions concurrently.
The Critical Section Problem
In concurrent programming, concurrent accesses to shared resources can lead to unexpected or erroneous behavior, so parts of the program where the shared resource is accessed are protected. This protected section is the critical section or critical region. It cannot be executed by more than one process concurrently. Typically, the critical section accesses a shared resource (such as a data structure, a peripheral device, or a network connection) that does not allow multiple concurrent accesses. The critical section problem refers to the problem of how to ensure that at most one process is executing its critical section at a given time. Important: Critical sections in different threads are not necessarily the same code segment!
Solution Conditions for the Critical Section Problem:
- Mutual Exclusion: If a process is executing in its critical section, no other process may execute in its critical section.
-
Progress: If no process is executing in its critical section and some processes wish to enter their critical sections, then only those processes not in their remainder section can participate in the decision of which will enter its critical section next. This decision cannot be postponed indefinitely.
- If no process is in the critical section, a decision can be made quickly about which process enters.
- Only one process can enter the critical section at a time; in practice, others are often placed in a queue.
-
Bounded Waiting: There must be a bound on the number of times other processes are allowed to enter their critical sections after a process has requested to enter its critical section and before that request is granted.
- The wait time is from when a process requests to enter its critical section until that request is granted.
- In practice, once a process enters its critical section, it typically does not get another turn until a waiting process gets a turn (often managed as a queue).
Understanding a Computer Program
A computer program is a collection of instructions that performs a specific task when executed by a computer. A computer requires programs to function and typically executes the program’s instructions in a central processing unit.
A computer program is usually written by a computer programmer in a programming language. From the program in its human-readable form (source code), a compiler can derive machine code—a form consisting of instructions that the computer can directly execute. Alternatively, a computer program may be executed with the aid of an interpreter.
A part of a computer program that performs a well-defined task is known as an algorithm. A collection of computer programs, libraries, and related data are referred to as software. Computer programs may be categorized along functional lines, such as application software or system software.
Handling of a Page Fault
- The requested memory address is first checked to ensure it is a valid memory request.
- If the reference is invalid, the process is terminated. Otherwise, the page must be paged in.
- A free frame is located, possibly from a free-frame list.
- A disk operation is scheduled to bring in the necessary page from disk. (This usually blocks the process on an I/O wait, allowing another process to use the CPU in the meantime.)
- When the I/O operation is complete, the process’s page table is updated with the new frame number, and the invalid bit is changed to indicate that this is now a valid page reference.
- The instruction that caused the page fault must now be restarted from the beginning (as soon as this process gets another turn on the CPU).