이 글의 내용은 국민대학교 황현태 교수님 CS015605 운영체제 수업의 필기 노트입니다.
Informally, the defintion of a process is a running program. The program itself is a lifeless thing. It just sits there on the disk, a bunch of instructions(and maybe some static data), waiting to spring into action. It is the operating system that takes these bytes and gets them running, transforming the program into something useful.
By virtualising the CPU, the OS runs one process then stopping it and running another, and so forth. The OS can promote the illusion tha tmany virtual CPUs exist when in fact there is only one physical CPU (or a few).
Time Sharing is a basic technique used by an OS to share a resource.
Time sharing of the CPU allows users to run as many concurrent processes as they would like; the pothential cost is performance, as each will run more slowly if the CPU(s) must be shared.
To implement virtualisation of the CPU, the OS will need both some low-level machinery and some high-level intelligence. The low-level machinery is called 'Mechanisms'. Mechanisms are low-level methods or protocols that implement a needed piece of functionality. For example it implements a context switch, which gives the OS the ability to stop running one program and start running another on a given CPU.
On top of these mechanisms resides some of the intelligence in the OS, in the form of policies.
Pollicies are algorithms for making some kind of decision within the OS. For example, given a number of possible programs to run on a CPU, which program should the OS run? A scheduling policy in the OS will make this decision.
Loading From Program To Process
The first thing that the OS must do to run a program is to load its code and any static data(e.g., initialised variables) into memory, into the address space of the process.
Programs initally reside on disk (or SSDs) in some kind of executable format.
In early operating systems, the loading process is done eagerly, i.e., all at once before running the program; modern OSes perform the process lazily, i.e., by loading pieces of code or data only as they are needed during program execution.
Once the code and static data are loaded into memory, there are a few other things the OS needs to do before running the process. Some memory must be allocated for program's stack. As we know, C programs use the stack for local variables, function parameters, and return addresses. The OS will also likely initialise the stack with arguments; specifically, it will fill in the parameters to the main()
function.
The OS may also allocate some memory for the program's heap. In C programs, the heap is used for explicitly requested dynamically-allocated data; programs request such space by calling malloc()
and free it explicitly by calling free()
. The heap is needed for data structures such as linked list, hash tables, trees, and other interesting data structures. The heap will be small at first; as the program runs, and requests more memory via the malloc()
library API, the OS may get involoved and allocated more memory to the process to help satisfy such calls.
The OS will also do some other initialisation tasks, particularly as realated to I/O. For example, in UNIX systems, each process by default has three open file descriptors, for standard input, output, and error.