Engineering Basics Day One

Summary: Understanding Operation System Basic, exploring CLI, learning about the Inter-Process communication, Process Management and Threads and concurrency.

Operating System Basics

In simple term Operation System is the layer which seats between the hardware and software it provides the libraries to abstractly interact with the hardware.

Unix/Linux is the most important OS to be aware of as it is core to run most of the servers on which our websites are hosted.

For more details checkout Operating-Systems-Notes

Command-Line Interfaces (CLI)

As a developer CLI is the key to automation it can automated the repetitive task, It makes the interaction with the application fast, Working with multiple application by chain them using pipes.

Pipes

Simply put if there are two application where second application depends on the output of first then instead of saving the output to a file or variable then using them for second application, we can directly pipe the output as a input to the other application using ” | ” this pipe symbol.

A pipeline of three program processes run on a text terminal
ref: Pipeline_(Unix)
# syntax:
$ command1 | command2 | command3
# example:
$ ls | tee filelist.txt
# ls gives the list of files then using pipe we write that output directly to the filelist.txt file.

ref: Linux Tee Command with Examples

CLI client

Most of the OS comes with the some sorts of cli like CMD / PowerShell in Windows, BASH in MacOS and Linux these are powerful cli which can be used to interact with OS without using the GUI, for example like creating and navigating folders and files, running applications directly through CLI.

But these default CLI have some limitation to overcome those limitation there are some famous alternative to them like Zsh and Fish which provide better auto complete, easy to create alias for long command, colorful themes ( Oh My ZSH! for themes in zsh and Oh My FISH! for themes in fish ).

Btw I use fish with toaster theme : )

The Art of Command Line

This guide has a covered some basic and advanced commands. Following are some of the advanced commands to know.

Key bindings to navigate the in the CLI:

  • “Tab” to autocomplete directory name and filename
  • “CTRL+w” to delete the last word
  • “CTRL+u” to delete content form current cursor back to the start of the line
  • “CTRL+e” to move cursor to end of line
  • “CTRL+k” to delete content form current cursor to the end of line
  • “CTRL+l” to clear the screen

Making alias for long commands:

# for bash
$ alias alias_name="command_to_run"
# for saving the alias
$ vim ~/.bashrc
# put the alias command on last line in .bashrc

# for zsh
$ alias alias_name="command_to_run"
# for saving the alias
$ vim ~/.zshrc
# put the alias command on last line in .bashrc

# for fish
$ alias alias_name "command_to_run"
# for saving just add "-s" at end of alias
$ alias alias_name "command_to_run" -s

To locate a file by name in the current directory and its sub directory:

$ find . -iname "*.txt"
# this command will find all the files ending with ".txt" in current directory and its all sub directory.

To know current cpu/disk status:

$ top
or
$ htop

Writing multiple lines of text in command line using:

#syntax:
$ command <<SOMETAG
$ [some_text]
$SOMETAG

#example:
$ cat <<TEXT
$ Hello World 1!
$ Hello World 2!
$ TEXT

#the command will terminate with the TEXT which can be any thing.

Creating a function on command line:

$ function foo() { 
$ echo "Hello World!"
$ }
$ foo()
$ Hello World!

For more commands look at the The Art of Command Line.

Inter-Process Communication

Inter process communication is used to share data between process its important for running applications which needs to communicate with each other. There are several types of inter process communication as follows:

  • Shared Memory: In this method, the multiple processes can communicate with each other simultaneously by accessing or sharing the memory. The multiple processes can exchange the data using read/write to the shared memory because the OS creates a common memory in RAM for sharing. It requires protection by the synchronization of access of all multiple processes in the communication.
  • Message Passing: It is the other method of inter-process communication for synchronization and communication between the processes. It is very slow and easy to implement using system calls compared to the shared memory method. The processes can exchange the data with each other without using any shared resources or variables.

There are other Methods also you can checkout them at Inter-Process-Communication

In context of WordPress to execute a memory intensive job we use Cron Jobs to divide one task into multiple task and to make sure they can communicated with each other they they need to do inter process communication, WordPress achieves this by using a shared database.

To know more about Cron you checkout this article What Is a Cron Job: Understanding Cron Syntax and How to Configure Cron Jobs

Process Management

Understanding Process Management can lead to the increasing efficiency and optimization of your application because you will know how your application is actully executing at the process level.

  • Process and Process Management

A lot of tasks are performed in the Process Management phase such as creation, scheduling, termination, deadlock resolution of process and more. It also includes the Inter Process communication.

For more details checkout Process and Process Management.

  • Process Life Cycle

The process life cycle contains stages as follows:

  • New: The process which is being created.
  • Running: The process which is currently getting executed.
  • Waiting / Blocked: The process which is waiting for some event to occur or blocked until the some event occurs.
  • Ready: The process which is created and waiting for processor.
  • Terminated: The process which has completed its execution.
ref: process-life-cycle

For more details checkout Process Life Cycle.

  • States of Process

This includes all the same states of life cycle and remaining are as follows:

  • Run: The process which has been chosen form ready queue by the CPU for execution.
  • Suspend Ready: Process that was initially in the ready state but was swapped out of main memory(refer to Virtual Memory topic) and placed onto external storage by the scheduler is said to be in suspend ready state. The process will transition back to a ready state whenever the process is again brought onto the main memory.
  • Suspend wait or suspend blocked: Similar to suspend ready but uses the process which was performing I/O operation and lack of main memory caused them to move to secondary memory. When work is finished it may go to suspend ready.
ref: states-of-a-process-in-operating-systems

For more details checkout States of a Process in Operating Systems.

Threads and concurrency

Threads are nothing but a lightweight process. A process can be divided into multiple threads and can be execute together to achieve parallelization. Threads also makes the Inter Process Communication cheaper. Sometimes when a process needs to execute a task which will take a while then the next other independent tasks in process have to wait, by assigning these task to the multiple threads this problem can be solved.

Concurrency is needed to manage the resources so that threads do not get deadlocked, which can be achieved by using Mutex, Semaphore.

learn about Mutex and Semaphore at Mutex vs Semaphore.

For more details checkout Operating System: Threads and Concurrency.

»

Leave a Reply

Your email address will not be published. Required fields are marked *