0% found this document useful (0 votes)
16 views

CH02 - Operating-System Structures-上課版

Uploaded by

2dbf02
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

CH02 - Operating-System Structures-上課版

Uploaded by

2dbf02
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 96

Chapter 2

Operating-System Structures
Outline

• Operating System Services


• User and Operating-System Interface
• System Calls
• System Services
• Linkers and Loaders
• Why Applications Are Operating-System Specific
• Operating-System Design and Implementation
• Operating-System Structure
• Building and Booting an Operating System
• Operating System Debugging
Outline

• Operating System Services


• User and Operating-System Interface
• System Calls
• System Services
• Linkers and Loaders
• Why Applications Are Operating-System Specific
• Operating-System Design and Implementation
• Operating-System Structure
• Building and Booting an Operating System
• Operating System Debugging
Operating System Services

• OS provides services to programs and users


– E.g.,
• Program execution
• Manage hardware
• ……
Program 1 Program 2
User Mode
Kernel Mode

OS

hardware 1 hardware 2 hardware 3


Outline

• Operating System Services


• Operating-System Interface to Users
• System Calls
• System Services
• Linkers and Loaders
• Why Applications Are Operating-System Specific
• Operating-System Design and Implementation
• Operating-System Structure
• Building and Booting an Operating System
• Operating System Debugging
User Operating System Interface

• For users, fundamental approaches to interface with the OS (see


the following slide)

– Command-line interface (CLI) (or command interpreter)


– Batch interface
– Graphical User Interface (GUI)
– Touch-Screen Interface
user 1 user 2

User Mode
Kernel Mode

OS

hardware 1 hardware 2 hardware 3


User Operating System Interface :
CLI
• Command interpreter (CLI) allows directly typing commands
– Fetch a command from user and execute

– Implemented as a system program


• Not part of OS

– In Linux, multiple choices are implemented – shells


• Bourne shell (bash), C shell (csh), Korn shell (ksh)
Shell Command Interpreter
User

Shell

OS

hardware
User Operating System Interface :
Batch Interfaces

cc -g -c menu.c
cc -g -o driver driver.c menu.o
driver < test_data > test_out
lpr -PthePrinter test_out
tar cvf driver_test.tar menu.c driver.c test_data test_out
uuencode driver_test.tar driver_test.tar >driver_test.encode

A “shell script” batch File

from “Operating Systems by NUTT”


User Operating System Interface : GUI

• User-friendly desktop interface


– Mouse-based window-and-menu

– Invented at Xerox PARC


• The first GUI in 1973.
User Operating System Interface :
Touchscreen Interfaces
• Touchscreen devices require new interfaces
– Actions and selection based on gestures (手勢)
– Virtual keyboard for text entry

• Voice commands
Outline

• Operating System Services


• Operating-System Interface to Users
• System Calls
• System Services
• Linkers and Loaders
• Why Applications Are Operating-System Specific
• Operating-System Design and Implementation
• Operating-System Structure
• Building and Booting an Operating System
• Operating System Debugging
Before discussing system call, 我們先複習Library
補充: Example of Programs Using Libraries in Linux
int add(int arg) { a.c
arg++;
return arg;
}
int sub(int arg) {
arg--;
return arg;
}

int abc() {

}

int sum(int arg1, int arg2) b.c


{
return (arg1+arg2);
}
int xyz()
{
……
}
17
補充: Example of Programs Using Libraries in Linux
(Cont.)

libtmp.a

a.c Compiler a.o link utility a.o

Compiler b.o
b.c b.o

Creating a library called libtmp.a

18
補充: Example of Programs Using Libraries in Linux
(Cont.)
main.c int add(int arg) { a.c
arg++;
#include <stdio.h> return arg;
}
extern int add(int); int sub(int arg) {
extern int sub(int); arg--;
extern int sum(int, int); return arg;
}
main()
{ int abc() {
int ret1, ret2, ret3; …
}
ret1 = add(5);
ret2 = sub(5);
ret3 = sum(ret1, ret2); int sum(int arg1, int arg2) b.c
{
printf("\n ret from add()= %d", ret1); return (arg1+arg2);
printf("\n ret from sub()= %d", ret2); }
printf("\n ret from sum()= %d", ret3); int xyz()
} {
……
}
19
補充: Example of Programs Using Libraries in
Linux (Cont.)
Object file executable file
Source file

