Nyong!

2. Process 본문

Study/Operating System

2. Process

신뇽! 2022. 2. 9. 15:31

프로세스란?

-> 실행 중인 프로그램

CPU 가상화(single core으로 가정)

  • Time sharirng : 여러개의 프로세스들은 CPU를 공유한다. OS는 각각의 프로세스들에게 각자가 하나의 CPU를 독점하여 사용하고 있다는 illusion을 제공한다. 
    • 프로세스가 실행되고 있는 시간동안, 프로세스는 하드웨어 리소스를 전부 사용하지 않는다.(CPU, I/O interface, memory 등 다양한 하드웨어 리소스를 필요한 것만 필요한 때에 사용한다.)
    • 프로세스가 다른 디바이스(ex. I/O interface)를 이용하는 동안, CPU는 그 디바이스가 프로세스 요청을 끝낼때까지 기다리고만 있어야하는 경우가 생길 수 있다.(엄청난 리소스 낭비임. 이를 해결하기 위한 context switcing) 
  • Context switching : CPU를 효율적으로 사용하기 위하여, OS는 현재 실행중인 프로세스를 멈추고 다른 프로세스로 switch하여 실행한다.
  • Scheduling policy : context switching을 효율적으로 하기 위한 policy

Memory Address Space

각각의 프로세스는 다음과 같은 가상화된 메모리를 이용한다.

이때 사용하는 memory address는 real physical memory address가 아닌 가상화된 private memory address이다.

Memory address

 

OS kernel : 읽고 쓸 수 없다.

 

Stack : local variables, return address, etc. 컴파일러에 의해 할당된다.

 

stack과 heap은 프로세스가 실행되면서 크기가 변할 수 있다. stack은 아래로 커지고, heap 은 위로 커진다.

 

Heap : Dynamic memory allocation. 프로그래머에 의해 할당된다.

 

Data : static, global variables

 

Code : Binary imgae of program

 

int func(int a, int b){
    return (a+b);
}

int main(){
    int a = 1;
    int b = 2;
    a = func(a+b);
    return a;
}
Stack
input arguments of main
return address(entry)
NULL(no previous frame)
local variables in main
inpurt arguments of func
return address of main
frame pointer to main
local variables of func

 

Process API

: interface to handle process.

  • Create 
  • Destroy
  • Wait 
  • Miscellaneous controll : bash commands  
    • Ctrl + z : pause process
    • Ctrl + c : terminate current process
    • kill <process id> : terminate particular process
  • Status : bash command - ps print list of process and status 

POSIX(portable operating system interface for uniX)

IEEE에서 지정한 application programming interface 규격. 

C API system call을 포함한다.

  • File operation : mkdir, symlink, etc.
  • Process handling : fork, execvp, pipe, etc.
  • Memory management : mmap, mlock, etc.

자세한 내용은 이후 system call에서 다룬다.

Process Creation

  1. disk(HDD or SDD)에 저장되어 있는 프로그램을 memory로 load한다.
  2. process의 address space를 만든다.
  3. CPU에게 새로운 process의 control을 준다.

Process states

  • New : 새로 만들어진 프로세스
  • Ready(or runnable) : 프로세스가 실행될 준비가 됨, OS에 의해 schedule 되기를 대기중
  • Running : 프로세스 실행
  • Waiting(or blocked) : 프로세스가 event를 기다림. ex. I/O signal
  • Terminated : 프로세스 종료

Process Control Block(PCB)

process의 정보를 기록한 data structure.

  • Process State
  • Intruction pointer or Program couonter : 다음에 실행(fetch)될 instruction의 memory address
  • Register context : 프로세스가 interrupt 될때 저장될 CPU core register 상의 정보(이 프로세스가 다시 실행될 때 불러옴)
  • Scheduling information : process priority, pointer to scheduling queue, scheduling paramters, etc.
  • Memory information : Base and limit registers of memory segments, page tables, etc.