34
Lecture 4 CS 326: Operating Systems Booting Up

CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

Lecture 4

CS 326: Operating Systems

Booting Up

Page 2: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

§ Studying for Quiz 1§ Debugging and Memory Profiling§ Booting the OS§ Processes

Today’s Schedule

8/27/20 CS 326: Operating Systems 2

Page 3: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

§ Studying for Quiz 1§ Debugging and Memory Profiling§ Booting the OS§ Processes

Today’s Schedule

8/27/20 CS 326: Operating Systems 3

Page 4: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

§ Quiz 1 is next Tuesday§ 12-hour window on Canvas§ 15 minutes

§ Multiple choice, short answer

§ Covers lectures and lab material§ To study: look over the slides, note anything you don’t

remember/understand, and go through the labs§ For things you don’t know: check the reading for more in-

depth explanation, ask on Discussion board, or office hours

Quiz 1

8/27/20 CS 326: Operating Systems 4

Page 5: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

§ Studying for Quiz 1§ Debugging and Memory Profiling§ Booting the OS§ Processes

Today’s Schedule

8/27/20 CS 326: Operating Systems 5

Page 6: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

§ Ok, what’s the toughest buggy program so far?§ Let’s look at a couple tools we can use to give us

hints: valgrind and gdb§ For gdb, check out the guide on the schedule page

Lab 2

8/27/20 CS 326: Operating Systems 6

Page 7: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

§ Studying for Quiz 1§ Debugging and Memory Profiling§ Booting the OS§ Processes

Today’s Schedule

8/27/20 CS 326: Operating Systems 7

Page 8: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

§ Let’s say you’re building your own OS. What’s the very first thing we have to do?

§ …

Running a Program

8/27/20 CS 326: Operating Systems 8

Page 9: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

§ When you turn on your computer, it starts the boot process§ Named for “pulling oneself over a fence by one's

bootstrapsӤ Or in other words, doing the impossible

§ Booting is a series of tasks that ultimately get the operating system running

§ The first thing you (may) see is the POST§ Power-on Self Test

Booting Up

8/27/20 CS 326: Operating Systems 9

Page 10: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

8/27/20 CS 326: Operating Systems 10

Page 11: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

§ After initializing the hardware, your basic input-output system (BIOS) will start iterating through the disks connected to your machine§ When installing a new OS, you may change the

boot order§ On a Mac, you can do this by holding down Option

during boot (other machines: F12, Del…)

§ Once a bootable disk is found, we proceed to the next boot phase

BIOS

8/27/20 CS 326: Operating Systems 11

Page 12: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

§ The first 512 bytes on a hard drive contain the Master Boot Record (MBR)

§ The MBR has two parts:§ Partition table

§ Partition: segment of a disk§ Partition 1: Windows; Partition 2: Linux; etc.

§ Boot code§ Responsible for continuing the boot process

Master Boot Record

8/27/20 CS 326: Operating Systems 12

Page 13: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

1. The BIOS looks for a disk partition that ends with 0x55AA

2. Then it moves to address 0x7C00 and begins executing the instructions there

Finding a Bootable Partition

8/27/20 CS 326: Operating Systems 13

Page 14: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

§ You might be surprised that these offsets are just hard-coded into the system

§ However, the BIOS is not particularly smart§ To make it smart would begin to resemble an OS

§ The hardware should be as simple as possible§ Making it smart is very expensive!

§ The software (OS) does not want to make many assumptions about the underlying hardware

§ Modern EFI systems add more complexity, but the principle remains the same

Hard-Coded Offsets

8/27/20 CS 326: Operating Systems 14

Page 15: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

§ We began executing instructions, so we’re all done starting the OS, right?§ Unfortunately, no…

§ The first instructions executed are part of the bootloader§ The bootloader is a bit

smarter than the BIOS, and handles the next steps in the boot process

§ What bootloader did we use on our VMs?

Continuing the Boot Process

8/27/20 CS 326: Operating Systems 15

Page 16: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

§ The bootloader can understand a variety of file systems§ Invent a new file system? Add support to the

bootloader. No need to modify the hardware ROM

