You are on page 1of 40

OPERATING SYSTEMS PROCESSES

1
Operating System Concepts – 10h Edition Silberschatz, Galvin and Gagne ©2018
Chapter Outline
1.2

▪ Process Definition
▪ Scheduling Processes
▪ What Do Processes Do?
▪ Inter-process Communication

2
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.3

Chapter Objectives
▪ Identify the separate components of a process and
illustrate how they are represented and scheduled
in an operating system.
▪ Describe how processes are created and
terminated in an operating system, including
developing programs using the appropriate system
calls that perform these operations.
▪ Describe and contrast interprocess communication
using shared memory and message passing.
▪ Design programs that uses pipes and POSIX
shared memory to perform interprocess
communication
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.4

Process
A process is :
• An instance of a program in execution
• An asynchronous activity
• The “animated spirit” of a procedure
• The “locus of control” of a procedure in execution
• The “dispatch able” unit
• An unit of work individually schedulable by an operating system.
Formally, we can define a process is an executing program, including
the current values of the program counter, registers, and variables. The
subtle difference between a process and a program is that the program is a
group of instructions whereas the process is the activity.
A program is passive; a process is active.

4
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.5

Process Management Functions


The process management functions include:
1. Process creation
2. Termination of the process
3. Controlling the progress of the process
4. Process Scheduling
5. Dispatching
6. Interrupt handling / Exceptional handling
7. Switching between the processes
8. Process synchronization
9. Inter process communication support
10.Management of Process Control Blocks
5
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.6

Process States
▪ Let us start with these states:
• The Running state
The process that is executing on the CPU is in the Running state

• The Blocked state


A process that is waiting for something (e.g. I/O) to complete is in the Blocked
state

• The Ready state


A process that is ready to be executed, but not currently assigned to a CPU,
is in the Ready state

▪ Only one process can be running on any processor at any


instant.
▪ Many processes may be ready and waiting.

6
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
7
1.7

PROCESSES PROCESS STATE(5)

As a process executes, it changes state


new: The process is being created
running: Instructions are being executed
waiting: The process is waiting for some event to occur
ready: The process is waiting to be assigned to a processor
terminated: The process has finished execution

Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning


1.8

A Seven-State Process Model


But it is better to add two more states to keep
track of those that are still blocked, and those
which are no longer blocked because their event
has occurred..

8
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.9

Some New state Transitions


▪ Blocked --> Blocked Suspend
• When all processes are blocked, the OS may remove a blocked process to bring
an unblocked process into memory
The “swap out” frees up memory to allow this to happen
▪ Blocked Suspend --> Ready Suspend
• When the event for which process has been waiting occurs
▪ Ready Suspend --> Ready
• When there are no ready processes in main memory
• Normally, this transition is paired with Blocked --> Blocked suspend for
another process (a “swap”)
▪ Ready--> Ready Suspend
• When there are no blocked processes and must free up memory for performance
reasons
▪ New--> Ready Suspend
• Probably the preferred way to introduce new processes
9
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
10
1.10

PROCESS CONTROL BLOCK Process State

PROCESS CONTROL BLOCK:


Each process is represented in the O.S.
by a Process Control Block.
1. Program counter
2. CPU registers
3. CPU scheduling information
4. Memory-management information
5. Accounting information
6. I/O status information.

Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning


1.11

PROCESS CONTROL BLOCK

• A PCB contains the following Information:


– Process state: new, ready, …
– Program counter: indicates the address of the next
instruction to be executed for this program.
– CPU registers: includes accumulators, stack pointers,
– CPU scheduling information: includes process
priority,pointers to scheduling queues.
– Memory-management information: includes the value of
base and limit registers (protection) …
– Accounting information: includes amount of CPU and
real time used, account numbers, process numbers, …
– I/O status information: includes list of I/O devices
allocated to this process, a list of open files, …

11
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
12
1.12
Scheduling Components
PROCESSES Context Switch
The act of Scheduling a process means changing the active
PCB pointed to by the CPU. Also called a 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
•Context-switch time is overhead; the system does no useful
work while switching
•Time dependent on hardware support

Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning


1.13

Two Types of Context Switch


▪ Simple Mode Switch to process an interrupt without switching
processes: user process is suspended but will be resumed
immediately:

• only save what is necessary to resume execution of the same


process (e.g. program counter, couple of registers)

▪ Full Process Switch: process is suspended and another process will


get the CPU:

• save entire context into PCB, load new context from other PCB,
update process state.

• A “heavier duty” operation


