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: melanie@cs.vu.nl, URL: www.cs.vu.nl/ ∼ melanie/ 01 Introduction 02 Processes 03 Input/Output 04 Memory Management 05 File Systems 00 – 1 / Overview • What’s an operating system? • Concepts • System calls • Structure 01 – 1 Introduction/
Position Operating System Banking� Airline� Web� Application programs system reservation browser Command� Compilers Editors interpreter System� programs Operating system Machine language Hardware Microarchitecture 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 0x3F4 /* status register */ #define FDC_DATA 0x3F5 /* data register */ #define MASTER 0x80 /* value: master? */ #define DIRECTION 0x40 /* 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) �✂✁☎✄✝✆✟✞✡✠☞☛☎�✝✌✍✆ (fp, tp) PRIVATE int 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 hard- ware. • 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 ba- sically two groups: • System calls related to managing files. • System calls related to managing processes. Primitive model: a process represents a user or ap- plication 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 al- location of resources (CPU, disks, etc) needed by that program. We therefore need to keep track of these al- locations. Model: however, a program is just a series of instruc- tions: 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 exe- cution 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 com- mand: ✎✑✏✂✒✓✏✂✔✖✕ some_program Then a process is created for the execution of some program : A B C D E F 01 – 8 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, indepen- dent 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 direc- tory is a file . 01 – 9 Introduction/1.3 Operating System Concepts
Files (1/2) Root directory Students Faculty Robbert Matty Leo Prof. Brown Prof. Green Prof. White Courses Papers Grants Committees CS101 CS105 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 con- catenation of directory names, ending with the filename: – an absolute pathname starts at the root direc- tory, for example: /home/steen/edu/os/chp.01/sheets.tex – a relative pathname starts at the current work- ing directory . Assume that the working direc- tory 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 cur- rently 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 operat- ing system. • A file system on storage device DEV can be at- tached to the root file system . • A mounted file system appears as a directory in the root file system ⇒ the actual device is trans- parent. ✗✙✘✝✚✛✘✂✜✣✢ mount -t iso9660 /dev/hdc /cdrom Root Drive 0 a b x y a b c d c d x y (a) (b) 01 – 14 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 k th byte from a device regardless of the file system it contains? Model: Represent real storage devices through spe- cial 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 per- formed 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----- 1 root disk 3, 1 Jan 22 14:07 hda1 brw-r----- 1 root disk 3, 2 Jan 22 14:07 hda2 brw-r----- 1 root disk 3, 3 Jan 22 14:07 hda3 brw-r----- 1 root disk 3, 4 Jan 22 14:07 hda4 brw-r----- 1 root disk 3, 5 Jan 22 14:07 hda5 crw-rw---- 1 root lp 99, 0 Jan 22 14:07 parport0 crw-rw---- 1 root lp 99, 1 Jan 22 14:07 parport1 crw-rw---- 1 root lp 99, 2 Jan 22 14:07 parport2 crw-rw---- 1 root lp 99, 3 Jan 22 14:07 parport3 01 – 16 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 cor- respond 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
Recommend
More recommend