csnb334 advanced operating systems 2. introduction to linux semester 2, 2007/2008 lecturer: asma...

42
CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

Upload: theresa-wright

Post on 18-Jan-2018

224 views

Category:

Documents


0 download

DESCRIPTION

So, what's an OS? The simplest way to think of an operating system is in terms of hardware vs. software. The hardware consists of the parts of your computer that you can touch. Software is the logical, invisible part that makes your hardware do things. –Software is simply a series of instructions that your computer follows. –The software is typically packaged up in 'programs,' like OpenOffice, Microsoft Office, Firefox, etc...OpenOffice Firefox

TRANSCRIPT

Page 1: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

CSNB334 Advanced Operating Systems

2. Introduction to Linux

Semester 2, 2007/2008Lecturer: Asma Shakil

Page 2: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

What is Linux?• Is it different from Windows?• Before moving along, let's make sure that

we understand the difference between a GUI and an OS.– Windows is an operating system that includes

a GUI. – Mac OS X is an operating system that includes

a GUI. – Linux is an operating system that may or may

not include a GUI.

Page 3: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

So, what's an OS?• The simplest way to think of an operating

system is in terms of hardware vs. software. • The hardware consists of the parts of your

computer that you can touch. • Software is the logical, invisible part that

makes your hardware do things. – Software is simply a series of instructions that

your computer follows. – The software is typically packaged up in

'programs,' like OpenOffice, Microsoft Office, Firefox, etc...

Page 4: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

Still not answered – what is an OS?• Computers are really dumb and don't think. They blindly follow whatever

instructions they are given.

• That said, the operating system is a program that exists to control access to hardware and software resources like your memory (RAM), hard-drives, network, display, keyboard, mouse and so on.

– Thus, a program like OpenOffice needs to ask the OS which keys were pressed on the keyboard.

– OpenOffice also needs to ask the operation system to draw letters on the screen.

– OpenOffice also needs to ask the operating system to allocate memory, load and save information to the hard disk, and send printing instructions to your printer.

Page 5: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

So, how does OpenOffice do all that?

• The application communicates to the operating system using services or APIs (Application Programming Interface).

In turn, the operating system will rely on a 'kernel' to translate the service request to commands that the specific hardware of your machine can understand.

Page 7: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

Back to the original question: What is Linux?

The answer is: Linux is a kernel. No more. No less.

• It is the fundamental piece of code that controls the resources on your machine. – Windows has a kernel. – Mac OS X has a kernel. – Pretty much every operating system has a kernel.

Page 8: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

So, what’s inside a kernel?

Page 9: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

But, I have never seen a kernel!!

• End users do not interact directly with the kernel.• To allow users to interact with their computer, a set of

tools are provided with the operating system. – In the world of Windows, you are familiar with the Start

menu, the Control Panel and the Windows Explorer. – These are some of the operating system's tools that one

uses everyday to control the machine. – These programs are part of the operating system, but run

on top of the kernel. Such tools are sometimes called shell programs.

• A kernel provides API that computer programmers use to write these shell programs.

Page 10: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

So, is Linux not an OS?• Linux is only a kernel.• It does not include a shell or programs. • However, Linux is most commonly packaged

with a combination of “free” programs from another--as of yet incomplete--operating system called GNU.

• Because it borrows from the GNU project, you may hear people refer to the operating system as GNU/Linux.

Page 11: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

Another view…..

• With the combination of the GNU tools and the Linux kernel, we now have a complete operating system. Let’s visualize it…

Page 12: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

A little bit more on GNU………

• GNU: Open Source OS before Linux• The concept of “free” software

– Through 70s: Richard Stallman advocates “free” software

– “ free” as in “free speech” not “free beer” (not zero cost) - free to use, distribute (for a profit), and modify.

• 1984: Richard Stallman founded GNU– Goal: to produce free software

• GPL: ensure software freedom by copyright terms– GNU software: Unix-like programs (no kernel)

Page 13: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

Linux History• 1991: Finnish student Linus Torvalds started working on an

update of Minix (released in 1987 by Andrew S Tanenbaum).

• From the start, Linus asked volunteers on the Internet to help him develop Linux– People started using and publicizing Linux– A number of programmers joined the project– Linux Community: created by a loose worldwide community of

programmers collaborating mainly through the internet• From the start, the source code has been freely available

