Processes - CH 3
Processes - CH 3
Processes - CH 3
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Outline
▪ Process Concept
▪ Process Scheduling
▪ Operations on Processes
▪ Interprocess Communication (IPC)
▪ IPC in Shared-Memory Systems
▪ IPC in Message-Passing Systems
Operating System Concepts – 10th Edition 3.2 Silberschatz, Galvin and Gagne ©2018
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.
Operating System Concepts – 10th Edition 3.3 Silberschatz, Galvin and Gagne ©2018
Process
■ Fundamental to the structure of operating systems
A program in execution
Operating System Concepts – 10th Edition 3.5 Silberschatz, Galvin and Gagne ©2018
Process Concept
An operating system executes a variety of programs that run as a
process.
▪ Process – a program in execution; process execution must progress in
sequential fashion. No parallel execution of instructions of a single
process
▪ Multiple parts
• The program code, also called text section
• Current activity including program counter, processor registers
• Stack containing temporary data
Function parameters, return addresses, local variables
• Data section containing global variables
• Heap containing memory dynamically allocated during run time
Operating System Concepts – 10th Edition 3.6 Silberschatz, Galvin and Gagne ©2018
Executing Program (Process)
• Process
– A program in execution
– Most important abstraction in
an OS
– Comprises of
$gcc hello.c • Code
In the
• Data user space
• Stack of process
• Heap
Executable Process • State in the OS In the kernel
(a.out) $./a.out • Kernel stack space
– State contains: registers, list
of open files, related
processes, etc.
Operating System Concepts – 10th Edition 3.7 Silberschatz, Galvin and Gagne ©2018
Process Concept (Cont.)
▪ Program is passive entity stored on disk (executable file); process is
active
• Program becomes process when an executable file is 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 the same program
Operating System Concepts – 10th Edition 3.8 Silberschatz, Galvin and Gagne ©2018
Process Memory Map
Operating System Concepts – 10th Edition 3.10 Silberschatz, Galvin and Gagne ©2018
Program ≠ Process
Program Process
Operating System Concepts – 10th Edition 3.11 Silberschatz, Galvin and Gagne ©2018
Process Address Map in xv6
Text
(instructions)
0
Operating System Concepts – 10th Edition 3.12 Silberschatz, Galvin and Gagne ©2018
Process Management
Operating System Concepts – 10th Edition 3.13 Silberschatz, Galvin and Gagne ©2018
Process State
Operating System Concepts – 10th Edition 3.14 Silberschatz, Galvin and Gagne ©2018
Diagram of Process State (five states)
Operating System Concepts – 10th Edition 3.15 Silberschatz, Galvin and Gagne ©2018
Admit Dispatch
Release
New Ready Running Exit
Timeout
Occurs
Event
Suspend
Suspend Blocked
Operating System Concepts – 10th Edition 3.16 Silberschatz, Galvin and Gagne ©2018
States of a Process in Operating Systems
https://www.geeksforgeeks.org/states-of-a-process-in-operating-systems/
Operating System Concepts – 10th Edition 3.17 Silberschatz, Galvin and Gagne ©2018
Process Termination
There must be a means for a process to indicate its
completion
A batch job should include a HALT instruction or an
explicit OS service call for termination
For an interactive application, the action of the user will
indicate when the process is completed (e.g. log off,
quitting an application)
Process Control Block (PCB)
Information associated with each process(also called task control block)
▪ Process identifier
▪ Process state – running, waiting, etc.
▪ Program counter – location of instruction to next execute
▪ CPU registers – contents of all process-centric registers – no
need for the variables as they are still in main memory
▪ CPU scheduling information- priorities, scheduling queue
pointers
▪ Memory-management information – memory allocated to the
process
▪ Accounting information – CPU used, clock time elapsed
since start, time limits
▪ I/O status information – I/O devices allocated to process,
list of open files
Operating System Concepts – 10th Edition 3.20 Silberschatz, Galvin and Gagne ©2018
CPU Switch From Process to Process
A context switch occurs when the CPU switches from one process
to another.
Operating System Concepts – 10th Edition 3.18 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 3.19 Silberschatz, Galvin and Gagne ©2018
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 of a process represented in the PCB
▪ 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
• Some hardware provides multiple sets of registers per CPU
multiple contexts loaded at once
• We do not need to save the registers of a process once switching as
there are for example two sets of registers.
Operating System Concepts – 10th Edition 3.21 Silberschatz, Galvin and Gagne ©2018
Address Main Memory Program Counter
Process Execution 0
100
8000
12000
Process C
Process B
Process C
Dispatcher
0 5 10 15 20 25 30 35 40 45 50
Operating System Concepts – 10th Edition 3.25 Silberschatz, Galvin and Gagne ©2018
Sharing the CPU
time
Operating System Concepts – 10th Edition 3.26 Silberschatz, Galvin and Gagne ©2018
Idle CPU Cycles
CPU is idle when executing app waits for an event. Reduced performance.
Operating System Concepts – 10th Edition 3.27 Silberschatz, Galvin and Gagne ©2018
When OS supports Multiprogramming
time
Operating System Concepts – 10th Edition 3.28 Silberschatz, Galvin and Gagne ©2018
Multiprogramming could cause starvation
while(1);
App1 App2
time
Operating System Concepts – 10th Edition 3.29 Silberschatz, Galvin and Gagne ©2018
Process Scheduling Queues
The Operating System maintains the following important process scheduling queues:
▪ Job queue − This queue keeps all the processes in the system (mostly in mainframe).
• PCs usually do not have this queue.
▪ Ready queue − This queue keeps a set of all processes residing in main memory,
ready and waiting to execute by CPU. A new process is always put in this queue.
▪ Device queues − The processes which are blocked due to unavailability of an I/O
device constitute this queue.
• There is generally a separate device queue for each device
▪ Processes migrate among the various queues
Operating System Concepts – 10th Edition 3.30 Silberschatz, Galvin and Gagne ©2018
Ready Queue Release
Admit Dispatch
Processor
Timeout
Blocked Queue
Event Event Wait
Occurs
(a) Single blocked queue
Timeout
Event 1 Queue
Event 1 Event 1 Wait
Occurs
Event 2 Queue
Event 2 Event 2 Wait
Occurs
Event n Queue
Event n Event n Wait
Occurs
Operating System Concepts – 10th Edition 3.32 Silberschatz, Galvin and Gagne ©2018
Types of Schedulers
1. Long term – performance – Makes a decision about how many processes should
be made to stay in the ready state, this decides the degree of multiprogramming. Once
a decision is taken it lasts for a long time hence called long term scheduler. It is called
job scheduler as well.
2. Short term – Context switching time – Short term scheduler will decide which
process to be executed next and then it will call dispatcher. A dispatcher is a software
that moves process from ready to run and vice versa. In other words, it is context
switching. It is called CPU scheduler as well.
Operating System Concepts – 10th Edition 3.33 Silberschatz, Galvin and Gagne ©2018
Multiprogramming
CPU and IO Bound Processes:
If the process is intensive in terms of CPU operations then it is called CPU bound process.
Similarly, If the process is intensive in terms of I/O operations then it is called IO bound
process.
Multiprogramming – We have many processes ready to run. There are two types of
multiprogramming:
1. Pre-emption – Process is forcefully removed from CPU. Pre-emption is also called as
time sharing or multitasking.
2. Non pre-emption – Processes are not removed until they complete the execution.
Degree of multiprogramming:
The number of processes that can reside in the ready state at maximum decides the
degree of multiprogramming, e.g., if the degree of programming = 100, this means 100
processes can reside in the ready state at maximum.
Operating System Concepts – 10th Edition 3.34 Silberschatz, Galvin and Gagne ©2018
Operations on Processes
Operating System Concepts – 10th Edition 3.35 Silberschatz, Galvin and Gagne ©2018
Process Creation
Operating System Concepts – 10th Edition 3.36 Silberschatz, Galvin and Gagne ©2018
Process Creation (Cont.)
▪ Address space
• Child duplicate of parent
• Child has a program loaded into it
▪ 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
• Parent process calls wait()waiting for the child to terminate
Operating System Concepts – 10th Edition 3.37 Silberschatz, Galvin and Gagne ©2018
Process Termination
▪ Process executes last statement and then asks the operating system
to delete it using the exit() system call.
• Returns status data from child to parent (via wait())
• Process’ resources are deallocated by operating system
▪ Parent may terminate the execution of children processes using the
abort() system call. Some reasons for doing so:
• Child has exceeded allocated resources
• Task assigned to child is no longer required
• The parent is exiting, and the operating systems does not allow a
child to continue if its parent terminates
Operating System Concepts – 10th Edition 3.38 Silberschatz, Galvin and Gagne ©2018
Process Termination
▪ Some operating systems do not allow child to exists if its parent has
terminated. If a process terminates, then all its children must also be
terminated.
• cascading termination. All children, grandchildren, etc., are
terminated.
• The termination is initiated by the operating system.
▪ The parent process may wait for termination of a child process by
using the wait()system call. The call returns status information
and the pid of the terminated process
pid = wait(&status);
▪ If no parent waiting (did not invoke wait()) process is a zombie
▪ If parent terminated without invoking wait(), process is an orphan
Operating System Concepts – 10th Edition 3.39 Silberschatz, Galvin and Gagne ©2018
Multiprocess Architecture – Chrome Browser
Operating System Concepts – 10th Edition 3.40 Silberschatz, Galvin and Gagne ©2018
Interprocess Communication
Operating System Concepts – 10th Edition 3.41 Silberschatz, Galvin and Gagne ©2018
Communications Models
Operating System Concepts – 10th Edition 3.42 Silberschatz, Galvin and Gagne ©2018
Communications Models
Operating System Concepts – 10th Edition 3.43 Silberschatz, Galvin and Gagne ©2018
Producer-Consumer Problem
Operating System Concepts – 10th Edition 3.44 Silberschatz, Galvin and Gagne ©2018
IPC – Shared Memory
▪ An area of memory shared among the processes that wish to
communicate
▪ The communication is under the control of the users processes not the operating
system.
▪ Typically, a shared memory region resides in the address space of the process
creating the shared memory segment.
▪ Other processes that wish to communicate using this shared memory segment
must attach it to their address space.
▪ The processes are also responsible for ensuring that they are not writing to the
same location simultaneously.
▪ Major issues is to provide mechanism that will allow the user processes to
synchronize their actions when they access shared memory.
▪ Example: Producer-Consumer
▪ Synchronization is discussed in great details in Chapters 6 & 7.
Operating System Concepts – 10th Edition 3.45 Silberschatz, Galvin and Gagne ©2018
IPC – Message Passing
Operating System Concepts – 10th Edition 3.46 Silberschatz, Galvin and Gagne ©2018
Types of Communication
▪ 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
▪ Indirect Communication:
▪ Messages are directed and received from mailboxes (also referred to as ports
(in Unix, Linux))
• Each mailbox has a unique id
• Processes can communicate only if they share a mailbox
▪ Operations
• Create a new mailbox (port)
• Send and receive messages through mailbox
• Delete a mailbox
▪ Primitives are defined as:
• send(A, message) – send a message to mailbox A
• receive(A, message) – receive a message from mailbox A
Operating System Concepts – 10th Edition 3.47 Silberschatz, Galvin and Gagne ©2018
Indirect Communication
Indirect Communication (Cont.)
▪ 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.
Operating System Concepts – 10th Edition 3.50 Silberschatz, Galvin and Gagne ©2018
Synchronization
Message passing may be either blocking or non-blocking
Operating System Concepts – 10th Edition 3.51 Silberschatz, Galvin and Gagne ©2018
Buffering
▪ Queue of messages attached to the link; implemented in one of three ways:
1. Zero capacity – no (0) messages are queued. Sender must wait for
receiver (rendezvous)
2. Bounded capacity – finite length of n messages Sender must wait if the
queue is full
3. Unbounded capacity – infinite length Sender never waits
Operating System Concepts – 10th Edition 3.53 Silberschatz, Galvin and Gagne ©2018
End of Chapter 3
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018