BYU logo Computer Science
CS 465 Introduction to Security and Privacy

Operating Systems Background

Introductions

  • introductions of two students

Questions on the readings

The readings today are from Operating Systems: Three Easy Pieces, an open source textbook. You shoud have read these sections:

Key concepts

  • Processes

    • processes enable the OS to provide the illusion that the system has an unlimited number of CPUs available, enabling many programs to run “simultaneously”
    • a process is a running program: state (registers, stack), memory (address space), I/O
    • the OS provides an API so that programs can create, destroy, wait for, and control processes
    • processes have a stack and a heap
    • a process can be in a variety of states, e.g. ready, running, blocked (e.g. waiting for I/O)
    • the OS keeps track of a process using a data structure
  • CPU scheduling

    • scheduling algorithm: the part of the OS that decides which process runs and for how long
    • can measure effectiveness of a scheduling algorithm with metrics, e.g. turnaround time (time completed - time arrived) or response time (time first run - time arrived)
    • see the book diagrams for turnaround time for different scheduling algorithms
    • First In, First Out (FIFO) — can lead to long turnaround times if there are long-running processes
    • Shortest Job First (SJF)
    • Shortest Time-to-Completion First (STCF)
    • Round Robin — better for response time
    • modern schedulers are much more complex — see multi-level feedback queue in next chapter
  • Address spaces

    • the pictures are particularly heplful for this chapter
    • early systems let a process use all physical memory not in use by the OS
    • modern systems use multi-programming, allowing multiple proceses to be in memory at a time
    • each process gets its own virtual address space, with the OS mapping the virtual numbers to physical space
    • the OS swaps memory in and out of physical space based on what the running process needs
    • the OS uses memory protection so that processes cannot access memory from another process

Class Exercises

Clone the OS homework repo.

Processes

Use cd cpu-intro to get to the appropriate directory.

This is meant to show you VERY basic CPU scheduling and the how the OS wants balance I/O and CPU.

Run the process simulator from the end of the Processes chapter. The -l flag provides a set of jobs of the form X:Y, where X is the number of instructions and Y is the percent chance that an instruction is CPU not IO. The -c flag computes how the CPU would alternate among proceses, and -p prints statistics.

Terminal window
# two jobs, 5 instructions each, 100% chance each instruction is CPU computation
python process-run.py -l 5:100,5:100

Instructions about the CPU scheduling are at the end. Figure out what it will do, then run:

python process-run.py -l 5:100,5:100 -c -p

Repeat with:

Terminal window
# two jobs, first has 4 CPU instructions, second has 1 IO instruction
python process-run.py -l 4:100,1:0
# switches the order of the two processes above
python process-run.py -l 1:0,4:100
# tells the CPU to swap in another ready process if one is blocked on I/O
python process-run.py -l 1:0,4:100 -S SWITCH_ON_IO
# four processes, first uses I/O, next three use CPU
# tells the CPU that if a job finishes its I/O, it is at the back of the ready queue
python process-run.py -l 3:0,5:100,5:100,5:100 -S SWITCH_ON_IO -I IO_RUN_LATER
# same as above, except rtells the CPU that if a job finishes its I/O, it is at the front of the run queue
python process-run.py -l 3:0,5:100,5:100,5:100 -S SWITCH_ON_IO -I IO_RUN_IMMEDIATE

CPU Scheduling

Use cd cpu-sched to get to the appropriate directory.

Terminal window
# three jobs, length 300, 200, 100, FIFO scheduling
python scheduler.py -l 300,200,100 -p FIFO -c
# three jobs, length 300, 200, 100, SJF scheduling
python scheduler.py -l 300,200,100 -p SJF -c
# three jobs, length 300, 200, 100, RR scheduling
python scheduler.py -l 300,200,100 -p RR -c
# three jobs, length 300, 200, 100, RR scheduling, quantum of 10 for the scheduler
python scheduler.py -l 300,200,100 -p RR -c -q 10

Address spaces

  • Connect to a Linux VPS (e.g. one running on Amazon or Digital Ocean)

  • Look at the man page for free.

  • Run:

Terminal window
# free memory in mebibytes
free -m
# free memory in "human format"
free -h
Terminal window
# find the process ID of nginx
ps -aux | grep nginx
# use pmap on it
sudo pmap 831
sudo pmap -x 831