Skip to Content
πŸŽ‰ Welcome to my notes πŸŽ‰

Basics of Operating System

What is CPU?

The central processing unit (CPU), also called a processor, is the part of a computer that executes instructions of a computer program. It performs basic arithmetic, logic, control, and input/output operations specified by the instructions in the program.

What is Core?

A core is a physical processing unit that reads and executes instructions. Many CPUs have two or more cores, which allows the CPU to execute multiple instructions at the same time. This can greatly increase the performance of the CPU. A single core means only one process at a time.

What is Context Switching?

Context switching is the process of storing and restoring the state (context) of a CPU so that execution can be resumed from the same point at a later time. This allows multiple processes to share a single CPU.

What is Clock Speed?

Clock speed, measured in gigahertz (GHz), determines how quickly a CPU can process data. It is the number of cycles a CPU can execute in a second. A higher clock speed means the CPU can process instructions faster.

What is Cache Memory?

Cache memory is a small amount of high-speed memory located inside or close to the CPU. It stores frequently accessed data and instructions to reduce the time it takes to access them. This can significantly improve the performance of the CPU.

What is an Operating System?

An operating system (OS) is system software that manages computer hardware, software resources, and provides common services for computer programs.

What is Kernel?

The kernel is a computer program at the core of a computer’s operating system that has complete control over everything in the system. It is the β€œportion of the operating system code that is always resident in memory.”

What is a Process?

A process is an instance of a computer program that is being executed. It contains the program code and its current activity. Multiple processes can run concurrently on a computer.

States of a Process:

A process can be in one of the following states:

  1. New: The process is being created.
  2. Ready: The process is waiting to be assigned to a processor.
  3. Running: Instructions are being executed.
  4. Waiting: The process is waiting for some event to occur.
  5. Terminated: The process has finished execution.

What is Thread?

A thread is a unit of execution within a process. A process can have multiple threads, allowing it to perform multiple tasks concurrently. Threads share the same memory space and resources of the process. Every process has at least one thread.

What is Concurrency?

Concurrency is the ability of an operating system to execute multiple processes or threads simultaneously, supported by the CPU’s ability to switch between tasks.

What is Parallelism?

Parallelism is the ability of an operating system to execute multiple processes or threads simultaneously on multiple CPUs or CPU cores. This can significantly improve performance by utilizing multiple processing units.

What is Scheduling?

Scheduling is the process of determining which process or thread to execute next on the CPU. It involves selecting from the ready queue and allocating the CPU to the selected process.

What is Deadlock?

Deadlock is a situation in which two or more processes are unable to proceed because each is waiting for the other to release a resource. This can result in a system freeze or crash.

What is Virtual Memory?

Virtual memory is a memory management technique that provides an β€œidealized abstraction of the storage resources that are actually available on a given machine” which β€œcreates the illusion to users of a very large (main) memory.”

What is Paging?

Paging is a memory management scheme that eliminates the need for contiguous allocation of physical memory. It allows the physical address space of a process to be non-contiguous, which can improve system performance.

What is Segmentation?

Segmentation is a memory management scheme that supports the use of variable-sized partitions, or segments, to store different parts of a program. It can help reduce memory fragmentation and improve memory utilization.

What is Thrashing?

Thrashing is a situation in which a computer’s operating system is spending a lot of time swapping data between memory and disk, rather than executing instructions. This can significantly degrade system performance.

What is Fragmentation?

Fragmentation is the phenomenon in which storage space is used inefficiently, reducing the capacity and performance of a storage system. It can occur in memory, disk, or file systems.

What is RAID?

RAID (Redundant Array of Independent Disks) is a data storage virtualization technology that combines multiple physical disk drives into a single logical unit for the purposes of data redundancy, performance improvement, or both.

What is File System?

A file system is a method for storing and organizing computer files and the data they contain to make it easy to find and access them. It provides a way to store, retrieve, and update data on a storage device.

What is Disk Partitioning?

