Lecture 2 Abstractions and Interfaces2. !actual doing the work of the !system call 24 system call...

57
Lecture 2 Abstractions and Interfaces 19 August, 2011 1

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

#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

#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

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

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

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

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

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

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

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

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

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

std*  create_student(){      std  *s  =  malloc(sizeof(std));      //  initialize  student        :        :      return  s;}

57