You are on page 1of 26

Chapter 3: Processes

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Process Concept (Cont.)
 Program is passive entity stored on disk (executable file), process is active
 Program becomes process when executable file loaded into memory
 Execution of program started via GUI mouse clicks, command line entry of its
name, etc
 One program can be several processes
 Consider multiple users executing running the same copies of mail or the
same user may invoke multiple copies of web browser program.

Operating System Concepts – 9th Edition 3.2 Silberschatz, Galvin and Gagne ©2013
Process in Memory

Operating System Concepts – 9th Edition 3.3 Silberschatz, Galvin and Gagne ©2013
Diagram of Process State

Operating System Concepts – 9th Edition 3.4 Silberschatz, Galvin and Gagne ©2013
3.1.2 Process State
 Scheduling may be divided into three phases: Long term, short term and medium term
i. Long Term/ Job: determines which job/processes compete for system resources.
ii. Medium Term scheduler/ swapper: swaps processes in and out of Memory, support multi
programing.
iii. Shor-term Scheduler/ dispatcher: allocates the CPU to the process that is loaded into main memory
and ready to run.
 As a process executes, it changes state. A process may be in one of the following states:
 new: The process is being created (Job/ long term scheduler)
 running: A process that is executing on a CPU. Instructions are being executed
 Waiting/blocked: The process is waiting for some event to occur (such as I/O
Completion or reception of a signal).
 ready: The process is waiting to be assigned to a processor.
 terminated: The process has finished execution.
 It is important to realize that only one process can be running on any processor at any
instant. Many processes may be ready and waiting.
 A process that halt its execution but OS maintained its record on Unix these referred to as
Zombie processes

Operating System Concepts – 9th Edition 3.5 Silberschatz, Galvin and Gagne ©2013
Process states
 Medium Term Scheduling adds two additional process states.
 Swapped/suspend-Blocked: A process that is waiting for some event to occur
and has been swapped out to secondary storage., the process may be high-
priority or the system may be lightly loaded with significant unused memory
available. In either case, moving a swapped-blocked process to the blocked state
allows the process to execute sooner, once the blocking condition satisfied.
 Swapped/suspend–ready: A process that is ready to run but is swapped out to
secondary storage. A swapped ready process may not be allocated to the CPU
since it is not in main memory.
 Following that occur between the states;
i. New Swapped/suspend–ready or new  Ready
If there is insufficient memory for newly created process ,the process may become
swapped ready process.
ii. Swapped/suspend-Ready  Ready: a process is swapped in when sufficient
memory for it becomes available.
iii. Suspend-wait /swapped-blocked  Blocked / Wait: Moving such process to the blocked
state allows the process to execute sooner., once the blocking condition is satisfied.

Operating System Concepts – 9th Edition 3.6 Silberschatz, Galvin and Gagne ©2013
Process states
iv. Ready  swapped ready or Blocked Swapped-Blocked: The medium term
scheduler swaps out processes to release memory resources for other processes.
v. Swapped-Blocked Swapped Ready or Blocked Ready: A process is waiting for
some event occurs, the process is moving to the corresponding ready state.
vi. Running  Ready: Processes are allocated the CPU for maximum amount of time or time slice. If
a process voluntarily release the CPU prior to the end of its time slice , timer run out (TRO) interrupt will
transfer control to the OS and the short term scheduler. Short Term scheduler then moves the process to
the ready state. It is also possible when high priority process from a blocked state will trigger the short
term scheduler to preempt a low priority running process.
vii. Ready  Running: when CPU is free, the short term scheduler selects the next process to run
from the pool of ready processes.
viii. Running  Blocked: process perform I/O, communicating with other processes or waiting on
system conditions are controlled by factors external to the process. Therefore execution of process must
be suspended.
ix. Running Terminated: process terminate either normally or abnormally.
x. Any State Terminated :

Operating System Concepts – 9th Edition 3.7 Silberschatz, Galvin and Gagne ©2013
Process States

Operating System Concepts – 9th Edition 3.8 Silberschatz, Galvin and Gagne ©2013
3.1.3 Process Control Block (PCB)
Information associated with each process
(also called task control block) PCB
 Process state – running, waiting, etc
 Program counter – location of instruction to next
