Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call...
Transcript of Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call...
![Page 1: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/1.jpg)
Lecture 2
Abstractions and Interfaces
19 August, 2011
1
![Page 2: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/2.jpg)
Overview of Basic Concepts in OS
2
![Page 3: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/3.jpg)
process
3
![Page 4: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/4.jpg)
Recall: Time-Sharing
4
![Page 5: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/5.jpg)
CPU Memory Disk
Waiting Area
Task 1
Task 2Task 3
Network
...
Task 4
5
![Page 6: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/6.jpg)
what do you need to remember in order to
resume a task?
6
![Page 7: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/7.jpg)
process: code + data +
context of execution
7
![Page 8: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/8.jpg)
address space
8
![Page 9: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/9.jpg)
how to prevent one process from corrupting the memory of
another process?
9
![Page 10: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/10.jpg)
every process has its own address space
10
![Page 11: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/11.jpg)
a process may occupy different physical memory location at different time
11
![Page 12: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/12.jpg)
but the executable code remains the same.
(e.g., for instruction: load from address X to register Y
what should X be?)
12
![Page 13: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/13.jpg)
Memory
stack
data
code
CPU
13
![Page 14: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/14.jpg)
file and directory
14
![Page 15: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/15.jpg)
Data on disks are organized into files and directories
15
![Page 16: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/16.jpg)
block special files character special files
in/dev
16
![Page 17: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/17.jpg)
pipe
17
![Page 18: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/18.jpg)
shell
18
![Page 19: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/19.jpg)
User interfaces to OS:1. shell
2. window systems
19
![Page 20: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/20.jpg)
processaddress space
files and directoriesshell
20
![Page 21: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/21.jpg)
operating system (a.k.a kernel)
compilers editors shell..
browser calendar media player..
file display keyboard mouse printer battery socket
user mode
kernel mode
21
![Page 22: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/22.jpg)
Interfaces provided by OS are known as system calls.
22
![Page 23: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/23.jpg)
stack
data
code
CPU
Memory
function parameterslocal variables
saved frame pointerreturn address
CPU
programcounter
stackpointer
framepointer
kernel
programstatus word
23
![Page 24: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/24.jpg)
Invoking system calls involved:
1. !a common setup/cleanup ! ! ! routine
2. !actual doing the work of the !system call
24
![Page 25: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/25.jpg)
system call setup:
1. switch to kernel mode (PSW bit) 2. save context of current process3. pass arguments to kernel 4. determine which system call ! service routine to call
25
![Page 26: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/26.jpg)
system call clean/up:
1. indicate error code, if any2. check if need to switch to another ! process3. restore process context4. return to user mode
26
![Page 27: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/27.jpg)
System calls are typically triggered by an interrupt instruction (e.g., TRAP or INT)
27
![Page 28: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/28.jpg)
Structure of OSes
28
![Page 29: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/29.jpg)
Structure of Linux Kernel
29
![Page 30: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/30.jpg)
interrupt dispatcher
system calls
hardware
user program
30
![Page 31: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/31.jpg)
interrupt dispatcher
system calls
hardware
user program
process management
memory managementI/O component
31
![Page 32: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/32.jpg)
interrupt dispatcher
system calls
hardware
user program
process management
memory management
device drivers
virtual file sys
32
![Page 33: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/33.jpg)
such design is called monolithic kernel
33
![Page 34: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/34.jpg)
dynamically loadable kernel module
keeps the kernel small, extensible(also in MS Windows, Mac OS X)
34
![Page 35: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/35.jpg)
Alternative to monolithic design:microkernel
35
![Page 36: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/36.jpg)
interrupt dispatcher
system calls
hardware
process managementmemory management
device driversfile systemsnetwork
An example microkernel architecture
36
![Page 37: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/37.jpg)
keep minimal functions in kernel
example:
37
![Page 38: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/38.jpg)
A brief introduction to
C
38
![Page 39: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/39.jpg)
C vs Java
(highlights)
39
![Page 40: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/40.jpg)
Java: set of classes
C: set of functions + structures
40
![Page 41: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/41.jpg)
C: no byte datatypeno boolean datatypeno String class
(use char, int, and array of char respectively)
41
![Page 42: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/42.jpg)
C: no “new” operator
must explicit declare as pointer for reference variables
must explicitly malloc() and free() memory
42
![Page 43: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/43.jpg)
Java: all variables are reference except boolean and numeric types
C: all variables are primitive types (holds the value of that exact type)
43
![Page 44: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/44.jpg)
Java: external classes must be imported
C: external functions and types must be declared
(made easy with #include statement)
44
![Page 45: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/45.jpg)
int main(){ return 0;}
45
![Page 46: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/46.jpg)
#include <stdio.h>
int main(){ printf(“Hello World!\n”); return 0; }
46
![Page 47: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/47.jpg)
#include <stdio.h>#include <stdlib.h>
void say_hello(int times){ int i; for (i = 0; i < times; i++) printf(“Hello World\n”);}
int main(int argc, char *argv[]){ say_hello(atoi(argv[1])); return 0; }
47
![Page 48: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/48.jpg)
every variable has an address and can store a value.
int x;
1089201
48
![Page 49: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/49.jpg)
assignment operator takes the value on the right, store into the address on the left.
49
![Page 50: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/50.jpg)
int x;int y;x = 1;y = 2;x = y;
50
![Page 51: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/51.jpg)
int *x;int y;y = 2;x = &y;*x = 3;y = *x;
// what is *y, &x ?
51
![Page 52: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/52.jpg)
int *x;int y = 2;
*x = 1;
52
![Page 53: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/53.jpg)
int y = 2;
f(y);
int f(int a){ a = 2; }
53
![Page 54: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/54.jpg)
int y[3];
*y = 7;*(y+1) = 9
54
![Page 55: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/55.jpg)
char *name;
name = malloc(10);strcpy(name, “cs2106”); : :free(name);
55
![Page 56: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/56.jpg)
struct student { int student_id; char *name; int age;}typedef struct student std;
56
![Page 57: Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call setup: 1. switch to kernel mode (PSW bit) 2. save context of current process 3. pass](https://reader034.fdocuments.net/reader034/viewer/2022042310/5ed82cdb0fa3e705ec0df9e6/html5/thumbnails/57.jpg)
std* create_student(){ std *s = malloc(sizeof(std)); // initialize student : : return s;}
57