§ It can also provide a list of operating systems available in a multi-boot configuration

§ …And it can handle larger disks!§ The BIOS is limited to a fixed number of partitions and

disk sizes

Bootloader

8/27/20 CS 326: Operating Systems 16

Page 17: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

§ You can only have one bootloader per disk§ Installing one will overwrite another§ Luckily, you can always insert another disk (rescue

CD, USB, etc)§ Folks that forgot their password: we just ”insert” the

CD .iso into the VM, boot off the LiveCD, and then change the password there§ Forgot root password on a Linux machine? Put its hard

drive in your computer, chroot into it, run passwd

Bootloader Restrictions

8/27/20 CS 326: Operating Systems 17

Page 18: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

§ The bootloader starts the OS§ In Linux, the next stage stage is represented by an

initial ramdisk (initrd)§ Lightweight, mini Linux system image§ After booting, the first process takes over

§ Process: a running instance of a program

§ PID 1, also known as init, starts the rest of the processes§ System services, startup tasks, etc§ Linux: systemd (that’s what the systemctl command is

controlling!)

Finishing the Boot Process

8/27/20 CS 326: Operating Systems 18

Page 19: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

§ PID 1 is the direct or indirect ancestor of all other processes§ When one process launches another, it is that process’s parent

§ The newly-launched process is the child§ Unfortunately there are no uncle or aunt processes…

§ The kernel or scheduler is usually considered PID 0, but they technically aren’t processes.

§ Some init implementation can do more or less:§ System V Init, systemd, upstart, launchd, etc.

§ You can even write your own!§ Boot Linux with flag: init=/path/to/your/init

PID 1, a.k.a. init

8/27/20 CS 326: Operating Systems 19

Page 20: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

/* We try each of these until one succeeds.* The Bourne shell can be used instead of init if we are* trying to recover a really broken machine. */

if (execute_command) {ret = run_init_process(execute_command);if (!ret)return 0;

panic("Requested init %s failed (error %d).", execute_command, ret);}

if (!try_to_run_init_process("/sbin/init") ||!try_to_run_init_process("/etc/init") ||!try_to_run_init_process("/bin/init") ||!try_to_run_init_process("/bin/sh"))

return 0;

panic("No working init found. Try passing init= option to kernel. ""See Linux Documentation/admin-guide/init.rst for guidance.");

Linux: init/main.c

8/27/20 CS 326: Operating Systems 20

Page 21: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

[malensek@ruby:~]$ ps -ef

UID PID PPID C STIME TTY TIME CMDroot 1 0 0 Feb21 ? 00:00:57 /usr/lib/systemd/systemdroot 2 0 0 Feb21 ? 00:00:00 [kthreadd]root 3 2 0 Feb21 ? 00:00:00 [ksoftirqd/0]root 5 2 0 Feb21 ? 00:00:00 [kworker/0:0H]root 7 2 0 Feb21 ? 00:01:21 [rcu_sched]root 8 2 0 Feb21 ? 00:00:00 [rcu_bh]root 9 2 0 Feb21 ? 00:00:13 [rcuos/0]root 10 2 0 Feb21 ? 00:00:00 [rcuob/0]root 11 2 0 Feb21 ? 00:00:00 [migration/0]root 12 2 0 Feb21 ? 00:00:00 [lru-add-drain]root 13 2 0 Feb21 ? 00:00:01 [watchdog/0]

...

malensek 235 1 0 Feb21 tty1 00:00:00 login -- malensekmalensek 282 235 0 Feb21 tty1 00:00:00 bash

Process Lineage

8/27/20 CS 326: Operating Systems 21

Page 22: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

§ Studying for Quiz 1§ Debugging and Memory Profiling§ Booting the OS§ Processes

Today’s Schedule

8/27/20 CS 326: Operating Systems 22

Page 23: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

§ When a program is executed, the OS reads its static data from the disk and copies it into main memory§ Program instructions, string literals, binary data

§ A process ID (PID) is assigned§ Space is allocated for the stack and heap§ File descriptors are initialized

§ stdout, stderr, stdin

§ Run-time permissions are applied

From Program to Process

8/27/20 CS 326: Operating Systems 23

