CH02 - Operating-System Structures-上課版
CH02 - Operating-System Structures-上課版
Operating-System Structures
Outline
OS
User Mode
Kernel Mode
OS
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
• Voice commands
Outline
int abc() {
…
}
libtmp.a
Compiler b.o
b.c b.o
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
linker
libtmp.a libtmp.a
a.o a.o
b.o b.o
補充: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
– 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);
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
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
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.)
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);
exit(0);
44
}
APIs (Cont.)
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
• 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
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
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
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
– Implementation
Design Goal
• 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
x86 ARM
Good Portability
… …
a = a + 1; a = a + 1;
… …
x86 ARM
Implementation (Cont.)
• 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
• 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
• 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
• 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
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
– Linux is monolithic,
• + modules for dynamic loading of functionality