Professor G. slides adapted by G. Sandoval for CS6233, from Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Also from Slides by -Gavitt
Copyright By PowCoder代写 加微信 powcoder
The kernel is a program at the core of the operating system.
User space is where ordinary programs run; must trap to the kernel (via a system call) to do anything that requires privilege
User space communicates with the kernel via system calls.
XV6 is a UNIX like operating system developed at MIT
Reimplements UNIX version 6 (1975)
Small enough to cover in a semester
Few system calls
Lacks many features (networking, users,
security features, etc. )
Simulating a PC
Real operating systems are difficult to debug
If something goes wrong, the whole machine crashes
At a minimum you need two computers
Simulating a PC
To get around this, we will use a PC emulator called QEMU
QEMU simulated PC hardware in software
We will call the machine doing the simulating the “host” and the machine being simulated the “guest”
We will briefly cover some XV6 aspects: ▪ Command Line arguments
▪ File I/O
▪ Pipes and redirection
When we launch an executable the OS sets up it’s environment to run
For traditional UNIX style C Programs this includes a list of command arguments
Command line args are passed to the main function
intmain(intargc,char *argv[])
Example: suppose you execute
$ wc README
argv[0] = ? argv[1] = ? argv[2] = ?
Example: suppose you execute
$ wc README
argv[0] = “wc” argv[1] = “README”
argv[2] = NOTHING argc = 2
At a Minimum you will need
▪ Types.h – basic data types used in XV6
▪ User.h – defines the API accessible by programs running in user mode
If you want to find out what to include to get a particular function: grep function *.h
A File Descriptor is a small integer representing a “kernel managed” object that a process can read/write to.
A process can obtain a FD by: opening file, directory, or device or creating a pipe.
The FD interface abstracts away differences between files, pipes and devices making them all look like a stream of bytes.
The shell always ensures that any process has 3 file descriptors open:
The 3 standard file descriptors: ▪ stdin(0),
▪ stdout(1), ▪ stderr(2)
In XV6 you have the open syscall:
The mode argument is a set of flags (defined
in fcntl.h): O_RDONLY, O_WRONLY, O_RDWR, O_CREATE
Returns a file descriptor, or -1 if there is an error (e.g., file not found)
Once you have open file descriptor, it can be used with various other APIs:
int printf(int fd, char *format, …) int read(int fd, void *buf, int n) int write(int fd, void *buf, int n)
Data to read or write
Once you have open file descriptor, it can be used with various other APIs:
int printf(int fd, char *format, …) int read(int fd, void *buf, int n) int write(int fd, void *buf, int n)
Once you have open file descriptor, it can be used with various other APIs:
int printf(int fd, char *format, …) int read(int fd, void *buf, int n) int write(int fd, void *buf, int n)
Number of bytes to read or write
At the command line, a program’s std input and output can be redirected to or from a file
▪ Input with the ‘<‘ character
▪ Output with the ‘>’ character
Example: search for the word ‘the’ in the file README
▪ Grep the README
Now save the results to a file “found.txt”
▪ Grep the < README > found.txt
Takes place of stdin Takes place of stdout 21
Since input and output use the general notion of a file descriptor, we can do interesting things.
This way of connecting programs is called a pipe –it hooks up the output of one program to the input of another
Examples:
▪ Count the lines, words, and characters in a file:
▪ cat README | wc
▪ Search the dictionary for words containing “cat” and return
only the first 10
▪grep cat /usr/share/dict/words | head
cat doesn’t know where it’s reading from
Use of file descriptors and the convention that fd 0 is input and fd 1 is output allows for a simple implementation of cat.
When using redirection the shell takes care of this
User and kernel space Processes
System Calls
Address spaces
Files & file descriptors
Users, groups, permissions (UNIX)
The kernel is where we’ll spend most of our attention
User space is where ordinary programs run; must trap to the kernel (via a system call) to do anything that requires privilege
distinguishes several types of operating systems Some types proposed over the years:
▪ Monolithic
▪ Microkernel ▪Virtual machine
Monolithic kernels are effectively one large program; any piece of the kernel can talk to any other by simple function calls
This is the most common kind in use today – Windows, Linux, and OS X are all monolithic
Downside: any mistake in the kernel can bring down the entire machine
Core idea: bugs per lines of code seems to be roughly constant
fewer bugs? write less code!
Corollary: make the kernel as small as possible
OS is structured as a collection of servers running in user space
Kernel-mode component just does scheduling and inter-process communication
Stability benefits: your video driver can crash without bringing down the whole OS
Communication between processes requires a context switch – and this is expensive on current hardware
Lots of work has been done to make this. But it still appears to be inherently slower than a monolithic kernel
Some modern OSes do have microkernel-like aspects Windows
▪ Some subsystems (e.g. the local security manager, LSASS) are regular user processes
▪ In early versions of Windows NT (up until 4.0), the graphics subsystem was entirely in userspace
▪ But this was later moved into the kernel (win32k.sys) for performance reasons
Definition: A program in execution.
A process is the basic unit of execution in an
Operating System
Each process gets its own CPU context and
The operating system switches between them, saving and restoring their context
Each process gets its own memory and cannot access the memory of others
▪Except through special mechanisms such as shared memory regions
▪This is enforced by the Operating System
Two ways of accomplishing Isolation of Address Spaces:
▪Naming: make sure that a process has no way of referencing the memory of another process; i.e., it can’t form a memory address that refers to memory outside its own space
▪Trapping: ensure that every access to someone else’s memory causes a trap to the kernel
One of the most fundamental units in most operating systems
Usually just a plain array of bytes with some associated metadata
Programs can obtain a reference to a file from the operating system (called a file descriptor or file handle)
All access to the file data is done by asking the OS and providing it the descriptor
UNIX takes the mentality that “everything’s a file”
Some hardware resources can be accessed by user programs by operating on files instead
For example: in Linux, /dev/ttyS0 is a file that gives you access to the serial port, /dev/hda is a file that refers to the actual hard drive
Even program input and output is treated as a file: ▪ Standard input is assigned to file descriptor 0
▪ Standard output is assigned to file descriptor 1 ▪ Standard error is assigned to file descriptor 2
We may want to allow multiple people to use the same system
So the OS needs to track the owners of resources such as files
It may also be convenient to have files accessible to a group of users
Each file has an owner, a group, and permissions
Permissions divided into three sections: user, group, and others
Permissions Owner Group
-rw-r–r– -rwxr-xr-x -rw-r–r–
1 moyix staff 1 moyix staff 1 moyix staff
8388608 Sep 1 18:00 vga.vram 8496 Sep 14 07:34 x
290 Sep 14 07:34 x.c
-rwxr-xr-x
User GroupOther
If the file is owned by the current user, use the user permissions to determine if access is
Otherwise, if the user is a member of the group of the file, use the group permissions
Finally, anyone else will be allowed access according to the other permissions
In this example, the file’s owner can read, write, and execute the file; the group and others can only read and execute it but not modify it
When you see a – (dash) in user, group or owner it means the particular permission has NOT been granted.
Use the chmod command to change file permissions. Take a look at the file first. At the shell prompt type:
ls –l foo.txt This displays: Now type:
ls -l foo.txt
-rw-rw-r– 1 user user 150 Mar 19 08:08 foo.txt
chmod o+w foo.txt
-rw-rw-rw- 1 user user 150 Mar 19 08:08 foo.txt
Identities
u — the user who owns the file (that is, the owner) g — the group to which the user belongs
o — others (not the owner or the owner’s group)
a — everyone or all (u, g, and o)
Permissions
r — read access
w — write access
x — execute access
+ — adds the permission
– — removes the permission
= — makes it the only permission
Abstractions that OS provides to interact with it. Are usually machine dependent
Written in assembly code
Library procedures are used in C/C++.
Let’s trace the read system call:
▪ Count = read(fd, buffer, nbytes)
The 11 steps in making the system call
read(fd, buffer, nbytes).
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com