CS/운영체제

Chapter 3: Processes

arsenic-dev 2025. 5. 4. 23:59

경희대학교 허선영 교수님의 운영체제 수업을 기반으로 정리한 글입니다.

Process in Memory

Process Address Space

: 메모리 중 process가 사용하는 공간 -> 4가지 섹션

  • stack: 함수의 local variable, return address 등 (e.g., main, add)
  • heap: 동적 object
  • data: 전역 변수 
  • text: 코드 = 명령어 (read only)

※ 전역 변수 따로 저장하는 이유: stack은 함수마다 공간 존재하지만, 전역 변수는 모든 함수가 접근

 

1. stack

각각의 함수는 stack frame을 갖게 되고, 이 frame 내에서 해당 함수가 사용하는 local variables 저장됨

-> 함수 호출/return에 따라 stack 크기 업/다운

 

2. heap

buffer, new Student(); (memory allocation)

 

3. data

int x = 3; int main() {}

-> int는 4 bytes이기에 메모리 공간 4 bytes

 

4. text

test.cpp -> test.exe (CPU 명령어)

 

※ CPU는 text 부분을 실행, 물론 실행에 필요한 데이터들은 다른 섹션에서 업데이트됨

Process address space는 process마다 따로 존재한다.

Process Control Block (PCB)

: Process마다 관리하는 Data Structure (process의 상태, ID, ... )

  • process number: PID (숫자), process identifier
  • memory limits: 프로세스가 접근 가능한 메모리 공간이 어딘지 저장 -> 다른 프로세스의 메모리 공간 침투 막음

  • copy: 다른 프로세스로 전환하려면 현재의 register, PC 값을 백업해 두어야 함 -> 재실행 시, 복원 필요
  • PCB: process가 많을 수록 PCB 할당을 위해 kernel 메모리 공간 많이 필요

※ 리눅스 시스템은 전체 프로세스들을 더블 링크드(양방향 연결) 리스트 형태로 관리

 

Process Scheduling

OS에는 수많은 프로세스 존재하기에 프로세스 스케줄링 필요

-> CPU core에서 실행할 process 선택 (이때, process state = ready여야 함)

CPU엔 여러 개의 core가 있고, 각 core는 독리벅으로 프로세스 명령어 실행 가능하다.

 

1. Ready queue: 실행 기다림

2. Wait queue: event 기다림 (type에 따라 여러 개 존재, e.g., I/O, interrupt, ... )

Context Switch

switching을 하려면 먼저 context를 PCB에 저장해야 한다.

  • context: 기존 process가 실행했던 결과, state (PC value: 실행 위치, register value: 중간 결과)

OS는 user process들을 실행하는 것이 목적인데, context switching을 하는 동안은 user process 실행 불가

-> context-switch time: overhead

Hyperthreading

: 코어마다 register set을 여러 개 두는 CPU (virtual cores)

-> CPU 내에서 context switching 일어나기에 PCB 백업(regs -> memory) X 

 

Operations on Processes

Process Creation

: Parent process create children processes

  • Root parent process 이후 모든 process는 자신의 parent process가 존재

Example) UNIX

1. fork(): 복제

2. exec(): memory space 갈아입기

3. wait(): parent process waits for the child terminate

  • pid = fork(): 처음엔 child와 parent 코드 동일하여 코드 구분 위해 시스템 콜 fork()의 return 값 활용
  • exec(): 프로세스 내에서 다른 프로그램 load하여 실행
  • exit(): 프로세스 종료하는 시스템 콜로, main 함수 리턴 시 호출

Process Termination

1. exit(): returns status from child to parent via wait()

2. abort(): parent가 child 강제 종료

-> process management도 시스템 콜로 이루어짐

 

Cascading termination

: parent 죽으면 child도 순차적으로 강제 죽음 (종속)

 

parent   child
wait <- exit
종료 <- exit
wait X <- exit

 

1. orphan: parent가 wait하지 않고 종료 -> parent X (고아)

2. zombie: parent가 wait 상태가 아닌데 child exit -> exit 상태를 return 받을 parent가 없게 되면 실질적인 종료 불가

 

※ 메모리 공간 낭비를 막기 위해 zombie 프로세스를 주기적으로 확인하여 종료하는 OS 존재

Interprocess Communication (IPC)

: cooperating process need IPC

  • Two interprocess communication models

  • Shared memory: 메모리 공간 통해 자유롭게 데이터 주고받음 (synchronization 필요)
  • Message passing: 커널 안의 message queue를 거쳐 데이터 주고받음 (send, receive)

shared memory은 시스템 콜을 통해 커널이 생성하지만 control은 프로세스가 하며,

message passing은 global variable로도 가능하다.

'CS > 운영체제' 카테고리의 다른 글

Chapter 5: CPU Scheduling  (1) 2025.05.07
Chapter 4: Thread & Concurrency  (0) 2025.05.06
Chapter 2: Operating System Structures  (1) 2025.05.04
Chapter 1: Introduction  (0) 2025.05.04