Transcript Lecture 5

CS 245: Introduction to Computer
Organization, Operating Systems, and
Networks
Lecture 5: Operating Systems
John Hurley
Cal State LA
Where We Are Now
Three main topics for this course:
• Computer Organization
– Part 2 of this week's lecture will cover a little more
Assembly
– This week's lab, with two more Assembly problems,
completes the Computer Organization unit
• Operating Systems
– We have covered aspects of *nix shell usage which were
essential to the lab work we have done so far
– Part 1 of this week's lecture will begin our general
introduction to operating systems
• Networking
– Yet to come
Midterm
The midterm for this course will be given in two
parts, each about 1 hour long.
• Part 1 will be an Assembly programming
problem and will be administered in class next
week.
• Part 2 will consist of two *nix shell script
programming problems and will be
administered in class during week 8
Computer System Structure
A computer system can be divided into four components:
– Hardware – provides basic computing resources
• CPU, memory, I/O devices
– Operating system
• Controls and coordinates use of hardware among various
applications and users
– Application programs – define the ways in which the
system resources are used to solve the computing
problems of the users
• Word processors, compilers, web browsers, database systems,
video games
– Users
• People, machines, other computers
Silberschatz, Galvin, and Gagne, 2013
Four Components of a Computer System
Silberschatz, Galvin, and Gagne, 2013
Operating Systems
Operating System (OS):
• Software that manages hardware and software resources and
provides common services for computer applications. (Wikipedia)
• Other than very small embedded systems, almost all computers use
operating systems.
• An OS allocates resources among multiple processes or users
• An OS controls program execution to prevent errors or
inappropriate access to data
Operating Systems
The most common operating systems in current usage are
• Windows
– Desktop and laptop computers
– Some market share for servers
• Linux
–
–
–
–
Widely used for web and database servers
Some market share for desktop computers
Larger embedded systems
Many specialized types of Linux
• UNIX
– Oracle Solaris
– Mac OSX
Operating System Definition
• No universally accepted definition
• “Everything a vendor ships when you order an operating
system” is a good proxy
– But varies wildly
• “The one program running at all times on the computer”
is the kernel.
• bootstrap program is loaded at power-up or reboot
– Typically stored in ROM or EPROM, generally known as
firmware
– Initializes all aspects of system
– Loads operating system kernel and starts execution
• Everything else is either
– a system program (ships with the operating system) , or
– an application program.
Silberschatz, Galvin and Gagne, 2013
Operating Systems
• The OS saves programmers from the necessity to work with lowlevel aspects of computing like
– Choosing specific blocs of memory to use
– Deciding which program can use the CPU at a particular time
– Working directly with storage and I/O hardware
• An OS abstracts away the details of hardware from programmers
and users who do not need to know these details.
• High-level languages are very hard to implement without an OS.
Platform-independence is not practical.
• In a system without an OS, such as a microcontroller used with a
small embedded system, we either use Assembly or very low level
C code. I/O often involves addressing specific bits in memory that
are mapped to particular input or output devices.
Operating Systems
 The OS manages these resources and
allocates them to specific programs and users.
 An OS provides services for
 Processor Management
 Memory Management
 File Management
 Device Management
 Concurrency Control
 Oss have at least three main objectives:
 provide easy to us platform for developers
 provide optimal performance for each user
 use hardware efficiently
