Download slides.01.pdf

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Acorn MOS wikipedia , lookup

Copland (operating system) wikipedia , lookup

Security-focused operating system wikipedia , lookup

Distributed operating system wikipedia , lookup

Library (computing) wikipedia , lookup

MTS system architecture wikipedia , lookup

Commodore DOS wikipedia , lookup

RSTS/E wikipedia , lookup

Windows NT startup process wikipedia , lookup

OS 2200 wikipedia , lookup

Process management (computing) wikipedia , lookup

Plan 9 from Bell Labs wikipedia , lookup

Batch file wikipedia , lookup

DNIX wikipedia , lookup

Spring (operating system) wikipedia , lookup

Burroughs MCP wikipedia , lookup

CP/M wikipedia , lookup

VS/9 wikipedia , lookup

Unix security wikipedia , lookup

Transcript
Operating Systems
Design and Implementation
Chapter 01
(version January 30, 2008)
Melanie Rieback
Vrije Universiteit Amsterdam, Faculty of Sciences
Dept. Computer Science
Room R4.23. Tel: (020) 598 7874
E-mail: [email protected], URL:
www.cs.vu.nl/∼melanie/
01
02
03
04
05
00 – 1
Introduction
Processes
Input/Output
Memory Management
File Systems
/
Overview
• What’s an operating system?
• Concepts
• System calls
• Structure
01 – 1
Introduction/
Position Operating System
Banking
system
Airline
reservation
Web
browser
Compilers
Editors
Command
interpreter
Application programs
System
programs
Operating system
Machine language
Microarchitecture
Hardware
Physical devices
Question: What distinguishes compilers, editors, etc.
from operating systems?
01 – 2
Introduction/1.1 What is an Operating System
Abstraction over Hardware (1/2)
#define FDC_STATUS
#define FDC_DATA
0x3F4
0x3F5
/* status register */
/* data register
*/
#define MASTER
#define DIRECTION
0x80
0x40
/* value: master? */
/* value: R/W?
*/
PRIVATE void fdc_out(val)
int val;
{
struct milli_state ms;
if (need_reset) return;
milli_start(&ms);
while ((in_byte(FDC_STATUS) &
(MASTER | DIRECTION)) != (MASTER | 0)) {
if (milli_elapsed(&ms) >= TIMEOUT) {
/* Controller is not listening. Hit it over the head. */
need_reset = TRUE;
return;
}
}
out_byte(FDC_DATA, val);
}
01 – 3
Introduction/1.1 What is an Operating System
Abstraction over Hardware (2/2)
PRIVATE int
(fp, tp)
struct floppy *fp; /* pointer to the drive struct */
struct trans *tp; /* pointer to the transfer struct */
{
int r, s;
...
if(...){
fdc_out(f_opcode == DEV_WRITE ? FDC_WRITE : FDC_READ);
fdc_out((fp->fl_head << 2) | f_drive);
fdc_out(fp->fl_cylinder);
fdc_out(fp->fl_head);
fdc_out(fp->fl_sector);
fdc_out(SECTOR_SIZE_CODE);
fdc_out(f_sectors);
fdc_out(gap[d]);
/* sector gap */
fdc_out(DTL);
/* data length */
}
/* Get controller status and check for errors. */
r = fdc_results();
/* Compare actual numbers of sectors transferred
with expected number. */
s = (f_results[ST_CYL] - fp->fl_cylinder)
* NR_HEADS * f_sectors;
s += (f_results[ST_HEAD] - fp->fl_head) * f_sectors;
s += (f_results[ST_SEC] - fp->fl_sector);
if ((s << SECTOR_SHIFT) != tp->tr_count)
return(ERR_TRANSFER);
/* This sector is next for I/O: */
fp->fl_sector = f_results[ST_SEC];
return(OK);
}
01 – 4
Introduction/1.1 What is an Operating System
Views on Operating Systems
Idea: Operating system can be seen as: (1) virtual
machine, extending the hardware functionality, or (2) bunch
of software that manages the hardware resources.
Virtual machine:
• hide all the messy details of programming the hardware.
• construct layers of software that provide more and
more functionality (f transfer() over fdc out()).
Resource manager:
• software protects against simultaneous accesses
and usage of resources (one user of the floppy
disk at a time)
• fair share of the resources (scheduling)
• account for using resources.
01 – 5
Introduction/1.1 What is an Operating System
How to View an Operating System
Basics: An operating system appears to applications
and users as a library of functions ⇒ system calls.
A group of system calls stand for a service that the
operating system has to offer. In MINIX there are basically two groups:
• System calls related to managing files.
• System calls related to managing processes.
Primitive model: a process represents a user or application and executes a program on behalf of its owner.
Data involved in this processing is retrieved from and
stored on files. Data in files persist over processes.
01 – 6
Introduction/1.3 Operating System Concepts
Processes
Problem: the execution of a program requires the allocation of resources (CPU, disks, etc) needed by that
program. We therefore need to keep track of these allocations.
Model: however, a program is just a series of instructions: you can’t allocate anything to that. Instead, we
construct a representant for that program during its
execution ⇒ process.
A process is an invention by an operating system: it is
used as a placeholder for executing programs so that
we can keep a precise account of how program execution is affecting the use of hardware and software
resources.
process = program in execution
01 – 7
Introduction/1.3 Operating System Concepts
Multiple Processes
Idea: An operating system can support more than one
process at a time: processes can be suspended and
(re)activated.
Problem: how do we get multiple processes ⇒ allow
a process to create another process.
Example: When you’re sitting at the terminal, a small
program called a command shell is executed (yes, by
means of a process). Suppose you issue the command:
some_program
Then a process is created for the execution of
some program:
A
B
D
01 – 8
E
C
F
Introduction/1.3 Operating System Concepts
Files (1/2)
Problem: We need to have a means for storing and
retrieving data associated with a program, independent of whether a process is executing that program.
Model: A file is an abstraction of a real storage device
(e.g. disk): you can read/write data from/to a file by
providing (1) a position in that file, and (2) the amount
of data to transfer.
Organization: a file is contained in a directory. A
directory holds an identifier for each file it contains.
These identifiers need to be stored as well ⇒ a directory is a file.
01 – 9
Introduction/1.3 Operating System Concepts
Files (1/2)
Root directory
Students
Robbert
Matty
Faculty
Leo
Prof. Brown
Courses
CS101
Papers
CS105
Prof. Green
Grants
Prof. White
Committees
SOSP
COST-11
Files
01 – 10
Introduction/1.3 Operating System Concepts
Files – Concepts (1/2)
• Files are organized hierarchically, the top is called
root directory.
• Files are referenced through a pathname: a concatenation of directory names, ending with the
filename:
– an absolute pathname starts at the root directory, for example:
/home/steen/edu/os/chp.01/sheets.tex
– a relative pathname starts at the current working directory. Assume that the working directory is /home/steen/edu. Same file as above:
os/chp.01/sheets.tex
01 – 11
Introduction/1.3 Operating System Concepts
Files – Concepts (2/2)
• Files are protected:
– Each file can be accessed by (1) its owner,
(2) a group member, or (3) everyone else.
– Each file allows access for reading (r), writing
(w), or execution (x).
-rwxr-x--x steen infstaff 144649 Aug 23 doit
Question: What does everyone else actually mean?
01 – 12
Introduction/1.3 Operating System Concepts
File System Mounting (1/2)
File systems are mapped onto real storage devices ⇒
a storage device may contain a file system.
Problem: suppose you have several storage devices
(USB sticks, hard disks, CD-ROM, etc.), each currently containing a file system. How do you refer to
them?
In Windows: use a drive specification:
D:\edu\bs\coll01\sheets.tex
Note: Windows NTFS supports mounting as well.
01 – 13
Introduction/1.3 Operating System Concepts
File System Mounting (2/2)
In UNIX-like systems: mount a filesystem:
• Each real storage device is known to the operating system.
• A file system on storage device DEV can be attached to the root file system.
• A mounted file system appears as a directory in
the root file system ⇒ the actual device is transparent.
mount -t iso9660 /dev/hdc /cdrom
Root
a
c
Drive 0
b
x
d
a
c
(a)
01 – 14
y
d
b
x
y
(b)
Introduction/1.3 Operating System Concepts
Special Files (1/2)
Idea: If storage devices can contain a file system, this
means they can store bytes. Couldn’t we just read,
say, the kth byte from a device regardless of the file
system it contains?
Model: Represent real storage devices through special files:
• block special files: all operations are performed
in units of blocks of bytes. Examples: hard disk,
floppy disk, tapes.
• character special files: all operations are performed in units of bytes. Examples: terminals,
network interfaces, line printer.
Note: special files are actually used as an abstraction
over all peripheral devices, not just those for storing
data.
01 – 15
Introduction/1.3 Operating System Concepts
Special Files (2/2)
Associated with each special file:
• major device number: denotes a class of similar
devices.
• minor device number: denotes a particular (real)
device.
brw-r----brw-r----brw-r----brw-r----brw-r----crw-rw---crw-rw---crw-rw---crw-rw----
01 – 16
1
1
1
1
1
1
1
1
1
root
root
root
root
root
root
root
root
root
disk
disk
disk
disk
disk
lp
lp
lp
lp
3,
3,
3,
3,
3,
99,
99,
99,
99,
1
2
3
4
5
0
1
2
3
Jan
Jan
Jan
Jan
Jan
Jan
Jan
Jan
Jan
22
22
22
22
22
22
22
22
22
14:07
14:07
14:07
14:07
14:07
14:07
14:07
14:07
14:07
hda1
hda2
hda3
hda4
hda5
parport0
parport1
parport2
parport3
Introduction/1.3 Operating System Concepts
System Calls
Process Management (1/2)
Example: consider a very simple command shell:
• Wait for a user to type in a command, possibly
with some parameters. The command should correspond with a program name (i.e. executable file).
• Start a process that executes the program, i.e.
loads the specified file into memory, and starts
executing it.
• Wait until the child process has finished.
01 – 17
Introduction/1.4 System Calls
System Calls
Process Management (2/2)
Needed: (1) process creation, (2) have process execute a file, (3) have a process wait for a child to finish.
while(TRUE){
read_command(command, parameters);
pid = fork();
}
if( pid != 0 ){ /* parent process */
waitpid( pid, &status, 0 );
}
else{ /* child process */
execve(command, parameters, 0);
exit(0);
}
: Create a child process identical to the parent
: Wait for a (specific) child to terminate
: Replace a process’s core image
: Terminate process execution
Question: Does it make sense to call exit(0)?
01 – 18
Introduction/1.4 System Calls
System Calls – Signals
Model: Processes sometimes need to be interrupted
during their execution:
• Have some process send a signal to the process
that needs to be interrupted,
• An interrupted process can catch a signal by installing a handler.
int handler(){
printf("Going down...\n");
exit(0);
}
main(){
signal(SIGALRM, handler);
alarm(60);
while(TRUE) printf(".");
}
Question: Who’s sending the signal SIGALRM?
Question: What happens if we remove exit(0)?
01 – 19
Introduction/1.4 System Calls
System Calls – Files
• : create a regular file, or possibly reinitialize
an existing one.
: create a special file (can only be done by
•
superuser).
•
: open a file for reading or writing, returns a
file descriptor.
• : close an opened file.
,
: read/write data from/to a specify
•
file.
• : set the file pointer to a specified position.
Question: Aren’t we missing something here?
01 – 20
Introduction/1.4 System Calls
System Calls – Directories
• : create a hard link to another file - appears
as just another file, but is actually just a pointer.
: remove a (hard) link to a file. As long as
•
there are links to a file, the file remains. Otherwise, it is removed permanently.
Question: How do we remove files?
01 – 21
Introduction/1.4 System Calls
System Calls – Example (1/2)
Basic idea: We want to set up a small system for
letting two processes exchange information across a
pipe:
Process
Process
Pipe
A
B
• Pipes are implemented using file mechanisms;
one process writes data to the file, the other reads
from the file.
• Normally, processes read data from standard input (console), and write data to standard output
(console). We need to redirect standard I/O to
our pipe system.
• Files have filedescriptors; 0 is used for standard
input, 1 for standard output.
• Solution: use dup(fd) which returns the lowestavailable file descriptor, and associates it with the
same file as identified by fd.
01 – 22
Introduction/1.4 System Calls
System Calls – Example (2/2)
#define STD_INPUT 0
#define STD_OUTPUT 1
void pipeline(char* process1, char* process2)
{
int fd[2]; /* array to hold two file descriptors */
pipe(fd);
}
/* create a pipe fd[1] → fd[0] */
if( fork() != 0 ){ /* parent */
close(fd[0]);
/* no need to read from pipe
close(STD_OUTPUT); /* prepare new std_output
dup(fd[1]);
/* assign std_out to fd[1]
close(fd[1]);
/* fd[1] no longer needed
execl(process1, process1, 0);
}
else{ /* child */
close(fd[1]);
/* no need to write to pipe
close(STD_INPUT); /* prepare new std_input
dup(fd[0]);
/* assign std_in to fd[0]
close(fd[0]);
/* fd[0] no longer needed
execl(process2, process2, 0);
}
01 – 23
*/
*/
*/
*/
*/
*/
*/
*/
Introduction/1.4 System Calls
OS Structure: Monolithic (1/2)
Address
0xFFFFFFFF
Return to caller
Trap to the kernel
5 Put code for read in register
10
4
User space
Increment SP
Call read
3 Push fd
2 Push &buffer
1 Push nbytes
11
User program
calling read
6
Kernel space
(Operating system)
Dispatch
Library
procedure
read
9
7
8
Sys call
handler
0
1-4:
5-6:
7-9:
10-11:
Prepare, and call library routine read(fd,buffer,nbytes).
Prepare, and switch to kernel
Lookup & sys. handler, and return to user mode
Return to program and pop the stack
01 – 24
Introduction/1.5 Operating System Structure
OS Structure: Monolithic (2/2)
Main
procedure
Service
procedures
Utility
procedures
• User programs form a top layer of programs that
can call OS service procedures through system
calls
• Middle layer is collection of programs implementing system calls
• Lowest layer is collection of (small) utility programs
that are used to implement service procedures
01 – 25
Introduction/1.5 Operating System Structure
Virtual Machine
Basic idea: Provide a bare-bones layer on top of the
hardware and build different operating systems on top
of that. Essentially implement the OS system call interface for different OSes.
Virtual 370s
System calls here
I/O instructions here
Trap here
CMS
CMS
CMS
Trap here
VM/370
370 Bare hardware
• Windows emulation under Linux (VMware)
• Linux emulation under Windows (VMware)
• FreeBSD emulation under Linux (Xen paravirtualization, meaning that the underlying system is not completely emulated for performance
reasons)
01 – 26
Introduction/1.5 Operating System Structure
Client/Server System
Simple model: Organize all services procedures as
programs that are run in separate processes (i.e., outside the OS kernel). Service calls are translated to
procedure calls that need to go through the OS kernel.
Client
process
Client
process
Process
server
Terminal
server
File
server
Memory
server
User mode
Kernel mode
Microkernel
Client obtains
service by
sending messages
to server processes
Machine 1
Machine 2
Machine 3
Machine 4
Client
File server
Process server
Terminal server
Kernel
Kernel
Kernel
Kernel
Network
Message from
client to server
Question: What’s the big advantage here?
01 – 27
Introduction/1.5 Operating System Structure