Disk partitioning is the process of dividing a hard disk drive into multiple logical storage units called partitions. Each partition can be formatted with a file system and used to store data independently.

What are environment variables?

Environment variables are dynamic-named (Key-value pairs) values that affect the processes that are running on a computer. They can affect the behavior of software programs and the operating system itself.

What is PATH variable?

The PATH environment variable is a system-level variable used by operating systems to specify a list of directories where executable programs are located. When you type a command in the terminal or command prompt, the operating system searches the directories listed in the PATH variable to find the corresponding executable file to run.

Key Points About PATH

  1. Purpose:
    It helps the operating system locate programs without needing to provide the full path to the executable each time.

  2. Format:

    • It contains a list of directories, separated by a delimiter:

      • On Windows: ; (semicolon)
      • On Unix/Linux/Mac: : (colon)
    • Example on Linux:

      /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
    • Example on Windows:

      C:\Program Files\Python39\Scripts\;C:\Program Files\Python39\
  3. Usage:

    • If a command like python is typed in the terminal, the system searches through each directory in PATH until it finds the python executable.
    • If it doesn’t find the executable, it throws a β€œcommand not found” or similar error.

How to View and Modify PATH

  1. Viewing PATH
  • On Unix/Linux/Mac: Run echo $PATH in the terminal.
  • On Windows: Run echo %PATH% in the command prompt or Get-ChildItem Env:Path in PowerShell.
  1. Modifying PATH
  • Temporarily:

    • Unix/Linux/Mac: export PATH=$PATH:/new/directory

    • Windows: set PATH=%PATH%;C:\new\directory

    Temporary changes last only for the session.

  • Permanently:

    • Unix/Linux/Mac: Edit the ~/.bashrc, ~/.bash_profile, or ~/.zshrc file and add:

      export PATH=$PATH:/new/directory
    • Windows: Modify it in:
      System Properties > Environment Variables.

Practical Example

If /usr/local/bin is in your PATH and it contains a script named myscript, you can run the script from any directory simply by typing myscript. If /usr/local/bin wasn’t in your PATH, you would have to type the full path like /usr/local/bin/myscript.

What are executable files?

Executable files are files that contain instructions that can be executed by the computer. They can be run directly by the operating system or by another program.

Types of executable files:

  1. Script Executable: Contains a script written in a scripting language that is interpreted by an interpreter program. Examples include shell scripts, Python scripts, and Perl scripts.
  2. Binary Executable: Contains machine code that can be executed directly by the CPU. Examples include compiled C programs, Java programs, and executable files in Windows.

File Permissions

Types of Permissions:

  1. Read (r): Allows the file to be read.
  2. Write (w): Allows the file to be modified.
  3. Execute (x): Allows the file to be executed as a program.

Types of Users:

  1. Owner (u): The user who owns the file.
  2. Group (g): The group associated with the file.
  3. Others (o): All other users on the system.

Permissions in Windows vs. Unix/Linux/Mac:

  • Windows:
    • Right-click on the file, select Properties, and go to the Security tab to view permissions.
    • Permissions are set using checkboxes and dropdowns in the file properties.
    • Permissions are more granular, allowing different permissions for different users.
  • Unix/Linux/Mac:
    • Run ls -l in the terminal to view file permissions in symbolic notation.
    • Run stat file.txt to view file permissions in octal notation.
    • Permissions are set using symbolic notation (e.g., rwxr-xr--) or octal notation (e.g., 754).
    • Permissions are divided into three categories: owner, group, and others.

Notation for File Permissions:

  • Symbolic Notation:

    • r: Read permission
    • w: Write permission
    • x: Execute permission
    • -: No permission
  • Octal Notation:

    • Each permission is represented by a number:
      • r: 4
      • w: 2
      • x: 1
    • The sum of the numbers represents the permission:
      • rwx: 4 + 2 + 1 = 7
      • rw-: 4 + 2 = 6
      • r-x: 4 + 1 = 5
      • r--: 4
  • Example:

    • rwxr-xr-- in symbolic notation is equivalent to 754 in octal notation.

