You are on page 1of 22

Operating Systems

Processes

Operating Systems

Slide 1 of 22

Some Basics
Process: a program in execution

Running a program may spawn (create) more than one process

The terms job and process are often used interchangeably

Operating Systems

Slide 2 of 22

Process

Process is more than program code (sometimes called the text section) It also includes current activity, such as:

Program counter Contents of registers Process stack (function parameters, return addresses, local variables)

Data section (contains global variables) Heap (memory dynamically allocated during runtime)
Slide 3 of 22

Operating Systems

Process in Memory
Stack

Two or more instances of the same program are treated distinctly Their text section may be the same, but the data section, heap and stack will be different

Heap Data Text


Operating Systems

Slide 4 of 22

Process State
As a process executes it changes state The state of a process is defined in part by what it is currently doing

New: the process is being created Running: instructions are being executed Waiting: process is waiting for some event to occur Ready: waiting to be assigned to a processor Terminated: finished execution
Slide 5 of 22

Operating Systems

Process State Diagram


New
admitted scheduler dispatch exit

Terminated

Ready
interrupt event completion

Running

Waiting

event wait

Operating Systems

Slide 6 of 22

Process Control Block


Each process in the OS is represented by a Process Control Block (aka task control block) Contains information about the process:

Process state Program counter CPU registers CPU-scheduling information Memory-management information Accounting information I/O status information
Slide 7 of 22

Operating Systems

CPU Switch from Process to Process

Operating Systems

Slide 8 of 22

Threads
Process model discussed so far implies that processes execute in a single thread of execution

Means only one thing can happen at a time Think of a thread as a sequence of execution Threads are a type of concurrency within a process

Operating Systems

Slide 9 of 22

Process Scheduling
Objective of multiprogramming is to have a process running at all times, to maximize CPU utilization Objective of multitasking is to switch frequently & fast between processes so that user can interact with all of them Process Scheduler is the part of the OS that selects one of the available processes for execution on the CPU
Operating Systems Slide 10 of 22

Scheduling Queues

As processes enter the system, they are put into a job queue Processes in main memory that are ready & waiting are put in the ready queue

Usually stored as a linked list Ready-queue header contains pointers to first & last PCBs in the list Each PCB has a field that points to the next in the list

Operating Systems

Slide 11 of 22

Scheduling Queues

When processes need to use an I/O device and have to wait for it, they enter its device queue

Operating Systems

Slide 12 of 22

Scheduling Queues: Visualization

Operating Systems

Slide 13 of 22

Queuing Interactions

Operating Systems

Slide 14 of 22

Schedulers
Processes move between various scheduling queues during their lifetime

Which process in any given queue should be picked next?

The selection of a process from all the processes in a queue is the job of the scheduler

Operating Systems

Slide 15 of 22

Long- & Short-Term Schedulers


The Long-Term Scheduler (or job scheduler) selects processes from the job pool and loads them into memory for execution The Short-Term Scheduler (or CPU scheduler) selects from among the processes that are ready-to-execute for allocating the CPU to

Basic difference is in the frequency of execution Speed more important for CPU scheduler
Slide 16 of 22

Operating Systems

Some Definitions

Long-term scheduler controls the degree of multiprogramming (the number of processes in memory) I/O-bound processes spend more of their time doing I/O CPU-bound processes spend more of their time doing computations

Operating Systems

Slide 17 of 22

Intricacies of Scheduling

LTS has to choose a good mix of CPU-bound & I/O-bound processes Too many CPU-bound processes I/O devices are under-utilized Too many I/O-bound processes CPU is under-utilized

Operating Systems

Slide 18 of 22

Medium-Term Scheduler
Medium-Term Scheduler manages the degree of multiprogramming

Removes processes from memory (& thus from CPU contention) Later process can be re-loaded & execution continued This unloading & reloading process is called swapping

Operating Systems

Slide 19 of 22

Switching Between Processes

Interrupts cause OS to change CPU from current task to run kernel routine Such events happen frequently When interrupt happens, OS needs to save the context of currently-executing process so that its state can be restored

Essentially suspending the process & resuming it

Operating Systems

Slide 20 of 22

Context of a Process
The context of a process is represented in its PCB

Value of CPU registers Process state Memory management information

Operating Systems

Slide 21 of 22

Context Switch
When switching from one process to another, the OS performs a state save and then a state restore This process is called a context switch

This is pure overhead: no useful work is being done

Operating Systems

Slide 22 of 22

You might also like