- Published on
KembaraXtra-Computer Science - Operating System Software Libraries
I. Introduction
I. Introduction
- An Operating System API is a programmatic interface for interacting with the OS.
- Software libraries provide the concrete implementation for invoking the OS API.
- Definition: A collection of code included with the OS that implements the OS API.
- Similarity to Other Libraries: Similar to programming language standard libraries or community-maintained libraries.
- Format:
- A file containing machine code.
- Typically lacks an entry point, so it can't run alone.
- Exports a set of functions for use by programs.
- Usage: Programs import (link to) functions from the library to use them.
- Implementation:
- Some functions are simple wrappers for kernel system calls.
- Other functions are fully implemented in user mode code within the library.
- Some functions implement logic in user mode but also make system calls.
- Linux: GNU C Library (glibc)
- Provides access to Linux kernel system calls.
- Includes the C programming language's standard library functions (some of which don't require system calls).
- Filename example: libc.so.6 (.so = shared object, 6 = version).
- Widely used in Linux distributions, so considered part of the standard Linux API.
- Windows API Libraries
- Core Libraries:
- kernel32.dll: Exposes system calls from the NT kernel to user-mode programs.
- user32.dll: Exposes system calls related to windowing and user interface.
- gdi32.dll: Exposes system calls related to graphics.
- .dll Extension: Indicates a dynamic link library (shared library code that a process can load and run).
- "32" Suffix: Remnant from the 16-bit to 32-bit Windows transition, retained for compatibility. 64-bit Windows includes both 32-bit and 64-bit versions of these files.
- Core Libraries:
- Method: Programs can bypass the software library by:
- Setting values in processor registers.
- Issuing a processor-specific instruction (e.g., SVC on ARM, SYSCALL on x86).
- Drawbacks:
- Requires assembly language programming.
- Code is not portable across processor architectures.
- Doesn't provide access to OS API functions not implemented as system calls.
- Challenge: Linux and Windows have different system calls and executable formats, making software incompatible.
- WSL Solution: Allows running many 64-bit Linux programs on Windows without modification.
- WSL Versions:
- WSL1: Intercepts Linux system calls and handles them within the NT kernel.
- WSL2: Uses a real Linux kernel running in a virtual machine alongside the NT kernel.
- Published on
- Published on
KembaraXtra- Computer Science - User Mode Bubble and System Calls
I. User Mode Limitations
ii.Put parameters into other registers.
iii.Execute the system call instruction.
IV. System Calls in Practice
I. User Mode Limitations
- Definition: Code running in user mode has restricted system access.
- Capabilities:
- Read/Write to its own virtual memory.
- Perform mathematical and logical operations.
- Control program flow of its own code.
- Limitations:
- Cannot access physical memory addresses (including memory-mapped I/O).
- Cannot directly perform I/O operations (e.g., printing, keyboard input, graphics, sound, network communication, file access).
- "User Mode Bubble": Analogy representing user mode's isolation from direct hardware interaction.
- Practical Effect: User mode code can do work but requires assistance to share results.
- Definition: A request from user mode code for kernel mode code to perform a privileged operation on its behalf.
- Purpose: Allows user mode applications to interact with the outside world (perform I/O).
- Mechanism:
- User mode code requests a specific operation (e.g., reading from a file).
- The kernel (with device drivers) performs the operation.
- The kernel returns the results to the user mode process.
- Kernel's Role:
- Acts as an intermediary between user mode code and hardware resources.
- Provides an abstraction layer, hiding hardware details.
- Constraints: The kernel enforces security and access control policies (e.g., preventing unauthorized file access).
- CPU Instructions: Specific instructions facilitate system calls.
- ARM: SVC (Supervisor Call).
- x86: SYSCALL, SYSENTER.
- System Call Numbers: Each system call is identified by a unique number.
- Example (Linux ARM): write (file writing) is number 4.
- Process:
ii.Put parameters into other registers.
iii.Execute the system call instruction.
IV. System Calls in Practice
- Abstraction: Developers typically don't make system calls directly.
- OS APIs and Standard Libraries: Operating systems and programming languages provide interfaces for system calls (e.g., open, CreateFileA).
- Transparency: Programmers write code at a higher level and may not be aware of the underlying system calls.
- Published on
KembaraXtra-Conputer Science - API Study Guide
I. Core Concept: Application Programming Interface (API)
I. Core Concept: Application Programming Interface (API)
- Definition: A specification that defines how a program interacts with an operating system (OS).
- Purpose: Allows applications to request services from the OS (e.g., creating files, accessing hardware).
- Components:
- Functions: Specific actions an application can request (e.g., open(), CreateFileA(), fopen()).
- Data Structures: Formats for data passed between the application and the OS.
- Implementation: APIs are implemented in software libraries that are included with the OS.
- UI (Shell):
- Interface for users to interact with the OS (e.g., taskbar, Start menu, command line).
- Translates user commands into API calls.
- API:
- Interface for applications to interact with the OS.
- Applications call the API directly, bypassing the UI.
- User Action (via UI): User clicks "New File" in a graphical shell.
- Shell Translation: The UI translates the click into an API call (e.g., a function call to create a file).
- API Call: The API function is invoked (e.g., open() in Unix/Linux, CreateFileA() in Windows).
- OS Action: The OS code (behind the API) executes the request, creating the file.
- Application Action (via API): The application directly invokes open() or CreateFileA() to create a file.
- C:
- Direct access to OS APIs.
- Example (Unix/Linux): open("hello.txt", O_WRONLY|O_CREAT);
- Example (Windows): CreateFileA("hello.txt", GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
- C (Standard Library):
- Provides portable functions that work across different OSes.
- Internally calls the OS-specific API.
- Example: fopen("hello.txt", "w");
- Python:
- Uses a Python interpreter to handle OS-specific API calls.
- The interpreter translates Python code into the correct OS API calls.
- Example: open('hello.txt', 'w')
- POSIX (Portable Operating System Interface):
- Standard for Unix-like systems (Linux, macOS, etc.).
- Defines standards for the OS API, shell behavior, and utilities.
- macOS/iOS:
- Cocoa (macOS): Apple's API for macOS.
- Cocoa Touch (iOS): Apple's API for iOS.
- Android:
- Android Platform APIs: A set of programming interfaces for Android development.
- Windows:
- Win16: Original 16-bit Windows API.
- Win32: 32-bit Windows API.
- Win64: 64-bit Windows API.
- Universal Windows Platform (UWP): Introduced in Windows 10 to provide a consistent API across different Windows devices.
- Published on
KembaraXtra-Computer Science -Virtual Memory
Core Concept
Core Concept
- Virtual Memory: An abstraction that provides each process with its own large, private address space, isolated from other processes.
- Isolation: Prevents processes from accessing or corrupting memory belonging to other processes or the kernel.
- Abstraction: Simplifies memory management for developers, eliminating fragmentation concerns caused by other processes.
- Large Address Space: Gives each process the illusion of a large contiguous memory block, even if the physical memory is smaller.
- Physical Address: Actual hardware memory address. Hidden from user-mode processes.
- Virtual Address: Address seen by the process. Translated to a physical address by the OS.
- Each process gets its own virtual address space (e.g., 2GB).
- Same virtual addresses in different processes map to different physical addresses.
- Mechanisms exist for processes to intentionally share memory.
- The entire virtual address space isn't necessarily backed by physical memory initially. Only parts are mapped to physical memory as needed.
- Separate from user-mode address space.
- Shared by all code running in kernel mode.
- Kernel code can access any part of the kernel address space.
- 32-bit systems have a 4GB (2^32 bytes) virtual address space.
- This 4GB is split between kernel and user mode. Common splits:
- 2GB User / 2GB Kernel
- 3GB User / 1GB Kernel
- Scenario: The total virtual memory requested by processes and the kernel exceeds the available physical RAM.
- Solution: The OS moves inactive "pages" (blocks) of memory from RAM to secondary storage (e.g., hard drive or SSD).
- When Needed: If a process tries to access a paged-out memory location, the OS must swap it back into RAM.
- Tradeoff: Paging allows more virtual memory than physical RAM but introduces a performance penalty due to slower secondary storage access.
- Potential: Huge address spaces (2^64 bytes).
- Reality: Current implementations use fewer bits (e.g., 48-bit addresses, yielding 256TB of virtual address space).
- Still vastly larger than 32-bit address spaces.
- Published on
KembaraXtra-Computer Science -Threads
1. Introduction to Threads
1. Introduction to Threads
- Default Program Execution: Programs typically execute instructions sequentially, handling one task at a time.
- Need for Parallelism: Threads enable programs to perform multiple tasks concurrently (in parallel). Example: A program performing a long calculation while simultaneously updating the user interface (e.g., a progress bar).
- Definition of a Thread: A thread is a schedulable unit of execution within a process, allowing for parallel execution of tasks. It can execute any program code loaded within that process.
- Task Focus: The code run by a thread typically encompasses a specific task the program aims to accomplish.
- Shared Resources: Threads within a process share the same address space, code, and other resources.
- Thread Creation: A process starts with one thread and can create additional threads as needed for parallel task handling.
- Thread ID (TID): Each thread has a unique identifier.
- Windows: Threads and processes are distinct object types. A process acts as a container for threads.
- Linux: Both processes and threads are represented using a single data type. A group of threads sharing an address space and a common process identifier is considered a process. There is no separate process type.
- User Mode: Processes have a Process ID (PID), and threads have a Thread ID (TID).
- Kernel Mode: The Linux kernel refers to a thread's ID as a PID and a process's ID as a thread group identifier (TGID).
- The Illusion of Parallelism: Although threads are said to run in parallel, the actual simultaneous execution depends on the number of processor cores.
- Processor Cores: Each processor core can execute only one thread at a time. The number of cores determines how many threads can run concurrently.
- Physical Core: A hardware implementation of a core within a CPU.
- Logical Core: The ability of a single physical core to run multiple threads simultaneously (one thread per logical core). Intel's hyper-threading is an example. Logical cores do not achieve the full parallelism of physical cores.
- Scheduling: The operating system uses a scheduler, a software component, to manage thread execution.
- Time Allocation: The scheduler allocates short periods of time (quantum) for each thread to run before suspending it to allow other threads to execute.
- Transparency: This scheduling process is mostly hidden from the thread's code, giving the illusion of continuous parallel execution.
- Developer Perspective: Developers write multithreaded applications as if all threads are running continuously in parallel.
- Published on
KembaraXtra-Case Law-Processes
What is a Process?
KembaraXtra-Computer Science -Threads
1. Introduction to Threads
What is a Process?
- Definition: A process is a running instance of a program. It's the OS's way of executing a program.
- Analogy: Think of a program as a recipe, and a process as someone actually following that recipe to bake a cake.
- Location: Processes operate in user mode.
- Container: A process acts as a container for a program's execution.
- Private Virtual Memory Address Space: A dedicated memory space for the process (more on this later).
- Program Code: A copy of the program's instructions loaded into the process's memory.
- State Information: Data about the current status of the process.
- Multiple Instances: You can run the same program multiple times, creating a separate process for each instance.
- Process ID (PID): A unique numerical identifier assigned to each process by the OS.
- Parent Process: The process that starts (creates) another process.
- Child Process: The process created by a parent process.
- Process Tree: The hierarchical relationship between processes, with parent processes branching out to child processes.
- Orphan Process: A child process whose parent has terminated before the child.
- Windows Behavior: The orphaned process remains parentless.
- Linux Behavior: The init process (the first user-mode process) typically adopts the orphaned process.
- Linux:
- pstree utility: Displays the process tree in a textual format.
- Child threads shown with curly braces.
- pstree utility: Displays the process tree in a textual format.
- Windows:
- Process Explorer (Microsoft): A GUI-based tool providing a comprehensive view of running processes.
KembaraXtra-Computer Science -Threads
1. Introduction to Threads
- Default Program Execution: Programs typically execute instructions sequentially, handling one task at a time.
- Need for Parallelism: Threads enable programs to perform multiple tasks concurrently (in parallel). Example: A program performing a long calculation while simultaneously updating the user interface (e.g., a progress bar).
- Definition of a Thread: A thread is a schedulable unit of execution within a process, allowing for parallel execution of tasks. It can execute any program code loaded within that process.
- Task Focus: The code run by a thread typically encompasses a specific task the program aims to accomplish.
- Shared Resources: Threads within a process share the same address space, code, and other resources.
- Thread Creation: A process starts with one thread and can create additional threads as needed for parallel task handling.
- Thread ID (TID): Each thread has a unique identifier.
- Windows: Threads and processes are distinct object types. A process acts as a container for threads.
- Linux: Both processes and threads are represented using a single data type. A group of threads sharing an address space and a common process identifier is considered a process. There is no separate process type.
- User Mode: Processes have a Process ID (PID), and threads have a Thread ID (TID).
- Kernel Mode: The Linux kernel refers to a thread's ID as a PID and a process's ID as a thread group identifier (TGID).
- The Illusion of Parallelism: Although threads are said to run in parallel, the actual simultaneous execution depends on the number of processor cores.
- Processor Cores: Each processor core can execute only one thread at a time. The number of cores determines how many threads can run concurrently.
- Physical Core: A hardware implementation of a core within a CPU.
- Logical Core: The ability of a single physical core to run multiple threads simultaneously (one thread per logical core). Intel's hyper-threading is an example. Logical cores do not achieve the full parallelism of physical cores.
- Scheduling: The operating system uses a scheduler, a software component, to manage thread execution.
- Time Allocation: The scheduler allocates short periods of time (quantum) for each thread to run before suspending it to allow other threads to execute.
- Transparency: This scheduling process is mostly hidden from the thread's code, giving the illusion of continuous parallel execution.
- Developer Perspective: Developers write multithreaded applications as if all threads are running continuously in parallel.
- Published on
KembaraXtra- Computer Science-Kernel Mode and User Mode
I. Core Concept: Privilege Levels
I. Core Concept: Privilege Levels
- Definition: A CPU capability that grants the operating system special rights while restricting other code. It's a way to control what different parts of the software can do.
- Purpose: Ensures programs behave well, prevents interference between programs and the OS, restricts direct hardware access, and protects system files.
- Kernel Mode (Supervisor Mode):
- Privileges: Highest level of privilege; full access to the system (memory, I/O devices, special CPU instructions).
- Trust: Code running in kernel mode is trusted.
- Components: Kernel, device drivers, and key OS components.
- User Mode:
- Privileges: Lower level of privilege; limited access.
- Trust: Code running in user mode is untrusted.
- Components: Most applications.
- Ensuring Trust: Only trusted code runs in kernel mode.
- Control: Allows the operating system to enforce rules and prevent user mode code from misbehaving.
- Security: Prevents direct access to hardware and critical system resources by user applications.
- Key Components:
- Kernel & Executive: Core kernel-mode functionalities (often discussed together). Stored in ntoskrnl.exe.
- Hardware Abstraction Layer (HAL): Isolates the kernel, executive, and device drivers from hardware differences.
- Windowing and Graphics System (win32k): Provides graphics drawing and user interface interaction capabilities.
- Published on
- Published on
KembaraXtra-Computer Science - Operating Systems Overview
I. Introduction to Operating Systems (OS)
I. Introduction to Operating Systems (OS)
- Definition: Software that communicates with computer hardware, providing an environment for program execution.
- Purpose:
- Abstracts hardware details, allowing developers to focus on application logic.
- Provides a common set of services for applications.
- Manages hardware resources (memory, I/O devices, etc.).
- Enables multitasking (running multiple programs concurrently).
- Enforces isolation between programs and the OS, as well as user access control.
- Role as a Layer: Acts as an intermediary between hardware and applications.
- Hides hardware complexity.
- Offers a consistent programming model.
- Facilitates software development across diverse hardware.
- Two Major Categories:
- Kernel: The core of the OS.
- Everything Else: Non-kernel components that provide usability.
- The Kernel:
- Responsibilities:
- Memory management.
- Device I/O facilitation.
- Providing system services to applications.
- Enabling multitasking and resource sharing.
- Limitation: By itself, it doesn't provide a user interface.
- Shell:
- Definition: User interface for interacting with the kernel.
- Types:
- Command Line Interface (CLI): e.g., Bash shell (Linux/Unix).
- Graphical User Interface (GUI): e.g., Windows shell (desktop, Start menu, taskbar, File Explorer).
- Daemons/Services:
- Definition: Background processes that provide OS capabilities.
- Examples:
- Task Scheduler (Windows).
- cron (Unix/Linux) - Scheduling programs to run at specific times.
- Software Libraries:
- Purpose: Provide common code for applications and OS components (shell, services) to use.
- Benefit: Promotes code reuse and simplifies development.
- Device Drivers:
- Definition: Software designed to interact with specific hardware devices.
- Role: Bridge the gap between the kernel and diverse hardware.
- OS Inclusion: OSes include drivers for common hardware and mechanisms for installing additional drivers.
- Utilities:
- Definition: Basic applications included with most OSes (text editor, calculator, web browser).
- Classification: Arguably not core OS components, but often bundled for convenience.
- Foundation: Kernel and device drivers directly interact with hardware.
- Libraries: Provide functionality that both OS components (shell, services, utilities) and applications build upon.
- Layered Architecture (from bottom to top):
- Hardware
- Kernel & Device Drivers
- Libraries
- Shell, Services, Utilities, Applications