Ugur Halici, Middle East Technical University
System Boot
• When power initialized on system, execution starts at a fixed
memory location
– Firmware ROM used to hold initial boot code
• Operating system must be made available to hardware so
hardware can start it
– Small piece of code – bootstrap loader, stored in ROM or
EEPROM locates the kernel, loads it into memory, and starts it
– Sometimes two-step process where boot block at fixed
location loaded by ROM code, which loads bootstrap loader
from disk
• Common bootstrap loader, GRUB, allows selection of kernel
from multiple disks, versions, kernel options
• Kernel loads and system is then running
Operating Systems
• Another view of the usage
of OS is that it is a
predefined library for
hardware-software
interaction.
• This is why system
programs (eg, *nix
utilities) apply to the
installed OS. They do not
access hardware directly.
Ugur Halici, Middle East Technical University
Application Programs
System Programs
Operating System
Machine Language
HARDWARE
Operating Systems
A simple program
segment with no
hardware
consideration
OS
A more
sophisticated
program segment
with hardware
consideration
Ugur Halici, Middle East Technical University
Machine
Language
Hardware
response
First Generation OS: direct input
• Run one job at a time
– Enter it into the computer (might require rewiring!)
– Run it
– Record the results
• Problem: lots of wasted computer time!
– Computer was idle during first and last steps
– Computers were very expensive!
• Goal: make better use expensive computer time
Ahmed Amer, Ethan L. Miller and Scott A. Brandt, University of Pittsburgh
Second Generation: Batch Systems
•
•
•
•
•
Bring cards to 1401
Read cards onto input tape
Put input tape on 7094
Perform the computation, writing results to output tape
Put output tape on 1401, which prints output
Ahmed Amer, Ethan L. Miller and Scott A. Brandt, University of Pittsburgh
Structure of a typical 2nd generation job
Data for
program
FORTRAN
program
$END
$RUN
$LOAD
$FORTRAN
$JOB, 10,6610802, ETHAN MILLER
Ahmed Amer, Ethan L. Miller and Scott A. Brandt, University of Pittsburgh
Spooling
• Punch card (AKA, "IBM Card")
Ahmed Amer, Ethan L. Miller and Scott A. Brandt, University of Pittsburgh
Spooling
• Original batch systems used tape drives
• Later batch systems used disks for buffering
– Operator read cards onto disk attached to the
computer
– Computer read jobs from disk
– Computer wrote job results to disk
– Operator directed that job results be printed from disk
• Disks enabled simultaneous peripheral operation
on-line (spooling)
– Computer overlapped I/O of one job with execution of
another
– Less need for CPU to wait for I/O to complete
– Still only one job in execution at any given time
Ahmed Amer, Ethan L. Miller and Scott A. Brandt, University of Pittsburgh
Operating Systems
• Applications use different resources differently.
– Some are computation-intensive, using the CPU
heavily.
– Others use memory heavily.
– Yet others use I/O heavily.
• Multiprogramming means sharing of resources
between different processes.
• In multiprogramming, hardware can be used
efficiently by intelligent allocation of different
resources at different times. For example, an OS
can allow one process to use the CPU while
another performs I/O.
Ugur Halici, Middle East Technical University
Third generation: multiprogramming
• Multiple jobs in memory
– Protected from one another
Job 3
Job 2
Memory
partitions
Job 1
Operating
system
• Operating system protected
from each job as well
• Resources (time, hardware)
split between jobs
• Still not interactive
– User submits job
– Computer runs it
– User gets results minutes
(hours, days) later
Ahmed Amer, Ethan L. Miller and Scott A. Brandt, University of Pittsburgh
Operating Systems
• With the development of interactive computation in 1970s, timesharing systems emerged.
• In these systems, multiple users have terminals (not computers)
connected to a main computer and execute tasks in the main
computer.
• Note both the similarities and differences between this and using
ssh to access a remote VM
Main computer; having a CPU
executing processes by
utilization of the OS, (e.g.
UNIX).
Terminals are connected
to the main computer
and used for input and
output. No processing is
made. They do not have
CPUs.
Ugur Halici, Middle East Technical University
Operating Systems
• Multiprocessor systems have multiple processors
sharing memory and peripheral devices.
• With this configuration, they have greater
computing power and higher reliability.
• Multiprocessor systems are classified as
– tightly-coupled: each processor is assigned a specific
duty, but processors work in close association, possibly
sharing the same memory and loosely-coupled
(distributed).
– loosely coupled: each processor has its own memory and
copy of the OS.
Ugur Halici, Middle East Technical University
Operating Systems
• Use of networks requires appropriate OS.
• In networked systems, each process runs in its
own machine but the OS have access to other
machines.
• This makes file sharing, messaging, etc.
possible.
• In networks, the user is aware of the fact that
s/he is working in a network and when
information is exchanged. The user explicitly
handles the transfer of information.
Ugur Halici, Middle East Technical University
Computer-System Operation
• I/O devices and the CPU can execute
concurrently
• Each device controller is in charge of a
particular device type
• Each device controller has a local buffer
• CPU moves data from/to main memory
to/from local buffers
• I/O is from the device to local buffer of
controller
• Device controller informs CPU that it has
finished its operation by causing an interrupt
Silberschatz, Galvin, and Gagne, 2013
Common Functions of Interrupts
• Interrupt transfers control to the interrupt
service routine generally, through the
interrupt vector, which contains the
addresses of all the service routines
• Interrupt architecture must save the address
of the interrupted instruction
• A trap or exception is a software-generated
interrupt caused either by an error or a user
request
• An operating system is interrupt driven
Silberschatz, Galvin and Gagne, 2013
Interrupt Handling
• The operating system preserves the
state of the CPU by storing registers
and the program counter
• Determines which type of interrupt has
occurred:
– polling
– vectored interrupt system
• Separate segments of code determine
what action should be taken for each
type of interrupt
Silberschatz, Galvin, and Gagne, 2013
Interrupt Timeline
Silberschatz, Galvin, and Gagne, 2013
I/O Structure
• After I/O starts, control returns to user program
only upon I/O completion
– Wait instruction idles the CPU until the next
interrupt
– Wait loop (contention for memory access)
– At most one I/O request is outstanding at a time, no
simultaneous I/O processing
• After I/O starts, control returns to user program
without waiting for I/O completion
– System call – request to the OS to allow user to wait
for I/O completion
– Device-status table contains entry for each I/O
device indicating its type, address, and state
– OS indexes into I/O device table to determine
device status and to modify table entry to include
interrupt
Silberschatz, Galvin and Gagne, 2013
Anatomy of a device request
3
CPU
1
5 Interrupt
controller
6
2
1: Interrupt
Disk
controller
4
• Left: sequence as seen by hardware
Instructionn
Instructionn+1
Operating
system
Interrupt handler
3: Return
2: Process interrupt
– Request sent to controller, then to disk
– Disk responds, signals disk controller which tells interrupt controller
– Interrupt controller notifies CPU
• Right: interrupt handling (software point of view)
Ahmed Amer, Ethan L. Miller and Scott A. Brandt, University of Pittsburgh
29
Processes
• Process: program in execution
– Address space (memory) the
program can use
– State (registers, including
program counter & stack
pointer)
A
B
C
E
• OS keeps track of all processes in
a process table
• Processes can create other
processes
D
F
G
– Process tree tracks these
relationships
– A is the root of the tree
– A created three child processes:
B, C, and D
– C created two child processes: E
and F
– D created one child process: G
Ahmed Amer, Ethan L. Miller and Scott A. Brandt, University of Pittsburgh
Inside a (Unix) process
0x7fffffff
Stack
• Processes have three
segments
– Text: program code
– Data: program data
• Statically declared variables
• Areas allocated by malloc()
or new
– Stack
Data
Data
Text
• Automatic variables
• Procedure call information
• Address space growth
– Text: doesn’t grow
– Data: grows “up”
– Stack: grows “down”
0
Ahmed Amer, Ethan L. Miller and Scott A. Brandt, University of Pittsburgh
Deadlock
Potential deadlock
Actual deadlock
Ahmed Amer, Ethan L. Miller and Scott A. Brandt, University of Pittsburgh
Interprocess communication
• Processes want to exchange information with each other
• Many ways to do this, including
– Network
– Pipe (special file): A writes into pipe, and B reads from it
A
B
Ahmed Amer, Ethan L. Miller and Scott A. Brandt, University of Pittsburgh
A Few More UNIX Commands
Part 1 of this week's lab will require you to learn a
few more things about using *nix:
tarball is a file compression format often used in
*nix systems. Work with it using the tar utility. Use
tar --help | less for more info. The syntax you will
use to untar the sample code file in this week's lab
will be
tar -zxvf *code.intro.tgz
Put the tarball in a separate directory first, since
there are several files in the archive.
A Few More UNIX Commands
• & means run a program in the background
(that is, don't make the shell user wait while
the program is running). For example,
./myprogram &
•
•
•
•
•
This won’t work if there is user I/O!
ps: list running processes
kill [num]: kill a running process
control z: stop (not kill) a foreground process
fg bring a background process to the
foreground
A Few More UNIX Commands
Consider this Java code:
public class LongLoop{
public static void main(String[] args){
long i;
for(i = 0; i < Long.MAX_VALUE; i+=1){}
System.out.println(i);
}
}
a) compile and run it in the foreground. Hit control c when
you get bored
b) compile and run it in the background, doing something else
at the command prompt for a while. Then use ps to find the
job number (it will be listed as just "java") and kill the process.
A Few More UNIX Commands
The reading also refers to some concepts used in C that you may not be
familiar with.
Check this reference to help you understand how pointers work:
http://www.cprogramming.com/tutorial/lesson6.html
And this one on malloc():
http://www.cplusplus.com/reference/cstdlib/malloc/
A general understanding of these concepts is enough for this course.
More
Assembly
Language
Here is a program with a function call that passes a parameter using a register:
.intel_syntax
.section .text
.globl add_two
# name of the function
.type add_two, @function
add_two:
push %rbp
# set up stack frame
mov %rbp, %rsp
# set up stack frame
mov %eax, %edi
# get 32 bit value and put in eax
add %eax, 2
# add two, just like the name
mov %rsp, %rbp
# clean up for function return
pop %rbp
ret
.size add_two, .-add_two
.globl main
.type main, @function
main:
push %rbp
mov %rbp, %rsp
mov %edi, 10
# edi is conventionally used to pass values as arguments. This is a 32 bit value, else would use rdi
call add_two
mov %esi, %eax
mov %rdi, OFFSET FLAT:format_string
call printf
mov %eax, 0
mov %rsp, %rbp
pop %rbp
ret
.size main, .-main
.section .rodata
format_string:
.string "%d\n"
Passing Arguments On The Stack
It is also possible to pass arguments on the stack. By convention, we would pass the
first few arguments in registers and only use the stack if there are many arguments.
See the Hirzel reading for the conventional order of registers to use.
This example and the second lab problem are exercises in using the stack for
this purpose. Thus, we ignore the registers-first convention and just use the stack.
More
Assembly
Language
This program has a function call that passes 64 bit parameters on the stack. Note that the call
instruction itself subtracts 8 from rsp.
.intel_syntax
.section .rodata
format_string:
.string "%d+%d=%d\n"
.section .text
.globl add_arguments
.type add_arguments, @function
add_arguments:
push %rbp
mov %rbp, %rsp
/* can't just pop because caller rbp is on the stack. Also, call subtracts 8 from rsp, so we need to account for that. Thus, first op we pushed is at rsp + 16, second is at rsp
+ 24 */
mov %rsi, [%rsp+16]
mov %rdx, [%rsp+24]
mov %rax, %rsi
add %rax, %rdx
mov %rcx, %rax
mov %rdi, OFFSET FLAT:format_string
call printf
mov %rsp, %rbp
pop %rbp
ret
.size add_arguments, .-add_arguments
.globl main
.type main, @function
main:
push %rbp
mov %rbp, %rsp
sub %rsp, 16
push 42
push 38
call add_arguments
mov %rax, 0
mov %rsp, %rbp
pop %rbp
ret
.size main, .-main