- 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
- Published on
KembaraXtra-Computer Science - Programming Without an Operating System
Core Concept: Direct Hardware Access
Core Concept: Direct Hardware Access
- Definition: In a system without an operating system (OS), software (like a game) directly interacts with the hardware. There's no intermediary layer managing resources or providing services.
- Examples: Atari 2600, Nintendo Entertainment System (NES), Sega Genesis
- How they worked:
- Game code resided on a cartridge.
- Inserting the cartridge and turning on the console started the game.
- The console executed the game's code directly, with no OS intervention.
- Only one program (the game) ran at a time.
- Switching games required turning off the system, swapping cartridges, and turning it back on.
- Total Control: The game developer was responsible for:
- Game logic
- Initializing the system (setting up hardware components)
- Controlling video hardware
- Reading controller inputs
- Managing all hardware interactions
- Hardware Specificity: Deep understanding of the target console's hardware was crucial. Different consoles had significantly different hardware designs.
- Challenges:
- Porting to different consoles required rewriting substantial portions of code due to hardware differences.
- Redundant code: Every game cartridge needed code for basic tasks (hardware initialization, etc.), leading to duplicated effort across different developers and games.
- Maximum Performance: Knowing the exact hardware specifications allowed developers to optimize their code to squeeze the maximum possible performance from the system.
- Stable environment: The hardware design was consistent during the manufacturing years, which allowed developers to target their code to that specific hardware
- Porting: It was hard to port the games, so they often had to rewrite a substantial portion of their code.
- Duplicated work: Every game cartridge had to include similar code to accomplish fundamental tasks, such as initializing the hardware.
- Published on
- Published on
KembaraXtra-Computer Science - Compiled vs. Interpreted Languages
I. Core Concepts
I. Core Concepts
- Source Code: Human-readable program instructions written by developers.
- Machine Language: Instructions that a CPU can directly execute.
- Compilation: The process of translating source code into machine code (or an intermediate form).
- Interpretation: The process of reading and executing source code instructions line by line (or bytecode).
- Definition: Languages where source code is converted into machine code before runtime.
- Process:
- Developer writes source code (e.g., in C).
- Compiler translates the source code into an executable file (binary).
- End user runs the executable file directly.
- Example: C (using gcc compiler)
- Advantages:
- Fast execution speed because the code is already in machine language.
- Disadvantages:
- Platform-dependent: Executables are specific to the architecture they were compiled for.
- Requires a compilation step during development.
- Definition: Languages where source code is executed line by line by an interpreter during runtime.
- Process:
- Developer writes source code (e.g., in Python).
- End user runs the source code using an interpreter.
- The interpreter reads and executes the code.
- Example: Python
- Advantages:
- Platform-independent: As long as an interpreter exists for a platform, the code can run.
- No compilation step required for the end user.
- Disadvantages:
- Slower execution speed due to the overhead of interpretation.
- Requires the user to have an appropriate interpreter installed.
- Definition: A combination of compilation and interpretation, using an intermediate language called bytecode.
- Process:
- Developer writes source code.
- Compiler translates source code into bytecode.
- A virtual machine (VM) executes the bytecode.
- Bytecode:
- Similar to machine code but designed for a virtual machine, not a specific CPU.
- Virtual Machine (VM):
- A software platform that provides a virtual CPU and execution environment.
- Abstracts away the details of the underlying hardware and OS.
- Examples:
- Java (Java bytecode runs on the Java Virtual Machine - JVM).
- C# (CIL or Common Intermediate Language runs on the .NET Common Language Runtime - CLR).
- Python (CPython implementation compiles to bytecode internally).
- Advantages:
- Combines platform independence (like interpreted languages) with some of the performance benefits of compiled code.
- Disadvantages:
- Still requires a VM to run.
- Execution is generally slower than native compiled code.
- Published on
KembaraXtra-Computer Science- Object-Oriented Programming (OOP)
I. Paradigms in Programming
myAccount.deposit(25) # Increases myAccount's balance by 25
Key Concept: myAccount is an object (instance of BankAccount). deposit() is a method called on that specific object, modifying its balance field.
I. Paradigms in Programming
- Programming languages support different approaches to programming called paradigms.
- Examples:
- Procedural Programming
- Functional Programming
- Object-Oriented Programming (OOP)
- Languages can support multiple paradigms.
- Definition: A programming paradigm where code and data are grouped together into objects.
- Objects: Logical groupings of data and functionality, designed to model real-world concepts.
- Class-Based Approach: Common in OOP languages.
- Class: A blueprint for an object. Defines the structure and behavior of a type of object.
- Object: An instance of a class. A concrete realization of the class blueprint.
- Methods: Functions defined within a class. They define the actions that an object can perform.
- Fields: Variables declared within a class. They store the data associated with an object.
- Instance Variables (Python): Fields that have different values for each object (instance) of the class. Each object has its own unique value for these fields.
- Class Variables (Python): Fields that have the same value across all objects (instances) of the class. These variables are shared by all objects of the class.
- Class: BankAccount (a blueprint)
- Fields:
- balance (instance variable - each account has a unique balance)
- holder's name (instance variable - each account has a unique name)
- Methods:
- withdraw()
- deposit()
- Fields:
- Objects: Specific bank accounts created from the BankAccount class (instances). These are real accounts with specific names and balances.
- Interaction: We can use the withdraw or deposit methods to modify the balance field of specific bank account objects.
myAccount.deposit(25) # Increases myAccount's balance by 25
Key Concept: myAccount is an object (instance of BankAccount). deposit() is a method called on that specific object, modifying its balance field.