Week 1

Week 1 #

Processes #

What happens when you run a program.

  • Has Input and Output ends:

    • stdin: Standard Input - eg. Keyboard input goes through stdin
    • stdout: Standard Output - eg. Terminal output (printing to screen)

    eg.

    Navinns-MacBook-Pro:~ home$ sort
    # OS connects keyboard to stdin
    Watermelon
    Apple
    Strawberry
    Mango
    ^D # 'exit' keybind, OS connects screen to stdout
    Apple
    Mango
    Strawberry
    Watermelon
    

Pipelining and Redirection #

Routing outputs of one program to the input of another, and outputting to a file instead of the screen.

eg.

Navinns-MacBook-Pro:Desktop home$ cat myfile # screen is connected to stdout
# starting text
This
this
is
is
a
file
haha
haha
Navinns-MacBook-Pro:Desktop home$ cat myfile | sort | uniq > myNewFile && cat myNewFile
# myfile -> stdin of cat => stdout -> stdin of sort => stdout -> stdin of uniq => stdout -> myNewFile
This
a
file
haha
is
this

Scripting #

Combining shell commands, instead of being redundant.

eg.

  • Redundant commands:

    Navinns-MacBook-Pro:Desktop home$ cat mywords | sort | uniq > mywords-unique
    Navinns-MacBook-Pro:Desktop home$ cat yourwords | sort | uniq > yourwords -unique
    Navinns-MacBook-Pro:Desktop home$ cat badwords | sort | uniq > badwords -unique
    
  • Simple For-Loop script:

    for w in mywords yourwords badwords; do
      cat $w | sort | uniq > $w-unique
    done
    

Devices and Services #

Presented as files in Unix.

  • Unix kernal creates file names and emulates file operations, known as ‘Special Files’

eg.

/dev/sda # Hard disk as a special file, restricted
/dev/zero # Endless stream of 0's as bytes

# To wipe a hard disk clean:
Navinns-MacBook-Pro:~ home$ dd bs=4K if=/dev/zero of=/dev/sda

Terminology #

  • Kernel: Decides which processes may run, and what it can access

  • OS Processes: More services, features, and background monitoring not in the kernal

  • Shell: Command-line user interface

  • User Processes: Your processes

The Unix Philosophy #

  • Consisting of small programs, that do one thing really well

  • Scripting + Pipelining versus rewriting bigger programs for complex tasks

  • Recursive file-like structure

  • Short and Simple program names - eg. cp: copies files