Processes - It works!

Download Report

Transcript Processes - It works!

Lecture 3
Process Concepts
What is a Process?
A process is the dynamic execution context of an executing program. Several processes
may run concurrently, each as a distinct entity with its own state. The process state
consists of at least:
• the code for the running program
• the static data for running program
• a space in the heap for dynamic data
• the address of next instruction
• execution stack
• values of the CPU registers
• OS resources (e.g. open filees)
• process execution state
Process Code
Computer Memory
Process State
Each process transitions from one state to the next as it shares the CPU and additional
computer resources with other processes. A process changes states due to actions by the OS
and other entities.
Terminated
New
Suspend & Resume
Suspending a process indefinitely removes it from contention for time on a processor without being
destroyed. Suspending a process is useful for detecting security threats and for software debugging
purposes (e.g. deadlock detection and recovery). A suspension may be initiated by the process
being suspended or by another process.
Linux (Unix) Processes
All processes are provided with a set of memory addresses, called a virtual address space
A process’s PCB is maintained by the kernel in a protected region of memory that user
processes cannot access A Linux (Unix) PCB stores:
• The contents of the processor registers
• PID
• The program counter
• The system stack
• All processes are listed in the process table
• All processes interact with the OS via system calls
• A process can spawn a child process by using the fork system call
• Child receives a copy of the parent’s resources as well
• Process priorities are integers between -20 and 19 (inclusive)
• A lower numerical priority value indicates a higher scheduling priority
• UNIX provides IPC mechanisms, such as pipes, to allow data transfer
Process Control Block (PCB)
The operating system keeps track of all active processes using a dynamic data structure called
the process control block. The PCB maintains the execution state and location in memory of
each process.
Process State Queues
• process state
• process number
• program counter
• stack pointer
• CPU registers
• memory addresses
• owner's username
• list of open files
• queue pointers
• scheduling info
• I/O status
Context Switch
A context switch occurs when a running process is returned to the ready state (or is blocked) and
another process is moved into the running state.
When the OS is ready to start executing a waiting process, it loads the hardware registers
(PC, SP, and other location registers) from the values stored in that process' PCB.
While a process is running, the CPU modifies the Program Counter (PC), Stack Pointer
(SP), and CPU registers.
When the OS stops a process, it saves the current values ose of the registers, (PC, SP,
etc.) into the PCB for that process.
In time sharing systems, many processes are running concurrently, so context switching
occurs many times each second (100's or even 1000's per second).
The time required to load the PCB with the current info for one process and to transfer
the contents of the PCB into active memory for another determines the cost of context
switching (overhead).
Example: Context Switching
Creating a Process
One process can initiate another to do work. The creating process is called the parent process
and the created process is called the child process.
A processs may share its resources with a child process. When a parent creates a child, it can
either wait for the child to complete or it can operate concurrently with the child. In a Linux
(Unix) OS subprocesses are created by a call the the system routine called fork.
• variable values and the program counter are copied from parent to child
• the return value of fork in the parent process is the child process ID (the ID in the
child process is set to 0)
• the parent can wait for the child to terminate by executing a wait system call
• the parent can continue to execute
• the child can start a new a different program (by making a call to exec)
• when you log into a machine running Linux (Unix) you create a shell process
• every command you tyope into the shell is a child of your shell proce3ss and is an
implicit fork and exec pair
• if you type an & after the command, Linu will run the process concurrently with your
shell, otherwise, your next shell commandc must wait until the first one completes
Example Linux (Unix) Program
fork( ) forks a new child process that is a copy of the parent
execlp( ) replaces the program of the current process with the named program
sleep( ) suspends execution for at least the specified time
waitpid( ) waits for the named process to finish execution
gets( ) reads a line form a file
Process Termination
When a process terminates the OS reclaims all the resources assigned to that process
In Linux (Unix)
• a process can terminate itself using the exit system call
• a process can terminate a child using the kill system call
Interrupts
Interrupts enable software to respond to signals from hardware
May be initiated by a running process
Interrupt is called a trap
Synchronous with the operation of the process
For example, dividing by zero or referencing protected memory
May be initiated by some event that may or may not be related to the running process
Asynchronous with the operation of the process
For example, a key is pressed on a keyboard or a mouse is moved
Low overhead
Polling is an alternative approach
Processor repeatedly requests the status of each device
Increases in overhead as the complexity of the system increases
2004 Deitel & Associates, Inc.
Handling interrupts
After receiving an interrupt, the processor completes execution of the current instruction,
then pauses the current process
The processor will then execute one of the kernel’s interrupt-handling functions
The interrupt handler determines how the system should respond
Interrupt handlers are stored in an array of pointers called the interrupt vector
After the interrupt handler completes, the interrupted process is restored and executed or
the next process is executed
2004 Deitel & Associates, Inc.
Common Interrupt Types
Intel CPU
2004 Deitel & Associates, Inc.
Common Exception Types
Intel CPU
2004 Deitel & Associates, Inc.
Interprocess Communication
Many operating systems provide mechanisms for interprocess communication (IPC)
Processes must communicate with one another in multiprogrammed and
networked environments (for example, a Web browser retrieving data from a
distant server)
Essential for processes that must coordinate activities to achieve a common
goal
Message-based interprocess communication
Messages can be passed in one direction at a time (sender to receiver)
Message passing can be bidirectional (either process can be
sender/receiver)
Messages can be blocking or nonblocking
Popular implementation is a pipe ( i.e. a protected communication buffer)
2004 Deitel & Associates, Inc.
Summary
• A process is the unit of execution
• Processes are specified in Process Control Blocks
• Each process has a state
• There are, at most n processes running in parallel on an n-core computer
• The OS starts and stops process by context switching
• Interrupt Handling permits software to respond to messages from hardware
• Interprocess Communication permits message-passing between processes