main.c Compiler main.o main.o

linker
libtmp.a libtmp.a

a.o a.o

b.o b.o

Link with libtmp.a to create the executable file 20


In fact, only add(), sub() and sum() three functions are
extracted from the libtmp.a library

補充:Link主要的工作是resolve external
references (shown later)
補充: Example of Programs Using Libraries in Linux
(Cont.)
• Compile source programs to object programs
– gcc –c main.c
– gcc –c a.c
– gcc –c b.c
• Create/add/replace object programs into library
– ar –r libtmp.a a.o b.o
• List contents of library
– ar –t libtmp.a
• Delete object programs from library
– ar –d libtmp.a b.o

22
補充: Example of Programs Using Libraries in Linux
(Cont.)
• Compile source programs to object programs
– gcc –c main.c
• Using library in programs
– Ex. gcc main.o libtmp.a –o prog
– Ex. gcc main.o –L. –ltmp –o prog
• Linking editor
– ld under SunOS
• References
– man gcc under SunOS
– man ar under SunOS
– man ld under SunOS
You will learn how to create a library in OS
Lab course.
複習結束
System Calls

• System calls
– Functions provided by OS to provide service to user applications

– Example: open(), read(), write(), close()……

– Even simple program (see the following example) may make heavy use of the system
calls
Example
#include<stdio.h> #include<sys/types.h> #include<sys/stat.h>
#include<fcntl.h> #include<unistd.h>
#include<stdlib.h> //for exit()
#include <string.h> //for strlen()

int main(){
int fd;
fd = open("myfile.txt", O_CREAT | O_WRONLY, 0600);
if (fd < 0){
printf("Failed to open the fild.\n");exit(1); }
int size;
size = write(fd, "e", strlen("e") );
close(fd);

printf("length of write data=%d \n", strlen("e"));


printf("Number of bytes written on success=%d \n\n", size);

exit(0);
27
}
按照程式執行的規則,當呼叫open()時,
程式應該會跳到對應的open() function。
這個例子:open()就是一個system call

問題:open() function在哪裡?
Program 1 Program 2
open()… read()…
User Mode
Kernel Mode
open(), read()

open()
{
….
}
read() OS
{

}

Storage
Examples of Windows and Unix System Calls

問題:為什麼system call介面不一樣?
System Calls (Cont.)
• Typically, a number is associated with each system call
– Maintains a table indexed according to these numbers
– See the following slide
System Calls (Cont.)

int 0x 10; // read();

CPU
mode = 1;
0;

…..

trap

OS
APIs

• However, system calls are often accessed indirectly via a Application


Program Interface (API)
– Rather than directly calling the system call

– fopen(), fread(), fwrite(), printf()….are APIs

– See the following slide


Program 2
Program 1
fopen();

fopen() {

open(); C library

}
User Mode
Kernel Mode

open()
{
….
}
read() OS
{

}

Storage
C program invoking fopen () library call, which in turn calls open () system call
Another Example
#include <stdio.h>
int main()
{

printf(“Greeting”);
Application

}

printf()
{

int 0x80;
C Library

}

write()
{
… OS


}

Hardware Hardware 35
在上述的例子,fopen(), fread(),
printf()……就是API
System Call and APIs

Application Program

Library API Function


(Ex:C Library, Windows Library)

OS Function

Hardware
複習:組合語言(Linking to a Library)
• The two LIB files: irvine32.lib, and kernel32.lib
– The irvine32.lib is provided by book’s author
– The kernel32.lib is part of the Microsoft Win32 Software Development Kit (SDK)

links
Your program Irvine32.lib
to
links to
can link to
kernel32.lib

executes

kernel32.dll
38
System Call and APIs
APIs (Cont.)

