출처: https://3months.tistory.com/307 [Deep Play]

3-2/운영체제

Mechanism:Limited Direct Execution

코딩하는 랄뚜기 2021. 9. 10. 20:07

Running programs에 limits를 두지 않는다면 OS는 그저 library에 불과하다.


Recap : Process Creation

1. 프로그램 코드를 memory와 process의 address space에 저장해준다.

2. 프로그램의 run-time stack이 할당된다.

- 스택을 local variables, function parameters, return address를 저장하는 데 사용한다.

- main함수의 argc,argv로 stack을 초기화한다.

3. 프로그램의 heap을 만든다.

- 명시적으로 선언된 동적할당된 데이터를 위해 사용된다.

- malloc과 free가 필요하다.

4. OS가 다른 일을 한다.

- (I/O) setup

5. 프로그램을 실행한다.(main())

- OS가 새로 생성된 process에 CPU권한을 넘긴다.

 


Problem 1 : Restricted Operation

process에게 많은 권한을 주면 아주 심각한 오류가 날 가능성이 높아진다.

이를 해결하기 위해서 User mode와 Kernel mode로 권한을 부여에 정도를 두었다.

User mode : Applications do not have full access to hardware resources

Kernel mode : The OS has access to the full resources of the machine

 


System Call

System call은 특정한 부분의 kernel을 user가 사용할 수 있게 허락해준다.

- Accessing the file system

- Creating and destroying processes

- Communicating with other processes

- Allocating more memory

system call write의 예시


Register systemcall handler at IDT

IDT는 interrupt descriptor table 이다.

Call flow(Pintos):write()

syscall_handler()를 읽게 되면 vector table은 interrupt되고 write할 파일의 주소를 가져온다.

 

Key Concept of System Call

 

Trap instruction

- privilege level을 kernel mode로 바뀐다.

- kernel stak에 registers 정보를 저장한다.(eip, cs, eflags, esp, ss)

-  kernel에 목적지를 둔다.

- 목적지로 간다.

- int 0x30 을 한다.(Pintos일 경우만, 그리고 int는 interrupt를 의미한다)

 

Return-from-trap instruction

- 다시 user program으로 돌아간다.

 


What code to run in trap

Sources of trap

- Completion of disk IO

- Keyboard interrupt

- System call

Trap handler : 각각의 interrupt number(trap number)을 돌리기 위한 코드

Trap table

- trap handler의 주소들

- OS는 hardware에게 trap handler의 주소를 준다.

- trap handler의 위치는 privileged instruction이기 때문에 user mode에서는 실행 시킬 수 없다.


Problem 2 : Switching Between Processes

어떻게 OS는 CPU control을 다시 얻을 수 있을까? 두가지 방법이 있다.

A cooperative Approach : Wait for system calls

A Non-Cooperative Approach : The OS takes control

 

A cooperative Approach

Cooperative Approach란 yield와 같은 system calls를 이용하므로서 Process가 일시적으로 CPU를 포기한다.

OS는 다른 것을 먼저 돌리는 것으로 결정한다.

Application도 illegal 한 instruction이 들어오면 OS에게 CPU사용 권한을 넘긴다.(ex : divide by zero, try to access memory that it shouldn't be able to access)

 

A Non-Cooperative Approach: OS Takes Control

A timer interrupt

- boot sequence동안 OS는 timer을 킨다

- timer는 거의 매 miliseconds마다 interrupt를 일으킨다

interrupt가 일어나는 경우

- The currently running process is suspended

- Save enough of the state of the process

- A pre-configured interrupt handler in the OS runs

 

Saving and Restoring Context

Scheduler가 결정을 한다

- 현재 프로세스를 실행 할 지 아니면 다른 것으로 바꿀지 결정한다.

- 다른 것으로 바꾼다면 OS는 context switch를 한다.

 

Context Switch

현재 process의 몇몇의 register values들을 그것의 kernel stack에 저장한다.

- General purpose registers

- PC

- kernel stack pointer

곧 실행될 process를 그것의 kernel stack에 저장한다

곧 실행될 process의 kernel stack으로 바꾼다.

'3-2 > 운영체제' 카테고리의 다른 글

Multiprocessor Scheduling  (0) 2021.09.24
Scheduling:The Multi-Level Feedback Queue  (0) 2021.09.17
Scheduling:Introduction  (0) 2021.09.17
Process API  (0) 2021.09.09
Process  (0) 2021.09.03