Page 24: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

Process Memory Layout

8/27/20 CS 326: Operating Systems 24

Page 25: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

§ Processes are given a zeroed out virtual address space rather than accessing main memory directly§ Prevents viewing/changing other process data§ Makes memory allocation and management simpler

§ Processes are still allowed to communicate, however, via inter-process communication (IPC) mechanisms§ Pools of shared memory§ Signals, sockets, pipes, or even files

Process Restrictions

8/27/20 CS 326: Operating Systems 25

Page 26: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

§ Processes are limited to virtualized views of the hardware, but they are still able to inspect it

§ Memory, CPU, disk availability and usage

§ Other process names and command lines

§ Logged in users

§ Hardware specs, serial numbers, etc.§ This is good, especially in shared environments!

Inspecting the System

8/27/20 CS 326: Operating Systems 26

Page 27: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

Demo: systemctl, ps, …

8/27/20 CS 326: Operating Systems 27

Page 28: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

[malensek@ruby:~]$ w23:11:06 up 1 day, 7:57, 11 users, load average: 16.07, 15.17, 11.34USER TTY LOGIN@ IDLE JCPU PCPU WHATmal pts/0 07:16 14:37m 0.07s 0.07s -bashzoe pts/1 20:04 3:06m 0.43s 0.38s vim output/file-0wash pts/2 23:00 4:57 0.09s 0.05s vim Makefileinara pts/3 21:52 1:08m 0.82s 0.79s /usr/bin/python2jayne pts/4 23:10 12.00s 0.03s 0.03s -bashmalensek pts/5 23:11 0.00s 0.10s 0.04s w

Inspecting the System

8/27/20 CS 326: Operating Systems 28

Page 29: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

§ In the early days, only one process could run at once§ After all, we only have one CPU, right?

§ The OS would load a program off the disk into main memory and then start executing its instructions§ At this point, the process had full control of the

machine

§ Multitasking challenged this idea: why not let multiple processes share the CPU?

Executing a Process

8/27/20 CS 326: Operating Systems 29

Page 30: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

§ In early multitasking systems, processes were responsible for yielding control of the CPU to others

§ Once you are given control of the CPU, you can do whatever you want!

§ As you can imagine, no process wanted to give up control of the CPU

§ Buggy processes can also take control of the CPU and never give it up

Multitasking

8/27/20 CS 326: Operating Systems 30

Page 31: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

§ Putting processes in control was messy and error prone… not to mention complicated!

§ A better approach turned out to be virtualizing the CPU§ Making each process think it has exclusive control

over its very own CPU

§ This led to preemptive multitasking§ Processes can be preempted – swapped out at will by

the OS

CPU Virtualization

8/27/20 CS 326: Operating Systems 31

Page 32: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

§ Let’s assume you want to listen to music and work on your CS 326 lab

§ If you only have a single core, you need to switch between these two tasks quickly§ Switch too slow, and your music will skip

§ Switch quickly enough, and the user will think things are all happening at the same time!§ Hundreds of processes run per second

Concurrent Tasks

8/27/20 CS 326: Operating Systems 32

Page 33: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

Multitasking

8/27/20 CS 326: Operating Systems 33

> Interrupt Safari, save Safari state< Load KernelThread state, execute KernelThread> Interrupt KernelThread, save KernelThread state< Load cmatrix state, execute cmatrix> Interrupt cmatrix, save cmatrix state< Load vim state, execute vim> Interrupt vim, save vim state< Load KernelThread state, execute KernelThread> Interrupt KernelThread, save KernelThread state< Load Safari state> Interrupt Safari, save Safari state

Page 34: CS 326: Operating Systemsmmalensek/cs326/schedule/lectures/326-L4.pdfToday’s Schedule 2/5/20 CS 326: Operating Systems 2 ... or office hours §After the individual quiz, we’ll

§ Great, OS class is all booted up.§ We’ll talk more about the process execution

mechanism next week§ Plus: how threads fit into this picture

§ We’ll also cover process scheduling, including preemptive multitasking in the… nearish future

Wrapping Up

8/27/20 CS 326: Operating Systems 34