Changing File Permissions:

  • Symbolic Notation:

    • chmod u+x file.txt: Adds execute permission for the owner.
    • chmod go-w file.txt: Removes write permission for the group and others.
    • chmod a=r file.txt: Sets read permission for all users.
  • Octal Notation:

    • chmod 754 file.txt: Sets permissions to rwxr-xr--.
    • chmod 644 file.txt: Sets permissions to rw-r--r--.

Permissions in Git:

  • Git tracks file permissions:

    • Git tracks whether a file is executable or not.
    • It does not track the specific permissions (e.g., read, write, execute).
    • It uses a file mode to determine the type of file:
      • 100644 for non-executable files
      • 100755 for executable files.
      • 120000 for symbolic links.
      • 040000 for directories.
      • 160000 for submodules.
  • Changing file permissions in Git:

    • To change file permissions in Git, you need to use the git update-index command.
    • Example: git update-index --chmod=+x script.sh
  • Viewing file permissions in Git:

    • git diff --summary shows changes in file permissions.

Accessing Process Properties (Node.js)

Command line arguments (process.argv):

An array containing the command line arguments passed to the Node.js process. The first element is the path to Node.js, the second element is the path to the script, and the remaining elements are the command line arguments.

Environment variables (process.env):

An object containing the user environment. It provides information about the current user, system, and process environment variables.

Current Process ID (process.pid):

A number representing the process ID (PID) of the current process. It uniquely identifies the process on the system.

Parent Process ID (process.ppid):

A number representing the parent process ID (PPID) of the current process. It identifies the process that spawned the current process.

Operating System Information (process.platform):

A string representing the operating system platform on which the Node.js process is running. Possible values include 'darwin'(Mac), 'linux', 'win32', etc.

Node.js Version (process.version):

A string representing the version of Node.js running the process. It includes the version number and additional information like the V8 engine version.

Node.js and dependencies (process.versions):

An object containing version information for Node.js and its dependencies. It includes the versions of Node.js, V8 engine, libuv, OpenSSL, zlib, and other dependencies.

Processor Architecture (process.arch):

A string representing the processor architecture for which the Node.js binary was compiled. Possible values include 'x64', 'arm64', 'ia32', etc.

Using Process Methods (Node.js)

Current Working Directory (process.cwd()):

Returns a string representing the current working directory of the Node.js process.

Change Working Directory (process.chdir("/directory")):

Changes the current working directory of the Node.js process to the specified directory.

Memory Usage (process.memoryUsage()):

Returns an object containing memory usage statistics for the Node.js process. It includes rss, heapTotal, heapUsed, external, and arrayBuffers properties.

Uptime (process.uptime()):

Returns the number of seconds the Node.js process has been running since it started.

Exit the Process (process.exit(codeNumber)):

Terminates the Node.js process with the specified exit code. A code of 0 indicates success, while a non-zero code indicates an error.

Killing the Process (process.kill(pid, [signal])):

Sends a signal to the process identified by the process ID (PID). The signal can be specified as a string or number. The default signal is 'SIGTERM'.

Emit warning (process.emitWarning(warning[, options])):

Emits a warning with the specified message. The options object can include properties like type, code, detail, ctor, and info.

Interacting with stdin, stdout, stderr streams:

  • process.stdin: A readable stream representing the standard input (stdin) of the process.
  • process.stdout: A writable stream representing the standard output (stdout) of the process.
  • process.stderr: A writable stream representing the standard error (stderr) of the process.
  • These streams can be used to read input from the user, write output to the console, and log errors.

Next Tick Queue (process.nextTick(callback)):

Adds the specified callback function to the next tick queue. The callback will be executed in the next iteration of the event loop, after the current operation completes. It is often used to defer execution of a function until the current operation is complete.

Registering Event Listener (process.on("event", callback)):

Registers an event listener for the specified event. The callback function will be called when the event occurs. Common events include 'exit', 'uncaughtException', 'warning', etc. The process object emits events related to the Node.js process lifecycle.

Last updated on