430 likes | 535 Views
Processes. Definitions. A program is a file that contains an executable module. Running a program loading the list of machine level instructions into memory and having the CPU execute the instructions one at a time. Hardware View. Memory. environment variables. Program Counter.
E N D
Definitions A program is a file that contains an executable module. Running a program loading the list of machine level instructions into memory and having the CPU execute the instructions one at a time.
Hardware View Memory environment variables Program Counter stack frame stack return address parameters status copy of registers local variables Registers heap At the hardware level, the computer is pretty dumb. It simply marches through memory, executing one instruction after another. It knows nothing of programs, processes, or threads. static data machine instructions
Definitions A program is a file that contains an executable module. A process is a program that has been loaded into memory, along with it’s stack, registers, program counter, and status. Processes live in user space. i.e. a process is a program in execution on a von Neumann computer!
Definitions A program is a file that contains an executable module. A process is a program that has been loaded into memory, along with it’s stack, registers, program counter, and status. Processes live in user space. We can think of all of the resources required to run a process (cpu, registers, memory, …) as a virtual or abstract machine.
A Single Process Memory Program Counter stack frame return address parameters status copy of registers local variables environment variables stack Registers heap static data machine instructions
Multi-Processing Program Counter Memory environment variables stack frame stack return address parameters status copy of registers local variables Virtual Machine 1 Registers heap static data machine instructions Program Counter Memory environment variables stack frame stack return address parameters status copy of registers local variables Virtual Machine 2 Registers heap static data machine instructions
Virtual Machine 2 Virtual Machine 1 Load Process 1 Execute Program 1 for a while … Memory Program Counter stack frame The real machine return address parameters status copy of registers local variables environment variables stack Registers heap static data machine instructions
Virtual Machine 2 Virtual Machine 1 Save the state of Process 1 on disk Memory Program Counter stack frame The real machine return address parameters status copy of registers local variables environment variables stack Registers heap static data machine instructions
Virtual Machine 2 Virtual Machine 1 Load Process 2 Execute Program 2 for a while … Memory Program Counter stack frame The real machine return address parameters status copy of registers local variables environment variables stack Registers heap static data machine instructions
Virtual Machine 2 Virtual Machine 1 Save the state of Process 2 on disk Memory Program Counter stack frame The real machine return address parameters status copy of registers local variables environment variables stack Registers heap static data machine instructions
The Process Address Space 0 Memory Manager In a 32 bit machine, each process can address up to 232 bits of address space. Real Memory Memory mapped devices, etc 232 Address Space
A Process Can Have Multiple Threads. An address space a program (the program segment) data (the data segment) resources (for example, open files) a process identifier Each thread running in a process has a unique set of general purpose registers instruction register status registers stack heap a thread identifier Shared by all threads
The ps Command The ps command provides a way to list the current processes running on a Unix system. ps lists your processes ps –a list all user processes ps –al list all user processes, in detail ps -ax list system processes
state process id niceness terminal parent’s process id size priority
The Process Manager Process creation and termination Thread creation and termination Process/Thread synchronization Resource allocation Resource protection Cooperation with device manager for I/O Cooperation with memory manager for address space mapping
A Simplified View of Process Management
Process Creation Processes are created * At system initialization time * By system call from an existing process - fork( ) in Unix - CreateProcess( ) in Windows * When a user requests a new process * When a batch job is initiated
Process Termination A processes terminates when * It is done and it exits normally * The process discovers an error and exits * There is a fatal error and the system kills the process * Another process kills it
Process Control Blocks Program Counter Memory environment variables stack frame stack Implementation of Processes return address parameters status copy of registers local variables Registers heap static data machine instructions Interrupt vector For each process, the operating system maintains a Process control block (PCB) or Process Descriptor. The process control block contains important information about each process that is used whenever a context switch takes place.
Typical Entries in a Process Control Block Process Management Memory Management registers pointer to program text program counter pointer to data segment program status word pointer to stack process state base and limit regs priority scheduling parameters FileManagement process id root directory parent process working directory process group open file descriptors Signals pending user ID accounting info group ID Stack Signal mask o o o
Creating a New Process (Linux) PCB of running process calls fork( )
Creating a New Process (Linux) PCB of running process PCB of child process calls fork( ) A new process control block is created
Then fields unique to this process are modified * process id * accounting info * timers * … Creating a New Process (Linux) PCB of running process PCB of child process The contents of the parent PCB are copied into the new child PCB
PCB PCB PCB Creating a New Process (Linux) The new PCB is added to a Linked list of PCBs PCB of child process
Creating a New Process (Linux) PCB of CHILD process A file table is created for the new process, and a file descriptor is created for each file that is open in the parent process (i.e. the child shares the parent’s open files. file table
Creating a New Process (Linux) The child is made the running process PCB of CHILD process address space file table An address space is created for the new process and the contents of the parent’s data segment and program segment are copied into this new address space.
When a process starts another new process, two • possibilities exist in terms of execution: • The parent continues to execute concurrently • with its children • 2. The parent waits until some or all of its children • have terminated
When a process starts another new process, two • possibilities exist in terms of the address space of • the new process: • The child process is an exact duplicate of the • parent process. • 2. The child process has a unique program loaded • into its address space.
Process States Running • Process requests • A resource: • Page fault • I/O 2 Process is pre-empted by a higher priority process, interrupt, timer, or makes a system call 1 3 Ready Blocked 4 The resource request is satisfied new
Operation old state new state Create (none) ready Request running blocked Release blocked ready Destroy any (none) Scheduler ready running running ready System calls The init process is created automatically when the system starts up, and is destroyed when the system shuts down.
The Ready List pcb pcb pcb When a new process is created, It’s pcb is added to the ready list (the set of processes that can be run). null
The Scheduler The scheduler selects the next process to be executed. Various scheduling algorithms are possible. We will study scheduling algorithms in a later set of slides. For this example case, we will use a multi-level priority scheduler with fixed priority levels. In this example, the ready list contains n queues, one for each priority level. Each queue contains a list of pointers to PCBs at that priority level. The scheduler services the ready list in FIFO order, starting with the highest priority queue.
The Ready List Showing priority queues 0 1 pcb pcb pcb null
Context Switches When the next process to be run has been selected by the scheduler, the process is dispatched. The context of a process is contained in the process control block. When a new process is dispatched, a context switch occurs. The state of the current process is saved in it’s pcb, and the cpu is loaded with the context of the new running process. Context switches are pure overhead – there is no useful work done during a context switch.
Trapping Kernel programs are run in supervisory mode. That is, they can execute privileged instructions not available to normal users. When a user program invokes a system call, the calling code puts the system call number in a register and invokes the trap function. The trap function switches the machine into supervisory mode and begins executing the dispatcher. This requires a context switch. The dispatcher looks at the system call number, and uses it to locate the executable code for the function call. The system call code executes, control is returned to the calling program, (another context switch) and the machine switches back to user mode.
Timers To implement time-sharing, some kind of timer must be provided. The timer issues periodic interrupts that call the scheduler.
Mechanism: generic management functions • Policy: Resource specific behavior • Who • Under what circumstances • shared or exclusive Resource Manager Blocked Processes Policy request( ) Process release( ) Resource Pool
Waiting For Resources When a process requires a resource, it makes the request for the resource. If the resource is not immediately available, the process is blocked and it waits for the resource to become available.
Resource ID Resource Control Blocks • Status • Free • Allocated Wait List pcb pcb
Requesting a Resource Request ( RID) { r = getRCB(RID); if (r->status == “free”) { r->status = “allocated”; insert (pcb, r); // show r as being allocated to process } else { self->status = “blocked”; // show blocked in pcb remove (self, ready_list); // remove self from ready_list insert ( r->waiting_list, self); // add self to resources wait list } scheduler( ); }
Releasing a Resource Release (RID) { r = get_RCB(RID); if (waiting_list == NULL) // nobody is waiting { r->status = “free”; } else { remove ( r->waiting_list, q); // get next process from queue q->status = “ready”; // mark it as ready insert (ready_list, q); // insert on ready list } scheduler( ); }