execute for this process
 CPU registers – contents of all process-centric registers
 CPU scheduling information- priorities, scheduling
queue pointers
 Memory-management information – memory allocated
to the process, value of base and limit registers and the
page tables, or the segment tables, depending on the
memory system used by the operating system.
 Accounting information – CPU used, process
numbers, time limits so on.
 I/O status information – I/O devices allocated to process,
list of open files .

Operating System Concepts – 9th Edition 3.9 Silberschatz, Galvin and Gagne ©2013
3.2 Process Scheduling
 Process scheduling allows OS to allocate a time interval of CPU execution
for each process. Another important reason for using a process scheduling
system is that it keeps the CPU busy all the time. This allows you to get the
minimum response time for programs.
 The objective of multiprogramming is to have some process running at all
times, to Maximize CPU use, quickly switch processes onto CPU for time
sharing.
 To meet these objectives Process scheduler selects among available
processes for next execution on CPU.
 System Maintains scheduling queues of processes
 Job queue – set of all processes in the system
 Ready queue – set of all processes residing in main memory, ready and
waiting to execute
 Device queues – set of processes waiting for an I/O device
 Processes migrate among the various queues

Operating System Concepts – 9th Edition 3.10 Silberschatz, Galvin and Gagne ©2013
3.2.1 Scheduling Queues
1. As process enter the system, they are put into JOB Queue which consists of all
processes in the system.
2. The processes that are residing in main memory and are ready and waiting to
execute are kept on a list called the ready queue, generally stored as linked
list. Each PCB includes a pointer field that points to the next PCB in the ready
Queue.
3. When process allocated to the CPU, it executes or a while and interrupted, or
waits for some event for example process makes an I/O request to a shared
device, such as disk., and if the disk busy with I/O request of some other process
, the process may have to wait for the disk. The list of processes waiting for a
particular I/O device is called a device Queue.
Each device has its own device queue.

Operating System Concepts – 9th Edition 3.11 Silberschatz, Galvin and Gagne ©2013
Representation of Process Scheduling
 A common representation of process scheduling is a Queueing diagram represents
queues, resources, flows.

Operating System Concepts – 9th Edition 3.13 Silberschatz, Galvin and Gagne ©2013
Representation of Process Scheduling
• A new process put into ready Queue, it waits there until it is
selected for execution, or dispatched.

• Once process is allocated to CPU , one of several events could


occur:
i. Process issue an I/O request placed in an I/O Queue
ii. Process could create child process and wait for child termination.
iii.Process could be removed forcibly from CPU and put back in the
ready queue.

Operating System Concepts – 9th Edition 3.14 Silberschatz, Galvin and Gagne ©2013
3.2.2 Schedulers
 A process migrates among the various queues throughout its lifetime. The OS must selects, for
scheduling purposes, processes from these queues in some fashion. The selection process is
carried out by the appropriate scheduler.
 Long-term scheduler (or job scheduler) – selects which processes should be brought into
the ready queue from the job Queue.
 Long-term scheduler is invoked infrequently (seconds, minutes)  (may be slow)
 The long-term scheduler controls the degree of multiprogramming
 Short-term scheduler (or CPU scheduler) – selects which process should be executed next
and allocates CPU
 Sometimes the only scheduler in a system
 Short-term scheduler is invoked frequently (milliseconds)  (must be fast)
 Processes can be described as either:
 I/O-bound process – spends more time doing I/O than computations, many short CPU
bursts
 CPU-bound process – spends more time doing computations; few very long CPU bursts
 It is important that the long-term scheduler makes a careful selection. Long-term scheduler
strives for good process mix. If all processes are I/O bound , then ready queue will almost
always empty and if all processes are CPU bound all device ques are always empty. System
will be unbalanced.

Operating System Concepts – 9th Edition 3.15 Silberschatz, Galvin and Gagne ©2013
Addition of Medium Term Scheduling
 Medium-term scheduler can be added if degree of multiple programming needs to decrease
 Remove process from memory, store on disk, bring back in from disk to continue execution:
swapping done for following reasons:
 To improve the process mix,
 Change in memory requirement
 Requiring memory to be freed up.

