1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
# Chapter 1
> An operating system is a program that manages the computer hardware.
4 high level components:
* hardware
* CPU, memory, I/O devices provide computing resources for the system.
* operating system
* controls the hardware and coordinates its use among the various applicatoin programs for the various users.
* application programs
* word processors, spreadsheets, compilers, web browsers define ways to use resources to solve users' computer problems.
* users
We can also view a computer system as:
* hardware
* software
* data
The OS provides the mans for proper use of these resources in the operation of the computer system.
The OS performs no useful function by itself.
It provides an environment within which other programs can do useful work.
## User View
Most computer users sit in front of a PC that has
* a monitor
* keyboard
* mouse
* and system unit
Another usage is where a user sits at a terminal connected to a mainframe
or a minicomputer. Other users access the same computer through other terminals.
These users share resources and may exchange information.
The operating system is the one program running at all times on the computer - usually called the kernel.
Along with the kernel there are systems programs, which are associated with the operating system but are not part of the kernel, and application programs, which include all programs not associated with the operation of the system.
1. bootstrap program stored in ROM or EEPROM known as firmware.
* initializes CPU registers, device controllers, memory contents.
* must know how to load the operating system
* must load the operating system kernel
* execute first process known as init.
* occurrence of an event is signaled by an interrupt from either hardware or software.
* hardware may send signal to CPU to trigger an interrupt via the system bus.
* software may trigger an interrupt by executing system calls.
* when a CPU is interrupted it stops what it is doing and immediately transfers execution to a fixed location.
The fixed location usually contains the starting address where the service routine for the interrupt is located.
Interrupts must be handled quickly so only a predefined # of interrupts is possible.
> Interrupt vector: table of interrupt addresses.
The CPU can load instructions only from memory, so any programs to run must be stored there.
All forms of memory provide an array of words. Each word has its own address.
Interaction is achieved through a sequence of `load` or `store` instructions to specific memory addresses.
The `load` instruction moves a word from main memory to an internal register within the CPU, whereas the
`store` instruction moves the content of a register to main memory.
von Neumann architecture:
1. fetch instruction from memory and store that instruction in the instruction register.
2. decode instruction. may cause operands to be fetched from memory and stored in some internal register.
3. execute instruction
4. store result back in register
Memory unit only sees a stream of memory addresses.
Ideally, we want programs and data to reside in main memory. This
is usually not possible because:
1. main memory is usually too small to store all needed programs and data permanently.
2. main memory is a volatile storage device that loses its contents when power is removed.
Most computer systems use secondary storage as an extension of main memory.
Secondary storage must be able to hold large quantities of data permanently.
The magnetic disk is a common secondary storage device.
Most programs are stored on disk until they are loaded into memory.
## Computer System Architecture
Single-Processor Systems: one main CPU
Multiprocessor System: have two or more processors in close communication sharing the computer bus, clock, memory and peripheral devices.
Multiprocessor systems have three main advantages:
1. Increased throughput: more work done in less time.
2. Economy of scale: can cost less because they share peripherals, storage, and power.
3. Increased reliability: failure of one processor will not halt the system. only slow it down
Symetric Multi Processor (SMP): Solaris is a commercial version of UNIX designed by Sun Microsystems. Many processors can run simultaneously.
Uniform Memory Access (UMA): access to any RAM from any CPU takes the same amount of time.
Non-uniform Memory Access (NUMA): some parts of memory may take longer to access than other parts.
Another trend is to include multiple cores on a single chip. These are like multiprocessor chips.
On chip communication is faster than a single core per chip.
One chip with multiple cores also uses less power than multiple single-core chips.
Dual core design:
```plaintext
------------------------------------
| --------------- --------------- |
| | CPU core0 | | CPU core1 | |
| | ----------- | | ----------- | |
| | |registers| | | |registers| | |
| | ----------- | | ----------- | |
| | | | | | | |
| | ----------- | | ----------- | |
| | | cache | | | | cache | | |
| | -----|----- | | -----|----- | |
| -------|------- -------|------- |
---------|----------------|---------
------------------
|
----------
| memory |
----------
```
Job pool
```plaintext
0 |------------------|
| operating system |
|------------------|
| job 1 |
|------------------|
| job 2 |
|------------------|
| job 3 |
|------------------|
| job 4 |
512M |------------------|
```
Operating schedules a job to put into memory to execute.
When the job needs to wait for an I/O operation to complete
the operating system switches to another job and so on.
As long as at least one job needs to execute then the CPU is never idle.
In time-sharing systems, the CPU executes multiple jobs
by switching among them, but the switches occur so frequently that
the users can interact with each program while it is running.
A program laoded into memory and executing is called a process.
When a process executes, it typically executes for only a short time
before it either finishes or needs to perform I/O.
Modern operating systems are interrupt driven.
Events are almost always signaled by the occurrence of an interrupt or a trap.
A trap, or an exception, is a software generated interrupt caused either by
an error or by a specific request from a user program that an operating-system
service be performed.
Terms:
* CPU scheduling
* job scheduling
* logical memory
* physical memory
* swapping
* virtual memory
For each type of interrupt, separate segments of code in the operating system
determine what action should be taken. An interrupt service routing
is provided that is responsible for dealing with the interrupt.
## Dual Mode Operation
Must distinguish between operating system code and user-defined code.
* user mode
* kernel mode (supervisor mode, system mode, priviliged mode)
The `mode bit` is added to hardware to indicate the current mode: kernel (0), user (1)
The hardware allows privileged instructions to be executed only in kernel mode.
System calls provide the means for a user program to ask the operating system to perform
tasks reserved for hte operating system on the user program's behalf.
A system call usually takes the form of a trap to a specific location in the interrupt vector.
This trap can be executed by a generic trap instruction, although some systems have a specific
syscall instruction.
### Timer
We must ensure that the operating system maintains control over the CPU.
We cannot allow a user program to get stuck in an infinite loop or to fail
to call system services and never return control to the operating system.
To accomplish this, we can use a `timer`. A timer can be set to interrupt
the computer after a specified period.
## Process Management
A programs instructions are executed by a CPU.
A program in execuation is a `process`.
A time-shared user program is a `process`.
A process needs:
* CPU time
* memory
* files
* I/O devices
A single-threaded process has one `program counter`
specifying the next instruction to execute.
The execution of such a process must be sequential.
Although more than one process may be associated with
the same program, they are considered two separate execution
sequences.
The operating system is responsible for:
* scheduling processes and threads on the CPU
* creating and deleting both user and system processes
* suspending and resuming processes
* providing mechanisms for process synchronization
* providing mechanisms for process communication
## Memory Management
Main memory is a large array of words or bytes, ranging
in size from hundreds of thousands to billions.
Each word or byte has it's own address.
Main memory is a pool of quickly accessible data shared by
the CPU and I/O devices.
On a von Neuman architecture, the central processor reads
instructions from main memory during the instruction-fetch
cycle and both reads and writes data from main memory during the
data-fetch cycle.
The main memory is generally the only large storage device
that the CPU is able to address and access directly.
For example, for the CPU to process data from disk, those data
must first be transferred to main memory by CPU-generated I/O calls.
For a program to be executed, it must be mapped to absolute
addresses and loaded into memory. As the program executes, it
accesses program instructions and data from memory by generating
these absolute addresses.
The operating system is responsible for the following:
* keeping track of which parts of memory are currently being used and by whom
* deciding which processes (or parts thereof) and data to move into and out of memory
* allocating and deallocating memory space as needed.
## Storage Management
The OS provides uniform, logical view of information storage.
The OS abstracts from the physical properties of its storage
devices to define a logical storage unit, the `file`.
The OS maps files onto physical media and accesses these files
via the storage devices.
### File-System Management
File management is one of the most visible components of an operating system.
Computers can store information on several different types of physical media.
Magnetic disk, optical disk, and magnetic tape are th emost common.
Each of these media has its own characteristics and physical organization.
Each medium is controlled by a device, such as a disk drive or tape drive,
that also has its own unique characteristics. These properties include
access speed, capacity, data-transfer rate, and access method (sequential or random).
A `file` is a collection of related information defined by it's creator.
Files represent programs and data. Data files may be numeric, alphabetic,
alphanumeric or binary. File may be free-from or they may be formatted rigidly.
The OS is responsible for:
* creating and deleting files
* creating and deleting directories to organize files
* supporting primitives for manipulating files and directories
* mapping files onto secondary storage
* backing up files on stable (nonvolatile) storage media
### Mass-Storage Managment
OS is responsible for
* free space management
* storage allocation
* disk scheduling
### Caching
| Level | 1 | 2 | 3 | 4 |
| ----- | - | - | - | - |
| Name | registers | cache | main memory | disk storage |
| Size | < 1 KB | < 16 MB | < 64 GB | > 100 GB |
| Access time (ns) | 0.25 - 0.5 | 0.5 - 25 | 80 - 250 | 5,000.000 |
| Bandwith (MB/sec) | 20,000 - 100,000 | 5,000 - 10,000 | 1,000 - 5,000 | 20 - 150 |
| Managed by | compiler | hardware | operating system | operating system |
| Backed by | cache | main memory | disk | CD or tape |
### I/O Systems
OS's hide the pecularities of specific hardware devices from the user.
The I/O subsystem consists of:
* memory management component that includes buffering, caching, and spooling
* general device-driver interface
* drivers for specific hardware devices
Only the device driver knows the peculiarities of the specific device to
which it is assigned.
## Protection and Security
Memory-addressing hardware ensures that a process can execute only within its own address space.
The timer ensures that no process can gain control of the CPU without eventually relinquishing control.
Device-control registers are not accessible to users, so the integrity of the various peripheral devices is protected.
`Protection` is any mechanism for controlling the access of processes or users to the resources defined by a computer system.
It is the job of `security` to defend a system from external and internal attacks.
Operating systems maintain a list of user names and associated user identifiers (user IDs aka SIDs).
The GNU General Public License (GPL) codifies copylefting and is a common license under
which free software is released. Fundamentally, GPL requires that the source code be
distributed with any binaries and that any changes made to the source code be released
under the same GPL license.
## BSD Unix
BSD Unix has a longer and more complicated history than Linux.
It started in 1978 as a derivative of AT&T's UNIX.
Releases from the University of California at Berkeley came in source and binary
form, but they were not open-source because a license form AT&T was required.
BSD UNIX's development was slowed by a lawsuit by AT&T, but eventually a fully
functional open-source version, 4.4BSD-lite, was released in 1994.
There are many distributions of UNIX, including FreeBSD, NetBSD, OpenBSD and DragonflyBSD.
The source code for FreeBSD can be found in `/usr/src/`. Ther kernel source code
is in `/usr/src/sys`.
Darwin, the core kernel component of macOS is based on BSD UNIX and is open-sourced as well.
## Summary
An operating system is software that manages the computer hardware, as well as providing
an environment for application programs to run.
For a computer to do its job of executing programs, the programs must be in main memory.
Main memory is the only large storage area that the processor can access directly.
It is an array of words or bytes, ranging in size from millions to billions. Each
word in memory has its own address.
|