on the Internet• Linux provides a kernel for GNU software

– GNU has lots of user-space programs (like libraries, compilers, text editors and a Windowing System) but no kernel

– Linux kernel + GNU software = Linux distribution

Page 14: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

Linux Kernel Evolution• Linux kernel has undergone massive development in the last one and

a half decade :– 0 (Apr 1991): First e-mail from Linus about his new OS.– 0.01 (Sept 1991)– 1.0 (March 94), user: 100,000– 1.2 (March 95), user: 500,000– 2.0 (June 96), user: 1,500,000– 2.2 (January 99)– 2.4 (January 2001)– 2.6 (December 2003)

• Odd/Even versioning system– 2.4.x: stable kernels– 2.5.x: development kernels– Starting with the Linux 2.6.x series,

• there is no significance to even or odd numbers, with new feature development going on in the same kernel series. Linus Torvalds has stated that this will be the model for the foreseeable future.

• The version number of the Linux kernel has changed from a three-number versioning scheme to a four-number one. A.B.C[.D] (e.g. 2.2.1, 2.4.13 or 2.6.12.3).

Page 15: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

Linux Kernel versions

Page 16: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

The Kernel of Our Study• Using Linux on Windows

– The machines in our lab (BW-4-L15) use Windows.

– In order to use Linux in this kind of environment, we need to install multiple OS in a single computer. This has been done in the lab in 2 ways:-

• By using partitioning and a dual boot system– Linux distribution used is Xandross Linux. Kernel version is

2.6.11.• By running Linux in a virtual machine (using VMWare

Player) on a Windows system.– Linux distribution used is Ubuntu Linux. Kernel version is

2.6.31

Page 17: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

Recall…..

Page 18: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

Kernel basic facilities• The kernel's primary purpose is to manage the

computer's resources and allow other programs to run and use these resources

• The Central Processing Unit (CPU, the processor). – Kernel’s role - deciding at any time which of the many

running programs should be allocated to the processor or processors .

• The computer's memory. – Kernel’s role - deciding which memory each process can

use, and determining what to do when not enough is available.

• Input/Output (I/O) devices.– Kernel’s role - allocates requests from applications to

perform I/O to an appropriate device and provides convenient methods for using the device (typically abstracted to the point where the application does not need to know implementation details of the device).

Page 19: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

The Linux Kernel

• Linux kernel is written in C programming language

• It implements POSIX standards in terms of interface, commands etc, but the source code implementation is not UNIX.

Page 20: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

A. Frank - P. Weisberg

UNIX System Structure

Page 21: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

Traditional UNIX Kernel [Bach86]

Page 22: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

Kernel design decisions1. Monolithic

• Examples: Linux, Unix2. Microkernel

• Examples: Minix3. Hybrid

• Examples: Windows NT

Page 23: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

CPU modes • At a minimum, any CPU architecture offers two

distinct operating modes; • Kernel mode or supervisor mode or privileged

mode– In kernel mode, the CPU may perform any operation

allowed by its architecture; any instruction may be executed, any I/O operation initiated, any area of memory accessed, and so on

• User Mode or Slave Mode– certain restrictions on CPU operations are enforced

by the hardware. Typically, certain instructions are not permitted (especially those—including I/O operations—that could alter the global state of the machine), some memory areas cannot be accessed, etc.

Page 24: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

Monolithic Kernel

• As the name suggests – kernel is one unstructured program.

• It runs every basic system service like process and memory management, interrupt handling and I/O communication, file system, etc. as a single process in the same address space (in kernel space).

Page 25: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

Monolithic Operating System

Page 26: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

Microkernel System Structure• Move as much functionality as possible from

the kernel into “user” space.• Only a few essential functions in the kernel

– primitive memory management (address space)– I/O and interrupt management– Inter-Process Communication (IPC)– basic scheduling

• Other OS services are provided by processes running in user mode (referred to as servers) – device drivers, file system, virtual memory,

networking…

Page 27: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

A. Frank - P. Weisberg

Layered vs. Microkernel Architecture

Page 28: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

Pros and Cons of Monolithic kernels

• Pros– increase the

performance of the system.

• Cons :– the kernel size, – lack of extensibility – bad maintainability.

• a bug in a device driver might crash the entire system.

Page 29: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