• API: a set of functions that can be called by the programs


– Three most common APIs
• Win32 API for Windows,
• POSIX API for POSIX-based systems
– All versions of UNIX, Linux, and Mac OS X
• Java API for the Java virtual machine (JVM)
– Supported by library
Example: Standard C Library
C programming uses functions defined in libc, which
provides a wrapper of system calls.

C program invoking printf() library call, which in turn calls write() system call
Example: Standard C Library
Some of POSIX APIs
Calling System Call and APIs: Example
#include<stdio.h> #include<sys/types.h> #include<sys/stat.h>
#include<fcntl.h> #include<unistd.h>
#include<stdlib.h> //for exit()
#include <string.h> //for strlen()

int main(){
int fd;
fd = open("myfile.txt", O_CREAT | O_WRONLY, 0600);
if (fd < 0){
printf("Failed to open the fild.\n");exit(1); }
int size;
size = write(fd, "e", strlen("e") );
close(fd);

printf("length of write data=%d \n", strlen("e"));


printf("Number of bytes written on success=%d \n\n", size);

exit(0);
44
}
APIs (Cont.)

• Why use APIs rather than system calls?


– Portability
• Application using APIs can be compiled and run on another platform using the same API
• See the following three slides
– Easier to use
• System calls often be more detailed and difficult to use than APIs
– E.g., printf() function provides output formatting, whereas the write() system call just outputs a
block of bytes
• Programmer knows nothing about how the system call is implemented
– Details of system calls are hidden by APIs
– These details are managed by library
Examples of Windows and Unix System Calls
Portability (Cont.)
… Change to Linux …
CreateProcess() fork()
… Need to modify …
source code

CreateProcess() fork()
since system call interface
Windows is different Linux
Portability
… …
pthread_create() Change to VxWorks pthread_create()
… …
Don’t need to modify
source code
POSIX Library POSIX Library

fork() new_thread()
even system call interface
Linux is different VxWorks
Summary
問題:當呼叫系統呼叫(system call)時,如
何傳遞參數?
如下圖:應用程式如何傳遞參數給作業系統?

SYS_XYZ(…);
CPU …

…..

OS

SYS_XYZ()
{

}
Idea: Storage Hierarchy
System Call Parameter Passing

• Three methods used to pass parameters to the OS


– Pass the parameters in registers
• In some cases, may be more parameters than registers
– Parameters stored in a block in memory, and address of block passed as a parameter in
a register
• Taken by Linux and Solaris
– Parameters pushed onto the stack by the program and popped off the stack by the OS

• Block and stack methods do not limit the number or length of parameters
Passing Parameters to System Calls: CPU Registers

mov eax, 10
mov ebx, 20
int SYS_XYZ;
……
User Mode

system_call Kernel Mode

SYS_XYZ :
mov a, eax; a = 10;
mov b, ebx; b = 20;
……
Passing Parameters to System Calls: CPU Registers

mov eax, 10
mov ebx, 20
CPU int 0x10; //SYS_XYZ;
eax 10 …
ebx 20
…..

OS

SYS_XYZ()
{

}
Passing Parameters to System Calls: Memory Block
eax
0x1000
mov eax, 0x1000
int SYS_XYZ; 0x1000
…… greetings
User Mode

Kernel Mode system_call

SYS_XYZ :
user parameters pointed by eax
……

memory
Passing Parameters to System Calls: Memory Block

move eax, 0x1000
int 0x10; //SYS_XYZ;
CPU …

eax 0x1000
…..
greetings

OS
SYS_XYZ()
{
read parameter
from [eax]
}
Passing Parameters to System Calls: Stack

push 10
push 20
int SYS_XYZ;
……
User Mode

system_call Kernel Mode

SYS_XYZ:
pop a; // a = 20;
pop b; // b = 10;
……
Passing Parameters to System Calls: Stack

push 10
push 20
CPU int 0x10; //SYS_XYZ;

…..
10
20
OS

SYS_XYZ()
{

}
Outline

• Operating System Services


