[Unit 1] Unix Operating System (OS)

binh12A3
5 min readSep 16, 2023

Is it possible to program multi-thread if the system has only 1 core ? 🧐

1. Overview

  • In Unix, everything is a file, process is a file (/proc), device (mouse, keyboard, disk, …) is a file (/dev), a folder is also a file which contains a list of items
  • Linux, Android is not a real time OS, but a multimedia OS therefore it has a high load capacity. For example, in Microsoft Word, sometime when we type 10 characters but nothing is displayed because of lagging. After some delay, all 10 characters are fully displayed.
  • The 3 principles of Unix are inherited to this day : File, User, Multi thread

2. File System directories

  • / : root directory (like My Computer in Windows)
  • /bin : contains all Linux programs/commands : ls, grep,zgrep, cat, ps, kill, …

We could code and build a program name MyCat and copy the binary file into /bin folder. Then we could execute MyCat like the cat command line.

  • /boot: contains the data to boot the OS
  • /etc, /lib: contains the scripts which will be executed when booting

We could insert our command into this script, so it’ll be executed automatically when booting

  • /var: contains data which is being stored/loaded on RAM
  • /dev : contains the files represent for the hardwares e.g: disk, cdrom, sda1, sda2 (drives), mem, core, stdin, stdout, stderr, tty1, tty2 (serial ports)…
  • /proc: contains the processes’s files

We could count the running process by counting the files in /proc

We could use the command cat /proc/xxx/cmdline to find the program’s name of the pid xxx

3. Types of file

There are 3 main types of file in Linux

  • Regular file : is a file which is stored on the drive
  • Directory : is a file which contains a list of items
btnguyen@DESKTOP-UAIA29B:/mnt/h/DEVELOPER/Linux$ ls -ltr
total 0
drwxrwxrwx 1 btnguyen btnguyen 4096 Sep 3 22:17 prog
drwxrwxrwx 1 btnguyen btnguyen 4096 Sep 4 00:29 script
btnguyen@DESKTOP-UAIA29B:/mnt/h/DEVELOPER/Linux$ vi prog
" ============================================================================
" Netrw Directory Listing (netrw v156)
" /mnt/h/DEVELOPER/Linux/prog
" Sorted by name
" Sort sequence: [\/]$,\<core\%(\.\d\+\)\=\>,\.h$,\.c$,\.cpp$,\~\=\*$,*,\.o$,\.obj$,\.info$,\.swp$,\.bak$,\~$
" Quick Help: <F1>:help -:go up dir D:delete R:rename s:sort-by x:special
" ==============================================================================
../ ./
mylib/
test*
test.cpp*
  • Other files : is a file which is NOT stored on the drive, it will be created when we boot the system and will be removed when we turn off the system (i.e : /proc, /dev)

4. Architecture

4.1 Block architecture

PM : Khốp lập lịch

4.2 Layer architecture

4.3 Types of kernel

  • In Monolithic, all the blocks (yellow blocks) are connected and built into a unified block (green block). So it’s faster but if one of those yellow block is crashed, then it could make the whole green block crash.
  • In Microkernel, every block is independent. It’s slower but saftier.
  • Monolithic kernel : Unix, Linux, Android, Windows, …
  • Microkernel kernel : iOS, …

5. Program vs Process

  • Program : is a binary file which is built from the source code. It consumes space of the hard drive.
  • Process : is a program which is loaded into the system. It consumes the system’s resources like RAM, CPU,…

6. Multitasking of the OS

Is it possible to be multithreaded programming if the system has only 1 core?

  • The answer is yes, the multithreaded programming depends on the process management (PM), it does not depend on the number of core.
  • For example, the intel Ceron/Pentium chip which has only 1 core but we could listen music and web suffering at the same time without interprubting
  • The idea is human processing speed is many times slower than computers
  • The chip switches between the programs continuously. It is too fast to be regconized by the people, thus we have a feeling that they run in parrallel but in fact they are running in serial, one by one.

7. Shell

  • Shell is an interpreted language, not a compiled language which could run directly without compiling
  • Shell is a program written in C, it is used to interpret other source code
  • There are many interpreted languages like python, java, php,… Linux selects the interpreter program to interpret a particular code based on the file header
  • Instead of typing 10 command lines, then it’s better to create a shell script to do that
  • Syntax
#!/bin/sh

echo "Hello World"

#! to indicate this is a shell script

/bin/sh is the path to the interpreter.

We could use either /bin/sh or /bin/bash. Itself is a compiled/built C program which translates each texts/commands in the shell script into the machine language.

  • Example
btnguyenÉDESKTOP-UAIA29B:/mnt/h/DEVELOPER/Linux/script$ cat test.sh
#!/bin/sh

