Processes and Threads
1
Outline
• Chapter 3 : Processes, Description and Control
• Chapter 4 : Threads, SMPs and Microkernels
• Chapter 9 : Uniprocessor Scheduling
• Chapter 10 : Multiprocessor, Real-time-time
Scheduling
2
Process Description and Control
Chapter 3
3
Learning Objectives for Chapter 3
• Identify reasons for creation/termination of processes
• Explain the role of different parts maintained by OS for processes
• Explain the refinement of `state’ of processes and evaluate its impact on process execution
• Evaluate the role of user mode and kernel mode and changes between them
• Explain different OS organizations and their impact
• Ability to write code for creating, managing processes in
Linux
4
Outline of Chapter 3
• Process creation, termination, states of Processes
• Process image, process control block
• user mode vs privileged mode
• Different OS designs
• SVR 4 processes
5
Process Description and Control
• All multiprogramming systems are built based on the concepts of processes.
• Main issues about processes
– different states of processes and transitions among them.
– process description (data structures maintained)
– process control (how process switches between processes)
• Focus on Unix SVR4 processes
6
Requirements of an
Operating System
• Interleave the execution of multiple processes to maximize processor utilization while providing reasonable response time
• Allocate resources to processes
• Support interprocess communication and user creation of processes
7
Process
• A program in execution
• An instance of a program running on a computer
• The entity that can be assigned to and executed on a processor
• A unit of activity characterized by the execution of a sequence of instructions, a current state, and an associated set of system instructions
10
Process Elements (L)
• Identifier
• State*
• Priority
• Program counter
• Memory pointers
• Context data
• I/O status information
• Accounting information
11
Process Control Block
• Contains the process elements
• Created and managed by the operating system
• Allows support for multiple processes
12
Two-State Process Model (I)
• Process may be in one of two states
– Running
– Not-running
• Transitions between these states is performed by dispatcher
17
Not-Running Process in a Queue
18
Process Creation
• Submission of a batch job
• User logs on
• Created to provide a service
– E.g, for printing, network management, etc
• One process creates another process
– For modularity
– This is something you would do in your projects as well
19
Process Termination (L)
• Batch job termintates
• User logs off
• Quit an application
• Error and fault condition
20
Process Termination (Continued) (L)
• Normal termination
• Time limit exceeded
• Memory limitation
• Illegal memory access
• Protection errors; Performing operations with resources that are not permitted
• Arithmetic error
• Process waited longer than a specified maximum for an event
21
Process Termination (Continued) (L)
• I/O failure
• Invalid instruction
• When process tries to execute data
• If return address from a procedure is corrupted
• Privileged instruction execution
• Intervention by operating system (e.g., Deadlock resolution)
• Signals (/usr/include/bits/signum.h @pacific)
• Termination of parent terminates the child process
22
Process States (I)
• Running
• Not-running
– Ready
• ready to execute
– Blocked
• E.g., waiting for I/O
• Dispatcher cannot just select the process that has been in the queue the longest because it may be blocked
23
Other States (I)
• New state
– OS has performed the necessary actions to create the process
• assigning a process identifier
• created data structures to manage the process
– the process execution has not begun
• e.g. if resources are limited
• Exit state
– Process has terminated
– Data structure about the process is preserved
– E.g., if parent is allowed to check the status of its children
24
• A Five-State Model
– Running
– Ready
– Blocked
– New
– Exit
Thus, (I)
25
Process States
26
Refinement for Process Organization
Using Two Queues
27
Refinement for Process Organization:
Multiple Blocked Queues
28
Suspended Processes (I)
• Processor is faster than I/O so all processes could be waiting for I/O
• Swap these processes to disk to free up more memory
• Blocked state becomes suspend state when swapped to disk
• Two new states
– Blocked/Suspend
– Ready/Suspend
29
One Suspend State
30
Two Suspend States
31
Reasons for Process Suspension (L)
• Swapping
– Due to lack of available physical memory
• Interactive user request
– E.g., debugging
• Timing
– Periodically executing processes
• Parent process request
– Under certain conditions, a parent can suspend a child process
• Other OS reasons
• E.g., an utility that is suspected to cause problems
32
Outline
• Process creation, termination, states of Processes
– Review
• Process image, process control block
• user mode vs privileged mode
• Different OS designs
• SVR 4 processes
39
Process Table
• Where process is located
• Attributes in the process control block
– Program
– Data
– Stack
40
Example PCB Contents (L)
• Process identifiers
– Numeric identifiers that may be stored with the process control block include
• Identifier of this process
• Identifier of the process that created this process (parent process)
• User identifier
• Links to other processes
– Parent, child, …
• Processor State Information
– User-Visible Registers
– Stack Pointers
41
Example PCB Contents (L)
• Process context
– Control and Status Registers
These are a variety of processor registers that are employed to control the operation of the processor. These include
• Program counter: Contains the address of the next instruction to be fetched
• Condition codes: Result of the most recent arithmetic or logical operation (e.g., sign, zero, carry, equal, overflow)
• Status information: Includes interrupt enabled/disabled flags, execution mode
42
Example PCB Contents (L)
• Scheduling/state information
– Process state: defines the readiness of the process to be scheduled for execution (e.g., running, ready, waiting, halted).
– Priority: One or more fields may be used to describe the scheduling priority of the process. In some systems, several values are required
(e.g., default, current, highest-allowable)
– Scheduling-related information: This will depend on the scheduling algorithm used. Examples are the amount of time that the process has been waiting and the amount of time that the process executed the last time it was running.
– Event: Identity of event the process is awaiting before it can be resumed
43
Example PCB Contents (L)
• Process Control Information
– Interprocess Communication
• Various flags, signals, and messages may be associated with communication between two independent processes. Some or all of this information may be maintained in the process control block.
– Process Privileges
• Processes are granted privileges in terms of the memory that may be accessed and the types of instructions that may be executed. In addition, privileges may apply to the use of system utilities and services.
44
Example PCB Contents (L)
• Process Control Information
– Memory Management
• This section may include pointers to segment and/or page tables that describe the virtual memory assigned to this process.
– Resource Ownership and Utilization
• Resources controlled by the process may be indicated, such as opened files. A history of utilization of the processor or other resources may also be included; this information may be needed by the scheduler.
45
Example PCB Contents (L)
• Contents of processor registers
– User-visible registers
– Control and status registers
– Stack pointers
• Program status word (PSW)
– contains status information
– Example: the EFLAGS register on Pentium machines
46
Pentium II EFLAGS Register
47
In Summary: User Process in Memory
48
In Summary: Organization of Processes in
Different States
49
Outline
• Process creation, termination, states of Processes
• Process image, process control block
• user mode vs privileged mode
• Different OS designs
• SVR 4 processes
50
Intuition (I)
• Before any access is permitted, check whether the process has the right privileges
– Achieved in several steps
• Certain instructions are restricted only for OS
– Has to be checked inside CPU
– Achieved with user and privileged mode
• Every memory access has to be checked to determine if the process is allowed to access it.
– Has to be done in hardware
– Amount of time permitted is in nanoseconds
– Cannot be done by operating system
• Access to files should be verified
– Can be done in software
– Time in milliseconds
– Done by operating system
51
Modes of Execution
• User mode
– Less-privileged mode
– User programs typically execute in this mode
• System mode, control mode, or kernel mode
– More-privileged mode
– Kernel of the operating system
• A bit in CPU tells which mode the system is in
• Certain instructions set this bit (e.g., interrupt, system call, trap)
52
Consider a process
P1
{
… read()
….
}
Example (I)
53
Example (continued)
• Compilation of the code for read will include
– A special place where the parameters for read will be placed
– An instruction that sets the bit.
• Invocation of the special instruction will transfer the control to operating system
• Operating system will check the parameters to determine the desired system call to be executed
• What if a user process calls the special instruction that sets the bit on its own?
54
Mode Switching
• It may happen that an interrupt does not result in a process switch; i.e., the interrupted process is run again.
• The system needs to save the processor state information
• This is called mode switching (e.g., user to kernel mode when an interrupt occurs and the interrupt handler runs).
55
Outline
• Process creation, termination, states of Processes
• Process image, process control block
• user mode vs privileged mode
• Different OS designs
• SVR 4 processes
57
Execution of the Operating System
• Up to now, by process, we referred to ``user process''
• Is the OS just like another process?
• If so, how is it controlled?
• Answer depends on OS design
58
Execution of the Operating System
• Non-process Kernel
– Execute kernel outside of any process
– Operating system code is executed as a separate entity that operates in privileged mode
59
60
Execution of the Operating System
• Execution Within User Processes
– Operating system software within context of a user process
– Process executes in privileged mode when executing operating system code
61
62
Execution of the Operating System
• Process-Based Operating System
– Implement operating system as a collection of system processes
– Useful in multi-processor or multi-computer environment
63
64
Outline
• Process creation, termination, states of Processes
• Process image, process control block
• user mode vs privileged mode
• Different OS designs
• SVR 4 processes
65
UNIX SVR4 Process Management
• Most of the operating system executes within the environment of a user process
66
UNIX SVR4 Process Management
• Uses two categories of processes
– System processes
• run in kernel mode for housekeeping functions (memory allocation, process swapping, etc.)
– User processes
• run in user mode for user programs
• run in kernel mode for system calls, traps, interrupts
67
Traditional Unix Kernel (continued)
• Typical kernel functions
– Controlling the execution of processes by allowing their creation, termination or suspension, and communication
– Scheduling processes fairly for execution on the CPU
– Allocating main memory for an executing process
– Allowing processes controlled access to peripheral devices
– Allocating secondary memory for efficient storage and retrieval of user files and process images
• In Unix, the kernel does not persist as a process itself.
Rather, its routines are executed (in protected mode) on behalf of user processes.
68
Unix SVR4 Process States
• Similar to seven state model
• Two running states (User and Kernel)
– transitions to other states (blocked, ready) must come from kernel running.
• Sleeping states (in memory/ swapped) correspond to blocking states
• A preempted state is distinguished from ready state
(but they form one queue)
• Preemption can occur only when a process is about to move from kernel to user mode
69
UNIX Process States
70
UNIX Process Image
71
72
UNIX Process Creation
• Every process, except process 0, is created by the fork() system call.
– fork() allocates entry in process table and assigns a unique PID to the child process
– child gets a copy of the process image of the parent; both child and parent are executing the same code after fork()
– fork() returns the PID of the child process to the parent process and returns 0 to the child process
73
UNIX System Processes
• Process 0 is created at boot time and becomes the
``swapper'' process after forking process 1 (the INIT process)
• When the user logs in: process 1 creates a process for that user.
74
UNIX Process Image
• User level context
– Process Text
– Process Data
– User stack
– Shared memory (e.g., for IPC)
• Register context
• System level context
75
Summary
• Process is a fundamental building block of an operating system
• OS is responsible for their creation, management, termination and interaction
• OS maintains a description of each process
• OS moves processes among a number of states
• A running process can switch to kernel mode via an interrupt or a system call
• After completion of the service, OS msay continue that process or switch to another one.
• REVIEW
76