[Robbins03] 2.1 How a Program Becomes a Process (程序如何转变成进程)

来源:百度文库 编辑:神马文学网 时间:2024/03/29 01:15:40

2.1 How a Program Becomes a Process

A program is a prepared sequenceof instructions to accomplish a defined task. To write a C source program, aprogrammer creates disk files containing C statements that are organized intofunctions. An individual C source file may also contain variable and functiondeclarations, type and macro definitions (e.g.,typedef) andpreprocessor commands (e.g.,#ifdef,#include,#define). The source program contains exactly onemainfunction.

Traditionally, C source filenames have a.c extension,and header filenames have a.h extension. Header files usually onlycontain macro and type definitions, defined constants and function declarations.Use the#include preprocessor command to insert the contents of aheader file into the source.

The C compiler translates each source file into an object file.The compiler then links the individual object files with the necessary librariesto produce an executable module. When a programis run or executed, the operating system copiesthe executable module into a program image inmain memory.

A process is an instance of aprogram that is executing. Each instance has its own address space and executionstate. When does a program become a process? The operating system reads theprogram into memory. The allocation of memory for the program image is notenough to make the program a process. The process must have an ID (the process ID) so that the operating system candistinguish among individual processes. The processstate indicates the execution status of an individual process. Theoperating system keeps track of the process IDs and corresponding process statesand uses the information to allocate and manage resources for the system. Theoperating system also manages the memory occupied by the processes and thememory available for allocation.

When the operating system has added the appropriate informationin the kernel data structures and has allocated the necessary resources to runthe program code, the program has become a process. A process has an addressspace (memory it can access) and at least one flow of control called a thread. The variables of a process can either remain inexistence for the life of the process (static storage) or be automaticallyallocated when execution enters a block and deallocated when execution leavesthe block (automatic storage). Appendix A.5 discusses Cstorage classes in detail.

A process starts with a single flow of control that executes asequence of instructions. The processor program counter keeps track of the nextinstruction to be executed by that processor (CPU). The CPU increments theprogram counter after fetching an instruction and may further modify it duringthe execution of the instruction, for example, when a branch occurs. Multipleprocesses may reside in memory and execute concurrently, almost independently ofeach other. For processes to communicate or cooperate, they must explicitlyinteract through operating system constructs such as the filesystem (Section 5.1),pipes (Section6.1), shared memory (Section 15.3) or a network(Chapters 18-22).