Muchang Bahng

Computer Systems & Basics


Contents
  1. System Hardware
  2. Program Lifecycle Phases
  3. Navigation Shell Commands
  4. Network & Connectivity Commands
  5. Environment Variables
  6. Git and Github
  7. Python Virtual Environments

System Hardware

[Hide]

Non-Volatile Drive Storage
A drive is basically a computer component used to store data. It may be a static storage device (e.g. a HDD or SSD) or may use removable media (e.g. thumb, disk, CD). All drives store nonvolatile data (also called nonvolatile memory, NVM), meaning that the data is not erased when the power is turned off. Furthermore, each type of drive on a computer are assigned a device/drive letter, a single alphabetic character A through Z. Computers containing a hard drive always have that default hard drive assigned to a C: drive letter, and external drives may be assigned different letters, such as Google Drive being assigned a G: drive letter. You may also notice that when opening the command prompt on windows, the leftmost letter represents which drive you are currently on. Note that the "wmic" is an abbreviation of Windows Management Interface Command. Some commands may require you to use an elevated command prompt, which can be used by opening the cmd file as an administrator.
C:\Users\bahng>
Windows CMD MacOS Terminal Task
wmic LOGICALDISK LIST BRIEF Lists all the drives on your computer. Note that the DeviceID is simply the drive letter, the DriveType has numerical encodings (2: Removable disk, 3: Fixed local disk, 4: Network disk), the FreeSpace and Size are in bytes, and the VolumeName is the name of the disk.
wmic diskdrive get status, model Outputs the model name of the drive along with its status. If the status is OK the health is good, and if it shows 'Pred Fail' your drive may crash soon.
chkdsk c: Checks the file system and provides a summary of issues on the drive. If the bad disk sectors is not 0, get technical assistance.
dfrgui Opens a window that tell you all types of drives on your computer.
<letter>: (d:) Changes the drive you are working in
We demonstrate some of the commands here.
C:\Users\bahng>wmic LOGICALDISK LIST BRIEF
  DeviceID  DriveType  FreeSpace     ProviderName  Size           VolumeName
  C:        3          838628864000                1003327844352  OS
  G:        3          71506178048                 107374182400   Google Drive

C:\Users\bahng>wmic diskdrive get status, model
  Model                      Status
  PM9A1 NVMe Samsung 1024GB  OK
Volatile, Short-Term Storage
Information travels from drives and other stores to the CPU, but the physical distance that the bits must travel across the motherboard also puts an upper limit on the retrieval speed (i.e. the speed of electromagnetic waves), especially if the distance must be covered thousands or millions of times back and forth. This limit is known as latency. This is why computers have a hierarchy of stores reserved for information that is accessed more frequently, some closer to the CPU and others even within the CPU itself!
To check the status of your RAM on your computer, refer to the following commands.
Windows CMD MacOS Terminal Task
wmic MEMORYCHIP (get BankLabel, DeviceLocator, MemoryType, TypeDetail, Capacity, Speed) Outputs relevant information about the RAM
wmic memoryship list full Outputs a list of all specifications of each memory stick.
systeminfo | findstr /C:"Total Physical Memory" Outputs the total RAM memory of your computer.
systeminfo |find "Available Physical Memory" Outputs the available RAM memory of your computer.
For my computer, the outputs are as such. It shows two memory sticks each with 8GB of memory, a memory type of 0 which is a DDR4 (type 24 means DDR3), speeds of 3200 mhz, and TypeDetail of 128 which means the RAM is synchronous (SDRAM).
C:\Users\bahng>wmic MEMORYCHIP get BankLabel, DeviceLocator, MemoryType, TypeDetail, Capacity, Speed
BankLabel  Capacity    DeviceLocator  MemoryType  Speed  TypeDetail
            8589934592  DIMM A         0           3200   128
            8589934592  DIMM B         0           3200   128
The status of your CPU can be checked with the following commands
Windows CMD MacOS Terminal Task
wmic cpu list full Outputs a list of all specifications of the CPU
wmic cpu (get caption, deviceid, name, numberofcores, maxclockspeed, status) Outputs relevant information about the CPU

Program Lifecycle Phases

[Hide]

First, we review some definitions. More on program lifecycle phases here.
More on Executables
More specifically, an executable is a file that contains a list of instructions and data to cause a computer's CPU to perform indicated tasks, as opposed to the data files, which are fundamentally strings of data that must be interpreted (parsed) by a program to be meaningful. Executables usually have extension names .exe or .bat, and they can generally be run (invoked) in two ways: Some common examples of executables are: Some commands for running python scripts.
Windows CMD MacOS Terminal Task
python (py) -V Checks the version of python.exe (py.exe)
python.exe (py.exe) <script>.py Runs a python script with the executable (assuming python and py are in PATH)
Statically Typed vs Dynamically Typed Languages
Type-checking is the process of checking and verifying the type of a construct (constant, variable, array, list, object) and its usage context. It helps in minimizing the possibility of type errors in the program, and type checking may occur either at compile-time (static checking) or at run-time (dynamic checking).

