Process Control
Modes of Execution (실행 모드)
- User mode(사용자 모드)
- 운영체제 서비스를 제공받을 수 없는 실행 모드
- 일반적인 응용 프로그램은 기본적으로 사용자 모드로 실행
- 입출력 명령어와 같이 하드웨어 자원에 접근하는 명령어를 실행할 수 없다.
- kernel mode(커널 모드)
- 운영체제 서비스를 제공받을 수 있는 실행 모드
- 자원에 접근하는 명령어를 비롯한 모든 명령어를 실행할 수 있음.
커널 모드의 기능들
두 개의 모드를 나누어 사용하는 이유
- OS 와 OS tables를 보호하기 위해서다.
즉, 응용 프로그램들이 CPU, 메모리, 하드 디스크 등에 마음대로 접근하고 조작할 수 있다면 자원이 무질서하게 관리될 것이고, 응용 프로그램이 조금만 실수해도 컴퓨터 전체에 큰 악영향을 끼칠 수 있다.
그렇기에 모드를 두개로 구별하여 자원을 보호한다.
프로세서는 어떤 모드가 실행중인지 어떻게 알까?
실행 모드를 나타내는 PSW(Program Status Word)의 비트를 통해 안다.
모드는 언제 바뀌는가?
- Call to system call(시스템 호출)
- Interrupt/Trap triggering(인터럽트/트랩)
System call
System call(시스템 호출)
- 사용자 모드로 실행되는 프로그램이 자원에 접근하는 운영체제 서비스를 제공받으려면 운영체제에 요청을 보내 커널 모드로 전환되어야 하는데 이때 운영체제 서비스를 제공받기 위한 요청을 뜻한다.
시스템 호출
- 커널로 들어가는 입구같은 것
- 사용자 프로그램으로부터 호출하는 C 함수 같은 것
ex) read(),write()
라이브러리 함수
- 라이브러리 기능은 종종 시스템 호출보다 더 정교하며 일반적으로 시스템 호출을 발생시킨다.
ex) fprintf(), fscanf()
Process creation
1. 고유한 프로세스 아이디(PID)를 할당받는다.
2. 프로세스를 위한 메모리를 할당받는다. (Program, data, user stack, PCB)
3. PCB값을 초기화한다.
- 첫 번째 단계에서 생성한 PID를 PID 영역에 채우고, 부모 프로세스 아이디도 채운다.
- 스택 포인터와 프로그램 카운터를 제외한 대부분의 영역을 0으로 초기화 한다.
스택 포인터는 시스템 스택의 주소로 채워지고, 프로그램 카운터는 프로그램의 주소값을 할당한다.(Ready-queue에 배치)
- 프로세스 제어 정보는 디폴트 값으로 초기화된다.
4. 적절한 링크에 배치한다.
ex) 프로세스를 스케줄링 큐를 위한 링크드 리스트에 추가한다.
5. 다른 자료구조를 생성하거나 확장한다.
Process switching
사용자 모드에서 운영체제로 제어가 넘어가는 상황
- Interrupt(인터럽트)
- Trap(트랩)
- Supervisor call(System call, 시스템 호출)
Interrupt
(1) Clock interrupt
프로세스가 정해진 시간동안 실행되었을 때 발생 -> dispatcher가 process 교체
(2) Memory fault
주소 값을 갖고 왔지만 ,메인 메모리에 올라오지 않을 때, blocked queue에 들어감.
CPU가 한 명령어를 실행하려 하는데, 이 명령어를 실행하기 위해 필요한 데이터가 메인 메모리가 아닌 보조기억 장치에 있다고 가정하자. 프로그램이 실행되려면 반드시 메모리에 저장되어 있어야 하기 때문에 CPU는 폴트를 발생시키고 보조기억장치로부터 필요한 데이터를 메모리로 가져와 저장한다.
(3) I/O Interrupt
CPU의 속도가 I/O의 속도보다 빠르기 때문에 I/O 인터럽트가 발생하는 경우가 있다.
Trap
- 에러나 예외처리가 발생할 때
- 프로세스를 종료 상태로 바꿀 때
예를 들어, 디버깅을 할 때 특정 코드가 실행되는 순간 프로그램의 실행을 멈추게 할 때가 있는데 잠시 멈춘 프로그램을 중단시키고 디버깅이 끝나면 프로그램은 다음 명령어부터 다시 실행을 이어나갈 것이다.
이처럼 예외가 발생한 명령어의 다음 명령어부터 실행을 재개하는 예외이다.
Supervisor call (system call)
- fopen 같은 명시적인 요청
CLOCK INTERRUPT가 발생하면
OS는 현재 running process A를 ready 상태로 두고, Ready 상태 였던 process B를 running process로 바꾼후
프로세서에 대한 제어를 process B에게 양도 -> process switching 발생
자세히 알아보면
Process Switching Step
1. 프로그램 카운터와 다른 레지스터를 포함한 프로세스를 PCB에 저장
2. 이 상태 값을 현재 실행상태인 프로세스 PCB에 업데이트
이 때, 실행중이던 프로세스는 Ready or Blocked or Ready/suspend 상태로 바뀜
3. 위에 내용과 같은말로 PCB를 적절한 큐(Ready or Blocked or Ready/suspend)에 옮김
4. Scheduling 알고리즘에 따라 실행 프로세스를 선택함.
5. 선택된 프로세스의 PCB를 업데이트 ex) Ready -> Running
6. 자료구조 메모리 관리 업데이트
7. 선택된 프로세스의 context를 restore(복원)한다.
Interrupt/Trap이 발생하면 항상 process swtiching이 발생해야하는가?
-> 일반적으로 프로세스는 안바뀜
Mode Switching
인터럽트가 있는걸 알았다면 인터럽트 핸들러의 시작 주소로 프로그램 카운터를 설정하고, 커널모드로 바꾼다.
대부분의 인터럽트는 프로세스 스위치를 하지않는다. 그 이유는 프로세서 상태 정보를 저장하고, 다시 프로그램의 제어가 돌아왔을때 복원할수 있는 정보가 있기 때문이다.
즉, 인터럽트가 발생하면 인터럽트 핸들러의 시작주소로 프로그램 카운터값을 설정하고,
Process switching이 발생하는 것이 아닌 kernel mode로 바뀌는 mode switching이 발생하는 것이다.
왜냐면 커널 모드에서는 더 많은 명령을 실행할 수 있기 때문이다.
*Mode switching만 발생
그렇다면 어떤 operation이 더 많은 비용이 드는가?
Process switch >>>>>> Mode switch
=> 모드 스위칭은 딸깍이지만, 프로세스 스위칭은 위에 프로세스 스위칭 스텝 처럼 해야하는 게 많기 때문이다.
Execution of the Operation System
OS를 보는 3가지 관점
- Non-process Kernel
- Execution within User process
- Process-based Operation System
Non-process Kernel
- 커널을 프로세스와 완전히 분리되어있는 독자적인 존재로 보는 접근
- 실행중인 프로세스가 시스템 호출이나 인터럽트가 발생하면 프로세스의 모드 컨텍스트가 저장되고 컨트롤이 커널로 넘어간다.
Execution Within User Processes
- 유저프로세스의 일부로 커널이 위치하고 있다는 접근
- 인터럽트, 시스템 호출이 일어나면 프로세서가 커널모드가 되거나 컨트롤이 운영체제로 넘어간다.
- 프로세스가 러닝상태에서 운영체제 소스코드가 실행됨.
Process-based Operation System
- 프로세스와 커널의 주요기능을 분리된 객체로 보지만, 커너릐 주요기능과 process swtiching 기능 또한 분리시켜서 보는 접
'🖥️ Computer Science > Operating System' 카테고리의 다른 글
[운영체제] Process Creation (0) | 2024.04.19 |
---|---|
[운영체제] Threads, SMP, Microkernels (1) | 2024.04.09 |
[운영체제] Process Description and Control - 2 (0) | 2024.03.27 |
[운영체제] Process Description and Control - 1 (0) | 2024.03.21 |
[운영체제] Computer System Overview (0) | 2024.03.15 |