13
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
14
1.14
CPU Switch From Process to Process
Let us understand with the help of an example. Suppose if two processes P0 and
P1 are in ready queue. Suppose if two processes P0 and P1are in ready
queue. If CPU is executing
process P0 and process P1 is in
wait state. If an interrupt occurs
for Process P0, the operating
system suspends the execution of
the first process, and stores the
current information of Process P0
in its PCB and context switch to
the second process namely
Process P1. In doing so, the
program counter from the PCB of
Process P1 is loaded, and thus
execution can continue with the
new process.
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.15

Process Scheduling Queues


▪ Objective of multiprogramming is to have some process running at all time to
maximize CPU utilization.
• Objective of time sharing is to switch the CPU among processes so
frequently that users can interact with each program while it is running.
▪For a uniprocessor system, there will never be more than one running
process.
If there are more processes, the rest will have to wait until the CPU is free and
can be rescheduled.
▪Job queue – Set of all processes in the system
▪Ready queue – Set of all processes residing in main memory, ready and
waiting to execute. Ready queue is stored as linked list. A Ready queue Header
will contain pointers to the first and last PCBs in the list. Each PCB has a
pointer field that points to the next process in the Ready queue.
• Device queues – Set of processes waiting for an I/O device
Each device has its own device queue. Processes migrate among the various
queues
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.17

Schedulers
Long-term scheduler (or Job scheduler) –

Selects which processes should be brought into the ready


queue (i.e, selects processes from pool (disk) and loads them
into memory for execution).

• Short-term scheduler (or CPU scheduler) –

Selects which process should be executed next and allocates


CPU (i.e, selects from among the processes that are ready to
execute, and allocates the CPU to one of them) .

Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning


1.18

Schedulers (Cont.)
▪Short-term scheduler is invoked very frequently (milliseconds)
 (must be fast)

▪Long-term scheduler is invoked very infrequently (seconds,


minutes)  (may be slow)
▪The long-term scheduler controls the degree of
multiprogramming
(The number of processes in the memory)

Medium-term scheduler – Is used to remove processes from


memory and reduce the degree of multiprogramming (the process
is swapped out and swapped in by the medium-term scheduler.
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.19

Addition of Medium Term Scheduling

Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning


1.20
Schedulers (Cont.)
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.
• If all processes are I/O bound, the ready queue will
almost always be empty and the short-scheduler will
have little to do.
• If all processes are CPU bound, the I/O waiting queue
will almost always be empty, devices will go unused,
and the system will be unbalanced.
• To get best performance the system should have a
combination of CPU and I/O bound processes.
20
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.21

Dispatcher (short-term scheduler)


▪ An OS program that switches the CPU from one process
to another selected by the short term scheduler. It
involves
—Context switching
—Switching to user mode
▪ It prevents a single process from monopolizing CPU
time. It decides who goes next according to a scheduling
algorithm
▪ The time it takes for the dispatcher to stop one process
and start another running is called dispatch latency
▪ The CPU executes instructions in the dispatcher while
switching from process A to process B
21
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.22

Process Creation
• A process may create several new processes, via a create-process system call,
during execution.
• Parent process creates children processes, which, in turn create other processes,
forming a tree of processes.
• Resource sharing, such as CPU time, memory, files, I/O devices …

– The Parent continues to execute concurrently with its children


– The parent waits until some or all of its children have
terminated
– The child process is a duplicate of the parent process (it has the
same program and data as the parent)
– The child process has a new program loaded into it

22
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.23

Process Creation (Cont.)

• UNIX examples
– fork system call creates new process
– execve system call used after a fork to replace the process
memory space with a new program.

23
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.24 Process Creation
int main()
{
pid_t pid; /* C Program Forking Separate Process
/* fork another process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0) { /* child process */
execlp("/bin/ls", "ls", NULL);
}
else { /* parent process */
/* parent will wait for the child to complete */
wait (NULL);
printf ("Child Complete");
exit(0);
}
}

24
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.25

When does a process get terminated?

▪ Batch job issues Halt instruction

▪ User logs off

▪ Process executes a service request to terminate

▪ Error or fault conditions

25
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.26
Process Termination
▪ Process executes last statement and asks the operating system to
delete it (exit)
• Output data from child to parent (via wait)
• Process’ resources are de-allocated by operating system
▪ Parent may terminate execution of children processes (abort)
—Child has exceeded allocated resources
—Task assigned to child is no longer required
• If parent is exiting
Some operating system do not allow child to continue if its parent terminates

– All children terminated - cascading termination

26
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.27

Threads
▪Process creation is heavy-weight while thread creation is light-weight
– Threads run within application
▪ Multiple tasks with the application can be implemented by separate threads
❑Update display
❑Fetch data
❑Spell checking
❑Answer a network request
▪ Can simplify code, increase efficiency
▪ Kernels are generally multithreaded.
• A traditional or heavyweight process is equal to a task with one thread.

