process
Q&A 정리: process
프로세스란 무엇이며, 프로그램과 어떻게 다른가?
프로그램은 하드디스크에 저장된 레시피(설명서)이고, 프로세스는 그 레시피를 보고 실제로 요리하는 행위다. 프로그램을 실행하면 메모리에 올라가 프로세스가 되어 비로소 작업이 수행된다.
In computing, a process is the instance of a computer program that is being executed by one or many threads. While a computer program is a passive collection of instructions typically stored in a file on disk, a process is the execution of those instructions after being loaded from the disk into memory.
같은 프로그램을 여러 번 실행하면 프로세스는 어떻게 되는가?
같은 레시피로 여러 냄비에서 동시에 요리할 수 있듯이, 하나의 프로그램을 여러 번 실행하면 각각 별개의 프로세스가 만들어진다. 예를 들어 메모장을 세 개 열면 세 개의 프로세스가 생긴다.
Several processes may be associated with the same program; for example, opening up several instances of the same program often results in more than one process being executed.
OS가 프로세스를 관리하기 위해 묶어두는 구성 요소에는 무엇이 있는가?
운영체제는 프로세스마다 실행할 코드, 시스템 자원, 접근 권한, 제어용 데이터 구조를 하나로 묶어 관리한다. 마치 각 직원에게 업무 매뉴얼, 사무용품, 출입 카드, 업무일지를 세트로 지급하는 것과 같다.
Almost all processes (even entire virtual machines) are rooted in an operating system (OS) process which comprises the program code, assigned system resources, physical and logical access permissions, and data structures to initiate, control and coordinate execution activity. Depending on the OS, a process may be made up of multiple threads of execution that execute instructions concurrently.
프로세스의 메모리 영역은 어떤 구성 요소를 포함하는가?
프로세스의 메모리에는 실행할 코드, 입출력 데이터, 현재 작업 흐름을 추적하는 호출 스택, 그리고 계산 중간 결과를 임시 보관하는 힙이 포함된다. 작업대 위에 레시피(코드), 재료(데이터), 조리 순서표(스택), 임시 보관함(힙)이 놓여있는 모습이다.
Memory (typically some region of virtual memory); which includes the executable code, process-specific data (input and output), a call stack (to keep track of active subroutines and/or other events), and a heap to hold intermediate computation data generated during run time.
PCB(프로세스 제어 블록)란 무엇인가?
운영체제가 각 프로세스의 상태 정보를 기록해두는 관리 카드다. 프로세스가 어디까지 실행했는지, 어떤 자원을 쓰고 있는지 등을 담고 있어서, 프로세스를 잠시 멈췄다가 다시 이어서 실행할 수 있게 해준다.
The operating system holds most of this information about active processes in data structures called process control blocks. Any subset of the resources, typically at least the processor state, may be associated with each of the process' threads in operating systems that support threads or child processes.
OS가 프로세스들을 서로 격리하는 이유와, 격리 실패 시 발생할 수 있는 문제는?
프로세스끼리 서로의 영역을 침범하면 한 프로그램의 오류가 다른 프로그램까지 먹통으로 만들 수 있다. 운영체제는 아파트 벽처럼 프로세스를 격리하여 교착 상태(서로 양보 없이 멈춤)나 시스템 과부하 같은 장애를 방지한다.
The operating system keeps its processes separate and allocates the resources they need, so that they are less likely to interfere with each other and cause system failures (e.g., deadlock or thrashing). The operating system may also provide mechanisms for inter-process communication to enable processes to interact in safe and predictable ways.
멀티태스킹이란 무엇이며, 하나의 CPU 코어에서 여러 프로세스가 동시에 실행되는 것처럼 보이는 원리는?
하나의 CPU 코어는 한 번에 하나의 프로세스만 처리할 수 있지만, 아주 빠르게 번갈아가며 실행하면 사람 눈에는 동시에 돌아가는 것처럼 보인다. TV 채널을 빠르게 돌리면 여러 프로그램을 동시에 보는 듯한 착각이 드는 것과 비슷하다.
Multitasking is a method to allow multiple processes to share processors (CPUs) and other system resources. Each CPU (core) executes a single process at a time. In time-sharing systems, context switches are performed rapidly, which makes it seem like multiple processes are being executed simultaneously on the same processor. This seemingly-simultaneous execution of multiple processes is called concurrency.
OS에서 컨텍스트 스위치(프로세스 전환)가 발생하는 시점은?
프로세스가 파일 읽기 등 외부 작업을 기다릴 때, 자발적으로 CPU를 양보할 때, 하드웨어 인터럽트가 발생할 때, 또는 운영체제가 "이 프로세스가 충분히 오래 실행됐다"고 판단할 때 전환이 일어난다.
Multitasking allows each processor to switch between tasks that are being executed without having to wait for each task to finish (preemption). Depending on the operating system implementation, switches could be performed when tasks initiate and wait for completion of input/output operations, when a task voluntarily yields the CPU, on hardware interrupts, and when the operating system scheduler decides that a process has expired its fair share of CPU time (e.g, by the Completely Fair Scheduler of the Linux kernel).
현대 OS가 프로세스 간 직접 통신을 막고 IPC를 제공하는 이유는?
프로세스끼리 직접 데이터를 주고받으면 보안 허점이나 오류가 생길 수 있으므로, 운영체제가 중간에서 안전하게 전달해주는 공식 통로(IPC)를 제공한다. 직원끼리 사적으로 열쇠를 빌려주는 대신, 관리실을 통해 정식으로 빌리는 것과 같다.
For security and reliability, most modern operating systems prevent direct communication between independent processes, providing strictly mediated and controlled inter-process communication.
부모 프로세스와 자식 프로세스의 관계는 어떻게 형성되는가?
메인 프로그램이 실행되면 부모 프로세스가 되고, 이 프로세스가 별도의 작업을 위해 새 프로세스를 만들면 그것이 자식 프로세스가 된다. 본사가 지사를 설립하여 독립적으로 업무를 맡기는 것과 비슷하다.
It is usual to associate a single process with a main program, and child processes with any spin-off, parallel processes, which behave like asynchronous subroutines.
프로세스가 blocked 상태가 되는 조건과, 가상 메모리 시스템에서 blocked 프로세스의 메모리는 어떻게 처리되는가?
프로세스가 파일이나 사용자 입력처럼 외부 자원을 기다려야 하면 blocked 상태가 된다. 이때 가상 메모리 시스템은 당장 쓰지 않는 메모리 부분을 디스크로 옮겨두었다가, 다시 필요해지면 불러온다. 자주 안 쓰는 서류를 창고에 보관했다가 필요할 때 꺼내오는 것과 같다.
If a process requests something for which it must wait, it will be blocked. When the process is in the blocked state, it is eligible for swapping to disk, but this is transparent in a virtual memory system, where regions of a process's memory may be really on disk and not in main memory at any time. Even portions of active processes/tasks (executing programs) are eligible for swapping to disk, if the portions have not been used recently. Not all parts of an executing program and its data have to be in physical memory for the associated process to be active.
프로세스의 상태 전이(lifecycle)에서 waiting과 blocked의 차이는?
waiting은 CPU 차례를 기다리는 대기 상태이고, blocked는 파일 읽기나 사용자 입력 같은 외부 자원을 기다리는 상태다. 식당 비유로 보면 waiting은 빈 테이블을 기다리는 것이고, blocked는 주문한 재료가 배달될 때까지 조리를 멈춘 상태다.
First, the process is "created" by being loaded from a secondary storage device (hard disk drive, CD-ROM, etc.) into main memory. After that the process scheduler assigns it the "waiting" state. While the process is "waiting", it waits for the scheduler to do a so-called context switch. The context switch loads the process into the processor and changes the state to "running" while the previously "running" process is stored in a "waiting" state. If a process in the "running" state needs to wait for a resource (wait for user input or file to open, for example), it is assigned the "blocked" state. The process state is changed back to "waiting" when the process no longer needs to wait (in a blocked state). Once the process finishes execution, or is terminated by the operating system, it is no longer needed. The process is removed instantly or is moved to the "terminated" state.