Processes 1
Overview
- Introduction to processes and their implementation
- Process states and state transitions
- System calls for process management
Processes and Implementation
Definition: “a process is a running instance of a program”
进程是程序的运行实例
- A process is registered with the OS using its “control structures”: i.e. an entry in the OS’s process table to a process control blocks (PCB)
- The process control block contains all information necessary to manage the process and is necessary for context switching in multi-programmed systems
- A process’ memory image contains:
- The program code (could be shared between multiple processes running the same code)
- A data segment, stack and heap
- Every process has its own logical address space, in which the stack and heap are placed at opposite[相反的] sides to allow them to grow
Process States and Transitions
Sates:
- A new process has just been created (has a PCB) and is waiting to be admitted (it may not yet be in memory)
- A ready process is waiting for CPU to become available (e.g. unblocked or timer interrupt)
- A running process “owns” the CPU
- A blocked process cannot continue, e.g. is waiting for I/O
- A terminated process is no longer executable (the data structures - PCB - may be temporarily preserved)
- A suspended[废除的] process is swapped out[换出] (not discussed further)
Transitions:
- New -> ready: admit the process and commit to execution
- Running -> blocked: e.g. process is waiting for input or carried out a
system call - Ready -> running: the process is selected by the process scheduler
- Blocked -> ready: event happens, e.g. I/O operation has finished
- . Running -> ready: the process is preempted, e.g., by a timer interrupt or
by pause - Running ->strong text exit: process has finished, e.g. program ended or exception
encountered
The interrupts/traps/system calls lie on the basis of the transitions
Context Switching
Multi-programming
- Modern computers are multi-programming systems
Assuming a single processor system, the instructions of individual processes are executed sequentially
- Multi-programming goes back to the “MULTICS” age
- Multi-programming is achieved by alternating[交替]processes and context switching
- True parallelism requires multiple processors
并不是真正的multi-programming
When a context switch takes place, the system saves the state of the old process and loads the state of the new process (creates overhead)
- Saved -> the process control block is updated
- (Re-)started -> the process control block read
- A trade-off[权衡] exists between the length of the time-slice and the context switch time
- Short time slices result in good response times but low effective “utilisation”[使用]
- e.g.: 99*(1+1)=198ms
- Long time slices result in poor response times but better effective “utilisation”
- e.g.: 99 * (100 + 1) = 9999ms
- Short time slices result in good response times but low effective “utilisation”[使用]
- A process control block contains three types of attributes:
- Process identification (PID, UID, Parent PID)
- Process control information (process state, scheduling information, etc.)
- Process state information (user registers, program counter, stack pointer, program status word, memory management information, files, etc.)
- Process control blocks are kernel data structures, i.e. they are protected and only accessible in kernel mode!
- Allowing user applications to access them directly could compromise[威胁] their integrity[完整性]
- The operating system manages them on the user’s behalf through system calls (e.g. to set process priority)
Tables and Control Blocks
- An operating system maintains information about the status of “resources” in tables
- Process tables (process control blocks)
- Memory tables (memory allocation, memory protection, virtual memory)
- I/O tables (availability, status, transfer information)
- File tables (location, status)
- The process table holds a process control block for each process, allocated upon process creation
- Tables are maintained by the kernel and are usually cross referenced
Switching Processes- Save process state (program counter, registers)
- Update PCB (running -> ready/blocked)
- Move PCB to appropriate queue (ready/blocked)
- Run scheduler, select new process
- Update to running state in the new PCB
- Update memory management unit (MMU)
- Restore process
System Calls
- System calls are necessary to notify the OS that the process has terminated
- Resources must be de-allocated
- Output must be flushed
- Process admin may have to be carried out
- A system calls for process termination:
- UNIX/Linux: exit(), kill()
- Windows: TerminateProcess()
Summary
- Definition of a process and their implementation in operating systems
- States, state transitions of processes
- Kernel structures for processes and process management
- System calls for process management