TECHNOLOGY 

Published on
KembaraXtra-Computer Science -Virtual Memory
Core Concept
  • Virtual Memory: An abstraction that provides each process with its own large, private address space, isolated from other processes.
Why Use Virtual Memory?
  • 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.
Virtual vs. Physical Addresses
  • 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.
Virtual Address Space
  • 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.
Kernel Address Space
  • 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.
Address Space Division (32-bit Systems)
  • 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
Paging
  • 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.
64-bit Systems
  • 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.



Picture
Published on
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.
2. Thread Characteristics
  • 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.
3. Threads in Windows vs. Linux
  • 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.
4. Linux Thread and Process Identifiers
  • 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).
5. Parallel Execution
  • 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.
6. Physical vs. Logical Cores
  • 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.
7. The Role of the Operating System Scheduler
  • 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.



Picture
Published on
KembaraXtra-Case Law-Processes
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.
Key Components of a Process:
  • 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.
Process Characteristics:
  • 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.
Process Relationships (Parent-Child):
  • 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.
Tools for Viewing Processes:
  • Linux:
    • pstree utility: Displays the process tree in a textual format.
      • Child threads shown with curly braces.
  • 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.
2. Thread Characteristics
  • 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.
3. Threads in Windows vs. Linux
  • 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.
4. Linux Thread and Process Identifiers
  • 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).
5. Parallel Execution
  • 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.
6. Physical vs. Logical Cores
  • 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.
7. The Role of the Operating System Scheduler
  • 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.
Picture
Published on
KembaraXtra- Computer Science-Kernel Mode and User Mode
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.
II. Two Main Privilege Levels
  • 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.
III. Why Use Kernel Mode?
  • 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.
IV. Kernel Mode Components in Windows
  • 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.
Picture
Published on

Operating System Families: Study Guide

I. Overview

Two Dominant Families:

- Unix-like operating systems
- Microsoft Windows

II. Unix-like Operating Systems

Characteristics: Behave like the original Unix OS.

Examples: Linux, macOS, iOS, Android.

History of Unix:
- Developed at Bell Labs in the 1960s.
- Originally for PDP-7 minicomputer, later ported.
- Rewritten in C, enabling portability.
- Supports multiple users, multitasking, and hierarchical directory structure.
- Strong command line shell with standard tools.

Linux:
- Kernel developed by Linus Torvalds.
- Unix-like but doesn't contain Unix source code.
- Open source.
- Bundled with other software to form a Linux distribution.
- Often includes components from the GNU project.

GNU:
- Recursive acronym for "GNU's Not Unix."
- A project to create a free Unix-like OS.
- Complements Linux: Linux provides the kernel, GNU provides other tools (shell, libraries).
- "Linux" often refers to the combination of the Linux kernel and GNU software.

Usage of Linux:
- Common on servers, embedded systems, and with software developers.
- Android OS is based on the Linux kernel.
- Raspberry Pi OS is a Linux distribution.

III. Microsoft Windows

Dominance: Dominant OS on personal computers (desktops, laptops). Strong presence on servers (Windows Server).

Unique History: Doesn't originate from Unix. Early versions based on MS-DOS.

OS/2:
- Joint project with IBM to succeed MS-DOS.
- Microsoft and IBM diverged; IBM took over OS/2.
- Microsoft shifted focus to Windows NT.

Windows NT:
- New kernel, unlike DOS-based Windows.
- Designed to be portable, compatible, support multiple users, and provide security/reliability.
- Led by Dave Cutler (formerly DEC).
- Elements of NT kernel based on DEC's VMS OS.

Evolution of Windows:
- Early Windows NT positioned for business use, coexisting with consumer Windows.
- Shared UI and programming interface.
- Windows XP (2001) brought the NT kernel to consumer Windows.
- All versions of desktop and server Windows since XP are built on the NT kernel.

IV. Common Operating Systems and Their Families

OS or Device Family Notes
Android Unix-like Uses Linux kernel but has a different user experience and programming interfaces.
iOS Unix-like Based on Darwin OS; user experience and programming interface differ from typical Unix.
macOS Unix-like Based on Darwin OS.
PlayStation 4 Unix-like Based on FreeBSD kernel.
Raspberry Pi OS Unix-like Linux distribution.
Ubuntu Unix-like Linux distribution.
Windows 10 Windows Uses the Windows NT kernel.
Xbox One Windows OS uses the Windows NT kernel.