Pros and Cons of Microkernel• Pros

– easier to maintain• Cons

– the large number of system calls and context switches slow down the system because they generate more overhead than plain function calls.

Page 30: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

The Linux Kernel• Linux kernel is monolithic

– Implemented in a single executable module• Data structure for any aspect of the kernel is available to

any other part of the kernel.• Design Philosophy:

– Main part of kernel should never change• To account for advanced devices (esp. bitmap displays and

networks)– Linux uses modules as containers to implement extensions to the

main part of the kernel.• Module – an independent software unit that can be

designed and implemented after the kernel and dynamically loaded into kernel space.

Page 31: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

Module and Device Driver Interface

Page 32: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

Introduction to System CallsLinux can be viewed at two levels:

◦user, or application, space - where the user applications are executed.

◦kernel space - where the Linux kernel exists.• Kernel space code has complete control over the machine;

– it can access any of the machine’s resources, such as memory, network adapters, and disk drives.

• User space code has limited access to system resources. – In order to read from a disk drive or write to the network, for example,

user code has to ask the kernel to perform the work on the user code’s behalf.

– If user code tries to carry out an operation which it doesn’t have permission to do, the microprocessor notifies the kernel,…

– A process (running in user mode) needing kernel services needs to do a system call.

Page 33: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

Introduction to System Calls

Page 34: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

The System Call Path

Page 35: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

System Call Stub Functions • The system call stub functions provide a high-level

language interface to a function whose main job is to generate the software interrupt (trap) needed to get the kernel's attention.

• These functions are often called wrappers. • The stub functions do the following:

– set up the parameters, – trap to the kernel, – check the return value when the kernel returns, and

• if no error: return immediately, else • if there is an error: set a global error number variable (called

"errno") and return a value of -1.

Page 36: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

x86 Linux read (glibc 2.1.3) read: push %ebx

mov 0x10(%esp,1),%edx ; put the 3 parms in registers

mov 0xc(%esp,1),%ecx mov 0x8(%esp,1),%ebx mov $0x3,%eax ; 3 is the syscall # for read int $0x80 ; trap to kernel pop %ebx cmp $0xfffff001,%eax ; check return value jae read_err

read_ret: ret ; return if OK.read_err:

Page 37: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

The dilemma?

• How can a user mode program switch the CPU to supervisor mode with the assurance that once the switch is done the CPU will be executing trusted kernel code and not untrusted user code?

Page 38: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

The trap instruction

• A trap instruction is not a privileged instruction, so any program can execute a trap.

• However, the destination of the branch instruction is predetermined by a set of addresses that are kept in supervisory space and that are configured to point to kernel code

Page 39: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

The Trap Instruction Operation

S

trap

Mode

Branch Table

Trusted Code

1

2

3

User Space Kernel Space

Page 40: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

Sequence of steps………….

The trap instruction does three things:1. Switch the CPU to supervisor mode.2. Look up a branch address in a kernel space table.3. Branch to a trusted OS function.

Because the trap instruction is compiled into the stub procedure, a user-space program cannot easily determine the trap’s destination address.

Also, it cannot branch directly to the kernel function – only through the system-provided stub.

Page 41: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

Actions performed in accomplishing a system call.

• For a system call, F, a stub procedure is used to invoke F (the stub is also named “F”).

• The stub is linked into the (user space) calling program. • When a process executes the call to F at runtime, control is

transferred to the stub procedure rather than directly to the kernel.

• The stub procedure validates the parameter values being passed to the kernel procedure, and could in principle authenticate the process that calls the stub procedure.

• Finally the stub procedure executes a trap instruction that switches the CPU to supervisory mode and branches (indirectly through a kernel table containing kernel function entry points) to the entry point for the target kernel function.

Page 42: CSNB334 Advanced Operating Systems 2. Introduction to Linux Semester 2, 2007/2008 Lecturer: Asma Shakil

Daemons• There is no special “kernel process” that executes the

kernel code.– Kernel code is actually induced by normal user processes.

• However, there are several user-transparent processes called daemons, that are started when a Linux machine is started, and which must exist for correct operation of the operating system. – They do not belong to a user.

• Used for functioning in areas such as for– Processing network packets– Logging of system and error messages etc.– Normally, filename ends with d, such as

• inetd, syslogd, crond, lpd etc– ps aux | more