Navigation Shell Commands

[Hide]

I work in both the Mac and Windows operating systems, which requires me to know two sets of commands when accessing the command line.

Windows CMD MacOS Terminal Task
dir ls Lists files and folders in current directory
dir /ad ls -a Lists ALL files and folders in current directory
cd pwd Full path of current folder/directory
cd < path to directory > cd < path to directory > Change folder/directory
cd .. cd .. One directory up in directory tree
cd \ cd / Move to root directory
mkdir (rmdir) newFolder mkdir (rmdir) newFolder Create (delete) new directory in current directory
echo < text > > filename(.txt) cat > fileName(.txt) Create new file with <text> written inside. If file type is not specified, default is .txt.
cls clear Clear the terminal screen
type cat Concatenate and print a file
ren oldName newName mv oldName newName Rename a file or directory
robocopy (move) FileOrFolderPath <path to destination directory> cp -r (mv) myFolder <path to destination> Copy (move) a directory to destination directory
wmic LOGICALDISK LIST BRIEF Lists all the drives on your computer
<letter>: (d:) Changes the drive you are working in
nano fileName Open file in nano, which would encode the binary data differently. This is especially fun since you can open non-text files and output them in a different encoding, even if the encoding is complete nonsense. Deleting a few lines in a, say mp3 file opened in nano can corrput the file, leading to distortions in the original mp3 audio.

Network & Connectivity Commands

[Hide]

When your device connects to a router in a LAN, it has a local IP address (usually starting with 172.30. or 192.168.). Packets of data leaves your comptuer address, from a port (out of the 216=65,536 ports), to the router local IP address, known as the default gateway. This router then connects to the internet, which now has another address (which can be Googled) and forwards the packets through there. Device_Ports_to_Router_to_Domain.jpg
In addition to the IP address, the MAC (Media Access Control)/physical address is used to uniquely define the physical address of a computer, while the IP address identifies the connection of the device on the network. The MAC address of a computer cannot be changed with time and environment, while the IP address modifies with time and environment. More info on the difference between them is found here.
Windows CMD MacOS Terminal Task
ping (-t) www.google.com ping www.google.com Checks the ping time to www.google.com by sending small multiple-byte packets of data. Measures in milliseconds. The "-t" pings the address forever until manually stopped.
ping 192.168.0.1 ping 192.168.0.1 Checks the ping time to an IP address in your local network by sending small multiple-byte packets of data. Make sure that they are both on the same network, with the same subnet mask, and firewalls are turned off.
ipconfig (/all) Shows your IP address and related information about your device within the network.
ipconfig /displaydns Displays the DNS cache of your system
ipconfig /flushdns Flushes the DNS cache of your system.
tracert www.google.com Traces the route it takes for a packet to reach a destination and shows information about each hop along that route. If you’re having issues connecting to a website, tracert can show you where the problem is occurring.
netstat -an nettop Displays a list of all open network connections on the computer, along with the port they're using and the foreign IP address they're connected to.
nslookup www.google.com nslookup www.google.com Find the IP address associated with a domain
ipconfig /release Forces your network adapter (internal computer hardware that connects to network) to drop its assigned IP address. Note that this disconnects your wifi.
ipconfig /renew Renews the network adapter's IP address
arp -a arp -a Shows all the devices connected to your network.

Environment Variables

[Hide]