Operating System Concepts – 9th Edition 3.16 Silberschatz, Galvin and Gagne ©2013
Context Switch
 When CPU switches to another process, the system must save the state of the old
process and load the saved state for the new process via a context switch
 Context (Process context is its current state) of a process represented in the PCB of
the process.
 In Context Switching the process is stored in the Process Control Block to serve the
new process,so that old process can be resumed from the same part it was left.
 To make context switching time to be less, registers which are the fastest access
memory are used.
 Information to be stored for context switching in PCB is:-
 1) program counter 2) Scheduling information 3) Changed state 4) Accounting
Information 5) Base and Limit regsiter

 Context-switch time is pure overhead; the system does no useful work while switching
 The more complex the OS and the PCB  the longer the context switch
 Time dependent on hardware support and complexity of OS.

Operating System Concepts – 9th Edition 3.17 Silberschatz, Galvin and Gagne ©2013
CPU Switch From Process to Process

Operating System Concepts – 9th Edition 3.18 Silberschatz, Galvin and Gagne ©2013
Operations on Processes

 System must provide mechanisms for:


 process creation,
 process termination,
 and so on as detailed next

Operating System Concepts – 9th Edition 3.20 Silberschatz, Galvin and Gagne ©2013
Process Creation
 Four common events leads to the creation of process
1. In batch environment a process is executed in response to the submission of job.
2. In interactive environment , a process is created when a new user attempts to log
on.
3. An OS can create a process on behalf of an application e.g if a user request that
a file be printed, the OS can create a process that will manage a printing.
4. Parent process create children processes (process spawning), which, in turn
create other processes, forming a tree of processes
 Generally, process identified and managed via a process identifier (pid)which
is typically an integer number.
 Pid can be used as an index to access the various attributes of a process within
a kernel.

Operating System Concepts – 9th Edition 3.21 Silberschatz, Galvin and Gagne ©2013
Process Creation
 When process creates child process , child process needs certain
resources. Resource sharing options:
 Parent and children share all resources
 Children share subset of parent’s resources
 Parent and child share no resources .obtain directly from the OS
 Execution options
 Parent and children execute concurrently
 Parent waits until children terminate

Operating System Concepts – 9th Edition 3.22 Silberschatz, Galvin and Gagne ©2013
A Tree of Processes in Linux

Operating System Concepts – 9th Edition 3.23 Silberschatz, Galvin and Gagne ©2013
Process Creation (Cont.)
 Address space(The process address space is the set of logical addresses
that a process references in its code)
 Child duplicate of parent( same program and data as the parent. Example
Process creation in UNIX
 Child has a program loaded into it (Process creation in windows).
 UNIX examples
 fork() system call creates new process
 exec() system call used after a fork() to replace the process’ memory
space with a new program

Operating System Concepts – 9th Edition 3.24 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 3.25 Silberschatz, Galvin and Gagne ©2013
Creating a Separate Process via Windows API

CreateProcess() function: fork child inherits the address space of its


parent. But createProcess() function require loading a specified
program into the address space of the child process at process
creation. Fork() pass no parameters while createProcess function
require ten parameters.

STARTUPINFO: specifies many properties of the new process such as


window size and appearance and handles to standard input/output files.

PROCESS_INFORMATION: contains a handle and the identifiers to


the newly created process and its thread.

ZeroMemory(): to allocate memory for each of these structures.

Operating System Concepts – 9th Edition 3.26 Silberschatz, Galvin and Gagne ©2013
Creating a Separate Process via Windows API

Operating System Concepts – 9th Edition 3.27 Silberschatz, Galvin and Gagne ©2013
Process Creation in Windows
#include<stdio.h>
#include<windows.h>
int main(void) {
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb=sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if(!CreateProcess(NULL, "C:\\WINDOWS\\system32\\mspaint.exe",NULL,NULL,
FALSE,0,NULL,NULL,&si, &pi))
{
fprintf(stderr, "Create Process Failed");
return -1;
} WaitForSingleObject(pi.hProcess, INFINITE);
printf("Child Complete");
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}

Operating System Concepts – 9th Edition 3.28 Silberschatz, Galvin and Gagne ©2013

You might also like