• User and Operating-System Interface
• System Calls
• System Services
• Linkers and Loaders
• Why Applications Are Operating-System Specific
• Operating-System Design and Implementation
• Operating-System Structure
• Building and Booting an Operating System
• Operating System Debugging
System Programs

• System services (or system utilities)


– Provide a convenient environment for program development and execution.
• Categories: the can be divided into:
– File management – Windows 檔案總管(file manager)……
– Status information – Windows工作管理員……
– File modification – Text editors
– Programming language support – Compiler, assembler, interpreter…
– Program loading and execution – Loader and debugger
– Communications – Email, talk, web browsing
– Background services – system program launched at boot time
• Usually called services, subsystems, or daemons
Outline

• Operating System Services


• User and Operating-System Interface
• System Calls
• System Services
• Linkers and Loaders (Skip!)
• Why Applications Are Operating-System Specific
• Operating-System Design and Implementation
• Operating-System Structure
• Building and Booting an Operating System
• Operating System Debugging
Linkers and Loaders
Outline

• Operating System Services


• User and Operating-System Interface
• System Calls
• System Services
• Linkers and Loaders
• Why Applications Are Operating-System Specific (Skip!)
• Operating-System Design and Implementation
• Operating-System Structure
• Building and Booting an Operating System
• Operating System Debugging
Outline

• Operating System Services


• User and Operating-System Interface
• System Calls
• System Services
• Linkers and Loaders
• Why Applications Are Operating-System Specific
• Operating-System Design and Implementation
• Operating-System Structure
• Building and Booting an Operating System
• Operating System Debugging
Operating System Design and Implementation

• The problems in designing and implementing an OS


– Design goal

– Mechanism and policies

– Implementation
Design Goal

• Start OS by defining goal


• Affected by
– Hardware
– Type of system: traditional desktop/laptop, mobile, distributed, or real time.

• User goals and System goals


– User goals – OS should be convenient to use, easy to learn, reliable, safe, and fast
– System goals – OS should be easy to design, implement, and maintain, as well as flexible, reliable,
error-free, and efficient

• There is no unique way to achieve all goals, and some compromises must be taken
Mechanisms and Policies

• Example:
– Mechanism: give priority to certain types of programs
– Policy: A program have priority over B program or vice versa.
• Important principle to separate mechanism and policies
– Mechanism
• How to do it?
• Should be insensitive to changes in policy
– Policy
• What will be done?
• Likely to change across places or over time
– Separation of policy from mechanism for flexibility
Implementation

• OS Implementation
– In early days, OS were written in assembly languages
• MS-DOS was written in Intel 8088 assembly language

– In nowadays, OS are now written in high-level languages


• Linux: 90% of Linux code was written in C
• Android
– The kernel is written mostly in C and some assembly language
– System libraries are written in C and C++
– Application frameworks are written mostly in Java
Implementation (Cont.)

• Advantages of using high-level language


– The code can be written faster, is more compact, and is easier to
understand and debug
– Improve portability: to move to some other hardware platform
• See the following two slids

• Disadvantages of using high-level language


– Reduced speed and increased storage requirements
– Solved by modern smart compiler
Poor Portability
… …
add eax, 10 add R1, R0, 10
… Need to modify …
source code

x86 ARM
Good Portability
… …
a = a + 1; a = a + 1;
… …

x86 ARM
Implementation (Cont.)

• In fact, major performance improvement


– NOT from using the assembly language
• Because of advanced compiler techniques
• Only bottleneck routine are written in assembly language

– It should be Better data structure and algorithms


Outline

• Operating System Services


• User and Operating-System Interface
• System Calls
• System Services
• Linkers and Loaders
• Why Applications Are Operating-System Specific
• Operating-System Design and Implementation
• Operating-System Structure
• Building and Booting an Operating System
• Operating System Debugging
Operating System Structure

• Operating system structure


– Monolithic (龐大的) structure
– Layered approach
– Microkernels
– Modules
– Hybrid systems
Monolithic Structure

• Do not have well-defined structures