echo "Hello World Iam $1"
btnguyen@DESKTOP-UAIA29B:/mnt/h/DEVELOPER/Linux/script$ chmod +x test.sh
btnguyen@DESKTOP-UAIA29B:/mnt/h/DEVELOPER/Linux/script$ ls -ltr
total 0
-rwxrwxrwx 1 btnguyen btnguyen 37 Sep 4 00:29 test.sh
btnguyen@DESKTOP-UAIA29B:/mnt/h/DEVELOPER/Linux/script$ ./test.sh
Hello World Iam
btnguyen@DESKTOP-UAIA29B:/mnt/h/DEVELOPER/Linux/script$ ./test.sh Binh
Hello World Iam Binh
btnguyen@DESKTOP-UAIA29B:/mnt/h/DEVELOPER/Linux/script$

8. Enviroment variables

  • We could use the command printenv to list all env variables, including our defined variable (e.g : LINUX_HOME)
btnguyen@DESKTOP-UAIA29B:/mnt/h/DEVELOPER/Linux$ printenv
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:
HOSTTYPE=x86_64
LESSCLOSE=/usr/bin/lesspipe %s %s
WT_SESSION=ea86ad55-b869-4018-b99e-f690552c38c7
LANG=C.UTF-8
OLDPWD=/mnt/c/Users/ADMIN
WSL_DISTRO_NAME=Ubuntu-18.04
USER=btnguyen
LINUX_HOME=/mnt/h/DEVELOPER/Linux
PWD=/mnt/h/DEVELOPER/Linux
HOME=/home/btnguyen
NAME=DESKTOP-UAIA29B
XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop
SHELL=/bin/bash
TERM=xterm-256color
SHLVL=1
LOGNAME=btnguyen
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Program Files (x86)/Common Files/Oracle/Java/javapath:/mnt/c/Users/ADMIN/Downloads/WINDOWS.X64_193000_db_home/bin:/mnt/c/Program Files/Common Files/Oracle/Java/javapath:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files/Git/cmd:/mnt/c/Users/ADMIN/.dnx/bin:/mnt/c/Program Files/Microsoft DNX/Dnvm/:/mnt/c/Program Files/Microsoft SQL Server/130/Tools/Binn/:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/Program Files/Microsoft/Web Platform Installer/:/mnt/c/Users/ADMIN/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/ADMIN/AppData/Local/Programs/Microsoft VS Code/bin:/mnt/c/BINH_DATA/Developer/flutter/bin:/mnt/c/adb:/mnt/c/Program Files/dotnet/:/mnt/c/MinGW/bin:/mnt/c/Program Files/nodejs/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/Users/ADMIN/AppData/Local/Programs/Python/Python310/Scripts/:/mnt/c/Users/ADMIN/AppData/Local/Programs/Python/Python310/:/mnt/c/Users/ADMIN/.dotnet/tools:/mnt/c/Program Files/heroku/bin:/mnt/c/Users/ADMIN/AppData/Roaming/npm:/snap/bin
WSLENV=WT_SESSION::WT_PROFILE_ID
LESSOPEN=| /usr/bin/lesspipe %s
WT_PROFILE_ID={61c54bbd-c2c6-5271-96e7-009a87ff44bf}
_=/usr/bin/printenv
btnguyen@DESKTOP-UAIA29B:/mnt/h/DEVELOPER/Linux$
  • We could use the command echo $XXX to print a single variable XXX
btnguyen@DESKTOP-UAIA29B:/mnt/h/DEVELOPER/Linux$ echo $LINUX_HOME
/mnt/h/DEVELOPER/Linux
btnguyen@DESKTOP-UAIA29B:/mnt/h/DEVELOPER/Linux$
  • We could use char* getenv(const char* name) to read the env variable
#include <iostream>
#include <stdlib.h>

int main () {
printf("HOME : %s\n", getenv("HOME"));
printf("ROOT : %s\n", getenv("ROOT"));
printf("LOGNAME : %s\n", getenv("LOGNAME"));
printf("LINUX_HOME : %s\n", getenv("LINUX_HOME"));

return(0);
}
btnguyen@DESKTOP-UAIA29B:/mnt/h/DEVELOPER/Linux/prog$ gcc test.cpp -o test
btnguyen@DESKTOP-UAIA29B:/mnt/h/DEVELOPER/Linux/prog$ ./test
HOME : /home/btnguyen
ROOT : (null)
LOGNAME : btnguyen
LINUX_HOME : /mnt/h/DEVELOPER/Linux
btnguyen@DESKTOP-UAIA29B:/mnt/h/DEVELOPER/Linux/prog$

--

--