When in an environment (whether it'd be a base or a virtual one), the environment has certain characteristics that are stored in what we call the environment variables. A list of then can be outputted with the commands below.
Windows CMD MacOS Terminal Task
set Lists all the environment variables
echo %<VARIABLE>% Returns the value of the environment variable
A few of the first variables that are outputted on my system is shown here.
C:\Users\bahng>set
ALLUSERSPROFILE=C:\ProgramData
APPDATA=C:\Users\bahng\AppData\Roaming
CommonProgramFiles=C:\Program Files\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
CommonProgramW6432=C:\Program Files\Common Files
They are all in the form
   NAME=VALUE
where the NAMEs are conventionally written in uppercase (&VARIABLE for MacOS and %VARIABLE% for Windows), and the VALUEs are strings. The point of these variables is to communicated to programs how the machine is set up and sometimes to control the behavior of programs (e.g. where the home directory is, what user is logged in, etc.). One notable environment variable is the PATH environment variable, which is basically a set of directories where executable programs are located. More specifically, it specifies the directories in which executable programs are located on the machine that can be started without knowing and typing the whole path to the file on the command line. Note that the shell does not check subdirectories in PATH, only that directory itself. We can also modify which file extensions (other than the common .exe, .bat, etc.) is supported by editing %PATHEXT%. One can check this this set of directories with the following commands:

Windows CMD MacOS Terminal Task
echo %PATH% echo $PATH Specifies the directories in which executables are located on the machine
echo %PATHEXT% echo $PATHEXT Specifies the extensions that are supported by PATH and can therefore be called directly in cmd/terminal.

For example, calling echo %PATHEXT% on my Windows laptop returns the following supported executable extensions:

C:\Users\bahng>echo %PATHEXT%
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC

On MacOS, PATH usually holds all bin and sbin directories relevant for the current user. On Windows, it contains at least the C:\Windows and C:\Windows\system32 directories. This is why you can run calc.exe or notepad.exe from the command line (you actually don't need to include the .exe, just calc will do), but not chrome.exe since it is located in C:\Program Files\Google\Chrome\Application.
Calling the PATH environment variable on my computer returns

C:\Users\bahng>echo %PATH%
C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Users\bahng\anaconda3;C:\Users\bahng\anaconda3\Library\mingw-w64\bin;C:\Users\bahng\anaconda3\Library\usr\bin;C:\Users\bahng\anaconda3\Library\bin;C:\Users\bahng\anaconda3\Scripts;C:\Users\bahng\AppData\Local\Microsoft\WindowsApps;C:\Users\bahng\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\bahng\AppData\Local\Programs\Python\Python39;C:\Users\bahng\AppData\Local\Programs\Python\Python39\Scripts
and since I have py.exe installed in C:\Windows\system32 (which is in PATH), I can call it immediately in cmd. However, the python.exe is located in C:\Users\bahng\AppData\Local\Programs\Python\Python39, which is currently not on the PATH variable and therefore cannot be called without specifying the entire path of python.exe. For convenience, we should modify the PATH variable to include the directory that python.exe is located in. Adding a directory to your PATH expands the # of directories that are searched when, from any directory, you enter a command in the shell. Unfortunately, modifying the PATH with the shell can be very dangerous, but there is an easy way to do it using a GUI explained very well through this link. All environment variables can be changed as explained in the link using the GUI (in the control panel).

Git and Github

[Hide]

Here is the general list of commands that you will use when working with git from the command line.
  1. When you are starting a new git repository, whether its creating a new repo from scratch or from an existing project, go into the directory and run git init. This can be done in two ways: Go into the directory through terminal/cmd (using the cd) command and run git init. Go into the directory through the graphical user interface, right-click, and open the git bash. Then run git init.
  2. Do git add (git add filename.ext for a specific file or git add . for everything in the directory) to add all the relevant files in the staging area.
  3. Do git status to check which files have been added and which changes are being tracked.
  4. You should create a .gitignore file right away to indicate all of the files you don't want to track. You should add this too with git add .gitignore.
  5. Do git commit -m "Your comment" to commit your changes (i.e. have git take a "snapshot" of your work in this timeline).
  6. Connect your local git repository to github. Go to Github, log in to your account, and click the new repository button on the top-right.Then follow the instructions on the screen, which should tell you to type in a command like this:
        >>> git remote add origin https://github.com/username/new_repo
        
  7. Push your changes into your remote repo by doing git push origin master (or sometimes, git push -u origin master). Sometimes, the branch may be called main, so you would do git push origin main.
To see the log of your changes in git, do git log or git log --oneline, which should show you a list of all the commits that have occured. Press enter to go down, and press q to exit the log. Now let us talk about working with branches.
  1. If we want to create a new branch, we simply type git branch myBranch
  2. We can see all of our branches with git branch -a. The branch with the asterick next to it is the one you are currently on.
  3. To move branches, do git checkout myBranch. Within this new branch, we can edit our files, do git add, git commit, and everything else completely separately from the master branch.
  4. To merge the myBranch branch to the master branch, first go into the master branch by doing git checkout master and then doing git merge myBranch. If there are no conflicts, the branches will merge successfully, and you can work with your changes in myBranch in the master branch. If there are conflicts, you should go into the files, edit the code accordingly, and then merge it again to the master branch.
  5. To delete the branch myBranch, first go into the master branch and do git branch -d myBranch. This will give you a warning whether you really want to delete it, and you can confirm it by doing git branch -D myBranch.

Python Virtual Environments & FileSystem

[Hide]

Remember that an environment really contains three things: Note that the scripts you have written as a part of your program is not within the virtual environment folder; it only contains the executable scripts and packages. Usually, the scripts would be contained one step above in the file tree of the virtual environment all within a bigger project folder.

We must also distinguish two types of environments. Thankfully, there are multiple package managers that allows us to organize our environments. In addition to the information below, this link is also a great tutorial. It is conventional to use venv somewhere in the environment name, and it is easily available in ignore files like .gitignore.

Some common virtual environment management programs. A final reminder that each instance of python, pip, and all the other packages installed is only updated within its environment! Updating, pip (for instance) in the base environment will not update pip in any one of the virtual environments.