Operating Systems: User Process User Process User Process User Process
Operating Systems: User Process User Process User Process User Process
CS 217
OS Kernel
Hardware
1
Operating System
• Coordinates access to physical resources
CPU, memory, disk, i/o devices, etc.
• Provides services
Protection
Scheduling User User
Memory management Process Process
File systems
Synchronization OS Kernel
etc.
Hardware
OS as Government
• Makes lives easy Randy
Promises everyone whole machine Wang
(dedicated CPU, infinite memory, …)
Provides standardized services
(standard libraries, window systems, …)
2
OS History
• Development of OS paradigms: Randy
Phase 0: User at console Wang
Phase 1: Batch processing
Phase 2: Interactive time-sharing
Phase 3: Personal computing
Phase 4: ?
• Advantages
Interactive!
No one can hurt anyone else
• Disadvantages
Reservations not accurate, leads to inefficiency
Loading/ unloading tapes and cards takes forever and
leaves the machine idle
3
Phase 1: Batch Processing
• How things work Randy
Sort jobs and batch those with similar needs Wang
to reduce unnecessary setup time
Resident monitor provides “automatic job sequencing”:
it interprets “control cards” to automatically run a bunch
of programs without human intervention
• Advantage
Good utilization of machine
• Disadvantagess
Loss of interactivity (unsolvable) Good
Good for
for
One job can screw up other jobs, expensive
expensive hardware
hardware
need protection (solvable) and
and cheap
cheap humans
humans
• Advantages:
Interactivity
Sharing of resources
• Disadvantages:
Does not always provide
reasonable response time Good
Good for
for
cheap
cheap hardware
hardware
and
and expensive
expensive humans
humans
4
Phase 3: Personal Computing
• How things work Randy
One machine per person Wang
OS with multiprogramming and memory protection
• Advantages:
Interactivity
Good response times
• Disadvantages:
Sharing is harder Good
Good for
for
very
very cheap
cheap hardware
hardware
and
and expensive
expensive humans
humans
Good
Good for
for
very,
very, very
very cheap
cheap hardware
hardware
and
and expensive
expensive humans
humans
5
Layers of Abstraction
Appl Prog
User
process Stdio Library FILE * stream
Disk
System Calls
• Processor modes
user mode: can execute normal instructions and
access only user memory
supervisor mode: can also execute privileged
instructions and access all of memory (e.g., devices)
• System calls
user cannot execute privileged instructions
users must ask OS to execute them - system calls
system calls are often implemented using traps
OS gains control through trap, switches to supervisor
model, performs service, switches back to user mode,
and gives control back to user
6
System Calls
• Method by which user processes invoke kernel
services: “protected” procedure call
Appl Prog
fopen,fclose, printf,
fgetc, getchar,…
Stdio Library
user open, close, read,
kernel write, seek
File System
Interrupt-Driven Operation
• Everything OS does is interrupt-driven Randy
System calls use traps to interrupt Wang
7
Interrupt Processing
Randy
Wang
8
Read System Call
• Read call
nread = read(fd, buffer, n);
• In the caller
mov fd,%o0
mov buffer,%o1
mov n,%o2
call read; nop
mov %o0,nread
• Kernel-side implementation
sets the C bit if an error occurred
stores an error code in %o0
(see /usr/include/sys/errno.h)
9
Write System Call
int safe_write(int fd, char *buf, int nbytes)
{
int n;
char *p = buf;
char *q = buf + nbytes;
while (p < q) {
if ((n = write(fd, p, q-p)) > 0)
p += n;
else
perror(“safe_write:”);
}
return nbytes;
}
Buffered I/O
• Single-character I/O is usually too slow
int getchar(void) {
char c;
if (read(0, &c, 1) == 1)
return c;
return EOF;
}
10
Buffered I/O (cont)
• Solution: read a chunk and dole out as needed
int getchar(void) {
static char buf[1024];
static char *p;
static int n = 0;
11
Summary
• OS virtualizes machine
Provides each process with illusion of having whole
machine to itself
• OS provides services
Protection
Sharing of resources
Memory management
File systems
etc.
12