• Examples
– MS-DOS:
– Original UNIX -
• Everything between the system call interface and physical hardware is the
kernel.
• An enormous amount of functions in one level=> monolithic (龐大的) kernel
Original UNIX Architecture
Monolithic Structure

• Monolithic Structure
– Bad: Difficult to implement and extend
– Good: Have a distinct performance advantage
• Very little overhead in the system-call interface and communication within
the kernel is fast
• Discussed later

– Thus, UNIX, Linux and Windows are monolithic kernels


Operating System Structure

• Operating system structure


– Monolithic structure
– Layered approach
– Microkernels
– Modules
– Hybrid systems
Layered Approach

• OS is divided into a number of layers

• Layer approach: each layer uses functions and services of only lower-level
layers

• Advantages:
– Simplicity of construction and debugging
• Starts from the lowest layer
– Information hiding
• Do not have to know the details of the other layers
Figure 2-15

TCP/IP and OSI Model

The McGraw-Hill Companies, Inc., 2000


Layered File System: File System Organized into
Layers
Layered Approach (Cont.)

• Difficulties
– Hard to determine the number of layers
– Hard to define functionality of each layer
– Less efficient
• A service may involve multiple layers
• But each layer adds overhead

• Trend: fewer layers with more functionality


Operating System Structure

• Operating system structure


– Monolithic structure
– Layered approach
– Microkernel
– Modules
– Hybrid systems
Microkernel

• Motivation: due to UNIX became large and difficult to manage

• Idea: moves as much from the kernel into user space


– A smaller kernel
– Mach: the first microkernel by CMU

• What a microkernel should provide ?


– Typically, minimal process and memory management, and a communication facility

• Communication facility
– Provide communication between the client program and the various services (called servers)
– Provided by message passing
Microkernel System Structure
Microkernel (Cont.)

• Advantages:
– Easier to extend the OS
• By adding new services to user space and do not require kernel modification
– Easier to port the OS to new architectures
• The kernel is small and the changes tend to be fewer
– More reliable and more secure
• Most services are running as user processes
• If a service failed, the rest of the OS remains untouched

• Disadvantages:
– Performance overhead by message passing between services
– Window NT: microkernel, Windows XP: a monolithic kernel
為什麼Message Passing會造成很大的
overhead呢?
第三章我們會介紹
Operating System Structure

• Operating system structure


– Monolithic structure
– Layered approach
– Microkernels
– Modules
– Hybrid systems
Modules

• Best current methodology in OS design


• An OS consists of
– A core kernel
– A set of loadable kernel modules (LKMs) that can dynamically loadable at run time

• Resemble a layered system


– But more flexible
• A module can call any other module

• Resemble the microkernel approach


– Primary core kernel has only core functions
– But more efficient
• Kernel module communications is function calls, not by message passing
Modules

https://www.researchgate.net/figure/Interaction-of-Linux-kernel-modules-with-their-environment_fig1_299373758
Example: Solaris Modular Approach

Solaris is organized around a core kernel with seven types of loadable kernel
modules
Operating System Structure

• Operating system structure


– Monolithic structure
– Layered approach
– Microkernels
– Modules
– Hybrid systems
Hybrid Systems

• Modern operating systems do not adopt a single structure


– Hybrid systems combine multiple structures

– Linux is monolithic,
• + modules for dynamic loading of functionality

– Windows is mostly monolithic


• + personalities (user-level services) (microkernel)
• + modular for dynamic loading of functionality
Outline

• Operating System Services


• User and Operating-System Interface
• System Calls
• System Services
• Linkers and Loaders
• Why Applications Are Operating-System Specific
• Operating-System Design and Implementation
• Operating-System Structure
• Building and Booting an Operating System (Skip!)
• Operating System Debugging
Outline

• Operating System Services


• User and Operating-System Interface
• System Calls
• System Services
• Linkers and Loaders
• Why Applications Are Operating-System Specific
• Operating-System Design and Implementation
• Operating-System Structure
• Building and Booting an Operating System
• Operating System Debugging (Skip!)

You might also like