27
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.28

Single and Multithreaded Processes

Benefits
Responsiveness

Resource Sharing

Economy

Scalability
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.29

Cooperating Processes
▪ Independent process cannot affect or be
affected by the execution of another process
▪ Cooperating process can affect or be
affected by the execution of another process
▪ Advantages of process cooperation
• Information sharing
• Computation speed-up
• Modularity
• Convenience

29
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.30

Synchronization
▪ Message passing may be either blocking or non-blocking
▪ Blocking is considered synchronous
• Blocking send has the sender block until the message
is received
• Blocking receive has the receiver block until a
message is available
▪ Non-blocking is considered asynchronous
• Non-blocking send has the sender send the message
and continue
• Non-blocking receive has the receiver receive a valid
message or null

30
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.31
Interproces Communication
Mechanism for processes to communicate and to
synchronize their actions.
• IPC is best provided by message-passing systems.
• IPC facility provides two operations:
– send(message) – message size fixed or variable
– receive(message)
• If P and Q wish to communicate, they need to:
– establish a communication link between them
– exchange messages via send/receive
• Processes can communicate in two ways:
– Direct communication
– Indirect communication
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.32
Direct Communication
Processes must name each other explicitly:
– send (P, message) – send a message to process P
– receive(Q, message) – receive a message from process Q
• Properties of communication link
– Links are established automatically.
– A link is associated with exactly one pair (two processes)
of communicating processes.
– Between each pair there exists exactly one link.
– The link may be unidirectional, but is usually
bidirectional
Disadvantage of Direct Communication:
The names of processes must be known - they can't be easily
changed since they are explicitly named in the send and receive.

32
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.33 Indirect Communication
Messages are directed and received from mailboxes (also referred
to as ports).
– Each mailbox has a unique id.
– Processes can communicate only if they share a mailbox.
• Properties of communication link:
– Link established only if processes share a common mailbox
– A link may be associated with many processes.
– Each pair of processes may share several communication
links.
– Link may be unidirectional or bi-directional.
• The O.S. provides a mechanism that allows a process:
– create a new mailbox
– send and receive messages through mailbox
– destroy a mailbox

33
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.34 Indirect Communication (Continued)
Example: Mailbox sharing
– P1, P2, and P3 share mailbox A.
– P1, sends; P2 and P3 receive.
– Who gets the message?
• Solutions:
– Allow a link to be associated with at most two processes
– Allow only one process at a time to execute a receive
operation.
– Allow the system to select arbitrarily the receiver.
Sender is notified who the receiver was

34
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.35

Q&A
Thank You

35
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.36

Buffering
Queue of messages attached to the link;
implemented in one of three ways.
1. Zero capacity – 0 messages.
Sender must wait for receiver (rendezvous).
2. Bounded capacity – finite length of n messages.
Sender must wait if link full.
3. Unbounded capacity – infinite length.
Sender never waits.

36
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.37
QA
1.Process is
a)program in High level language kept on disk
b)contents of main memory
c) A program in execution d) Job in secondary memory
2. Which of the following is not the state of a process ?
a) New b) Old c) Waiting d) Running
3.What is the ready state of a process?
a) when process is scheduled to run after some execution
b) when process is unable to run until some task has been
completed
c) when process is using the CPU
d) none of the mentioned
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.38 QA
4.What is interprocess communication?
a) communication within the process
b)communication between two process
c) communication between two threads of same process
d) none mentioned
5.The address of the next instruction to be executed by the
current process is provided by the
a) CPU registers b) program counter
c) process stack d) pipe
6.In Unix, Which system call creates the new process?
a) fork b) create c) new d) none of the mentioned

38
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning
1.39
QA
7. A Process Control Block(PCB) does not contain which
of the following :
a) Code b) Stack c) Program Counter
d) bootstrap program
8. The Process Control Block is :
a) Process type variable b) Data Structure
c) a secondary storage section d) a Block in memory
9. The degree of multi-programming is :
a) No. of processes executed per unit time
b) No. of processes in the ready queue
c) No. of processes in the I/O queue
d) No. of processes in memory

Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning


1.40

QA
10. The child process completes execution, but the parent
keeps executing, then the child process is known as :
a) Orphan b) Zombie c) Body d) Dead
11. Context switch is involved in
a) Switching computer on b) Switching CPU from one
process to another
c) Switching a process on d) None of these
12. Scheduling a process from Ready Queue to CPU is done
by
a) Short term scheduler b) Midlle term Scheduler
c) Long-Term Scheduler d) Dispatcher

Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning


1.41

QA

41
Silberschatz, et al, Operating System Concepts Compiled by: Samuel Banning

You might also like