V. Key Terms

Kernel: The core of an operating system.

Distribution: A specific release of an OS, usually referring to Linux.

Open Source: Software with freely available source code.

Command Line Interface (CLI): A text-based interface for interacting with an OS.

Published on
KembaraXtra-Computer Science - Operating Systems Overview
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.
II. Key Components of an Operating System
  • Two Major Categories:
    1. Kernel: The core of the OS.
    2. 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.
III. Component Relationships
  • 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):
    1. Hardware
    2. Kernel & Device Drivers
    3. Libraries
    4. Shell, Services, Utilities, Applications
Picture
Published on
KembaraXtra-Computer Science - Programming Without an Operating System
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.
Example: Early Video Game Consoles
  • 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.
Programmer's Responsibilities (Without an OS)
  • 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.
Advantages (of this approach)
  • 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
Disadvantages (of this approach)
  • 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.



Picture
Published on

Study Guide: Factorial Calculation in C

1. C Implementation of Factorial

Code:

// Calculate the factorial of n.
int factorial(int n)
{
  int result = n;

  while(--n > 0)
  {
    result = result * n;
  }

  return result;
}
  

Functionality:

- Takes an integer n as input.

- Calculates the factorial of n.

- Uses a while loop and a local variable result to compute the factorial.

- Returns the calculated factorial value.

Key Aspects:

- Uses a while loop to iterate.

- The --n operator decrements n before its value is compared to 0.

- More readable compared to assembly language.

- Portable and can be compiled for different processor types.

2. Exercise: Running the C Code

Goal: Understand the step-by-step execution of the C factorial function.

Method:

- Trace the code's execution with a specific input value (e.g., n = 4).

- Keep track of variable values (n and result) before and after each step.

Expected Outcome: Verify that the function returns the correct factorial (e.g., 24 for n = 4).

3. Compilation and Disassembly

Process:

- C code is compiled into machine code.

- Machine code can be disassembled into assembly language.

Implication: Compiled programs can be analyzed even without the original source code.

ARM Assembly Output:

Address     Assembly
0001051c    sub     r3, r0, #1
00010520    cmp     r3, #0
00010524    bxle    lr
00010528    mul     r0, r3, r0
0001052c    subs    r3, r3, #1
00010530    bne     00010528
00010534    bx      lr
  

Compiled C code outputs similar logic to the ARM assembly above.

Note: A compiler translates high-level language to machine code.

4. Cross-Platform Compilation

Advantage of High-Level Languages: Code portability.

Concept: The same C code can be compiled for different processors (e.g., ARM, x86) and operating systems (e.g., Linux, Windows), as long as it doesn’t depend on OS-specific features.

x86 Assembly Output (Windows):

Address     Assembly
00406c35    mov     ecx,dword ptr [esp+4]
00406c39    mov     eax,ecx
00406c3b    jmp     00406c40
00406c3d    imul    eax,ecx
00406c40    dec     ecx
00406c41    test    ecx,ecx
00406c43    jg      00406c3d
00406c45    ret
  

5. Key Takeaways

- High-level languages simplify development compared to assembly.

- Compilers handle the translation to machine code.

- High-level languages enable portability across processors and operating systems.

- Compiled code can be disassembled to study its underlying structure.

Picture
Published on
KembaraXtra-Computer Science - Compiled vs. Interpreted Languages
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).
II. Compiled Languages
  • Definition: Languages where source code is converted into machine code before runtime.
  • Process:
    1. Developer writes source code (e.g., in C).
    2. Compiler translates the source code into an executable file (binary).
    3. 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.
III. Interpreted Languages
  • Definition: Languages where source code is executed line by line by an interpreter during runtime.
  • Process:
    1. Developer writes source code (e.g., in Python).
    2. End user runs the source code using an interpreter.
    3. 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.
IV. Hybrid Approach: Bytecode
  • Definition: A combination of compilation and interpretation, using an intermediate language called bytecode.
  • Process:
    1. Developer writes source code.
    2. Compiler translates source code into bytecode.
    3. 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.
Picture
Published on
KembaraXtra-Computer Science- Object-Oriented Programming (OOP)
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.
II. Object-Oriented Programming (OOP)
  • 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.
III. Classes and Objects
  • 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.
IV. Example: Bank Account
  • 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()
  • 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.
V. Python Example
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.


Picture