ostep02--The Abstraction:The Process

来源:互联网 发布:淘宝网在哪里买基金 编辑:程序博客网 时间:2024/06/05 18:36

ostep02–The Abstraction:The Process

process
The definition of a process,informally,is quite simple:it is a running program
The program itself is a lifeless thing:it just sits there on the disk,a bunch of instructions,waiting to spring into action.
It is the OS that take these bytes and gets them running,transfroming the program into something useful

many programs running at the same time?

OS create this illustration by virtualizing the CPU
The basic technique is time sharing of CPU

how to implement above target?

low-level machinery
mechanism
context-switch
time-sharing

high-level intelligence
policy
scheduling algorithm

machine stat

address space:the memory that the process can addressregisters:    program counter(PC):which instruction of the program is currently being executed    stack pointer:    frame pointer:        manage the stack for function parameters、local variables and return addresspersistent information:a list of files...

process API

Create:create new processesDestroy:destroy processes forcefullyWait:wait for a process to stop runningMiscellaneous Control:suspendStatus:get process status information

how programs are transformed into processes

load its code and any static data (e.g., initialized variables) into memory, into the address space of the process. Programs initially reside on disk in some kind of executable format.    eagerly    lazily        paging        swapping

-

allocate  memory for program's <font color=099ff size=5 face="΢ÈíÑźÚ">run-time stack</font>.(C programs use the stack for local variables,function parameters,and return addresses).also likely initialize the stack with arguments such as argc、argv

-

create some inital memory for the program's <font color=099ff size=5 face="΢ÈíÑźÚ">heap</font> .In C programs, <font color=099ff size=5 face="΢ÈíÑźÚ">the heap is used for explicitly requested dynamically-allocated data</font> ; programs request such space by calling <font color=099ff size=5 face="΢ÈíÑźÚ">malloc()</font>  and free it explicitly by calling <font color=099ff size=5 face="΢ÈíÑźÚ">free()</font> . The heap is needed for data structures such as linked lists, 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 involved and allocate more memory to the process to help satisfy such calls.

-

initialization tasks:in UNIX systems,each process by default has three open <font color=099ff size=5 face="΢ÈíÑźÚ">file descriptors</font> ,for standard input,output and error

-

to start the program running at the entry ,namely <font color=099ff size=5 face="΢ÈíÑźÚ">main()</font> 

process state

Running
In the running state,a process is running on a processor.This means it is executing instructions

Ready
In the ready state,a process is ready to run but for some reason the OS has chosen not to run it at this given time

Blocked
In the blocked state,a process has performed some kind of operation that makes it not ready to run until some other event takes place.e.g.:Initiate an I/O request to a disk.

process data structures

process list
keep track of all process that are ready
with additional information to track running processes
and some blocked processes

information to track
register context
process state
sleeping\running\runnable\zombie…
initial state:process being created
final state:process being exited but not cleaned up
the parent process can use final state to examine whether the child executed successfully

PCB
Process Control Block
individual structure that stores above information

0 0