Docsity

os.pdf, Study notes of Operating Systems

Partial preview of the text

Download os.pdf and more Study notes Operating Systems in PDF only on Docsity!

Operating Systems

CS 217

Operating System (OS)

• Provides each process with a virtual machine

Promises each program the illusion of having whole machine to itself

Hardware

OS Kernel

User

Process

User

Process

User

Process

User

Process

Operating System

• Coordinates access to physical resources

CPU, memory, disk, i/o devices, etc.

• Provides services

Protection Scheduling Memory management File systems Synchronization etc.

Hardware

OS Kernel

User

Process

User

Process

OS as Government

• Makes lives easy

Promises everyone whole machine (dedicated CPU, infinite memory, …) Provides standardized services (standard libraries, window systems, …)

• Makes lives fair

Arbitrates competing resource demands

• Makes lives safe

Prevent accidental or malicious damage by one program to another

Randy Wang

Phase 1: Batch Processing

• How things work

Sort jobs and batch those with similar needs 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) One job can screw up other jobs, need protection (solvable)

Good for expensive hardware and cheap humans

Good for expensive hardware and cheap humans

Randy Wang

Phase 2: Interactive Time-Sharing

• How things work

Multiple users per single machine OS with multiprogramming and memory protection

• Advantages:

Interactivity Sharing of resources

• Disadvantages:

Does not always provide reasonable response time Good for cheap hardware and expensive humans

Good for cheap hardware and expensive humans

Randy Wang

Phase 3: Personal Computing

• How things work

One machine per person OS with multiprogramming and memory protection

• Advantages:

Interactivity Good response times

• Disadvantages:

Sharing is harder Good for very cheap hardware and expensive humans

Good for very cheap hardware and expensive humans

Randy Wang

Phase 4: What Next?

• How will things work?

Many machines per person? Ubiquitous computing?

• What type of OS?

Good for very, very cheap hardware and expensive humans

Good for very, very cheap hardware and expensive humans

Randy Wang

System Calls

• Method by which user processes invoke kernel

services: “protected” procedure call

• Unix has ~150 system calls; see

man 2 intro /usr/include/syscall.h

File System

Stdio Library

Appl Prog

open, close, read, write, seek

fopen,fclose, printf, fgetc, getchar,… user kernel

Interrupt-Driven Operation

• Everything OS does is interrupt-driven

System calls use traps to interrupt

• An interrupt stops the execution dead in its tracks,

control is transferred to the OS

Saves the current execution context in memory (PC, registers, etc.) Figures out what caused the interrupt Executes a piece of code (interrupt handler) Re-loads execution context when done, and resumes execution

Randy Wang

Interrupt Processing

Randy Wang

System Calls (cont)

• Parameters passed…

in fixed registers in fixed memory locations in an argument block, w/ block’s address in a register on the stack

• Usually invoke system calls with trap instructions

ta 0 with parameters in %g1 (function), %o0..%o5, and on the stack

• Mechanism is highly machine-dependent

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; }

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;

if (n--) return *p++;

n = read(0, buf, sizeof(buf)); if (n <= 0) return EOF; n = 0; p = buf; return getchar(); }

Standard I/O Library

#define getc(p) (--(p)->_cnt >= 0?
(int)(*(unsigned char *)(p)->_ptr++) :
_filbuf(p))

typedef struct _iobuf { int _cnt; /* num chars left in buffer */ char _ptr; / ptr to next char in buffer */ char _base; / beginning of buffer / int _bufsize;/ size of buffer / short _flag; / open mode flags, etc. / char _file; / associated file descriptor */ } FILE; extern FILE *stdin, *stdout, *stderr;