COSC 303:Operating Systems
Lecture 2: Process
Computer Science Department, Gombe State University
2021/2022 Session
1
Lecture Outline
• Process Concept
• Process Scheduling and Queues
• Operations on Processes
• Inter-process Communication
2
Process Concept and Process
Management
3
Process Concept
• Process: A program in execution
– process execution must progress in sequential fashion
• A process includes:
• text – code – section (program counter – PC)
• stack section (stack pointer)
• data section
• set of open files currently used
• set of I/O devices currently used
• An operating system executes a variety of programs:
• Batch systems: jobs
• Time-shared systems: user programs or tasks
– We will use the terms job and process almost interchangeably
4
Process: program in execution
• If we have a single program running in the system, then the task of OS is easy:
– load the program, start it and program runs in CPU
– (from time to time it calls OS to get some service done)
• But if we want to start several processes, then the running program in CPU
(current process) has to be stopped for a while and other program (process)
has to run in CPU.
– Process management becomes an important issue
– To do process switch, we have to save the state/context (register values)
of the CPU which belongs to the stopped program, so that later the
stopped program can be re-started again as if nothing has happened.
5
Process =! Program
• What is the difference between a process and a program ?
6
Process State
• 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
In a single-CPU system, only one process may be in running state; many
processes may be in ready and waiting states.
7
Diagram of Process State
8
Process Control Block (PCB)
PCB Contains information associated with each
process
– Process state (ready, running, waiting,
etc)
– Program counter (PC)
– CPU registers
– CPU scheduling information
• Priority of the process, etc.
– Memory-management information
• Text/data/stack section pointers,
sizes, etc.
• pointer to page table, etc.
– Accounting information
• CPU usage, clock time so far, …
– I/O status information
• List of I/O devices allocated to the
process, a list of open files, etc.
PCB
9
Process Scheduling and Queues
10
Process Scheduling
• In a multiprogramming or time-sharing system, there may be multiple
processes ready to execute.
• We need to select one them and give the CPU to that.
– This is scheduling (decision).
– There are various criteria that can be used in the scheduling
decision.
• The scheduling mechanism (dispatcher) than assigns the selected
process to the CPU and starts execution of it.
Select Dispatch
(Scheduling Algorithm) (mechanism)
11
Process Scheduling Queue
• Ready queue is one of the many queues Process/CPU scheduling
that a process may be added
Device
– CPU scheduling schedules from ready
queue
queue. CPU Device
• Other queues possible:
– Job queue – set of all processes in the Ready Device
system queue queue
– Device queues – set of processes Device
waiting for an I/O device
• A process will wait in such a queue Memory
until I/O is finished or until the waited
event happens
• Processes migrate among the various
queues during execution Job queue
12
Representation of Process Scheduling
CPU Scheduler
ready queue
I/O queue
13
Schedulers
• Long-term scheduler (or job scheduler) – selects which processes
should be brought into the ready queue
• Short-term scheduler (or CPU scheduler) – selects which process
should be executed next and allocates CPU
Short-term
scheduler
CPU
ready queue
Long-term Main Memory
scheduler
job queue
14
Schedulers
• 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
– i.e. number of processes in memory
– Can also control kind of processes in memory!
• What kind of processes will be in memory?
– A good mix of IO bound and CPU bound processes
15
Process Behavior
• 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
• CPU burst: the execution of the program in CPU between two
I/O requests (i.e. time period during which the process wants to
continuously run in the CPU without making I/O)
– We may have a short or long CPU burst.
I/O bound CPU bound
waiting waiting
16
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 overhead; the system does no useful work
while switching
• Time dependent on hardware support
17
CPU Switch from Process to Process
18
Process Creation and Termination
19
Process Creation
• Parent process create children processes, which, in turn create other
processes, forming a tree of processes
• Generally, process identified and managed via a process identifier
(pid)
Process
• Resource sharing alternatives:
– Parent and children share all resources
Process Process
– Children share subset of parent’s resources
– Parent and child share no resources
• Execution alternatives:
– Parent and children execute concurrently Process Process Process
– Parent waits until children terminate
20
Process Creation (Cont)
• Child’s address space?
Parent Child
1)
Child has a new address space. AS AS
Child’s address space can contain:
– 1) the copy of the parent (at creation)
– 2) has a new program loaded into it
Parent Child
2)
AS AS
• 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
21
Process Termination
• Process executes last statement and asks the operating system to
delete it (can use exit system call)
– Output data from child to parent (via wait)
– Process’ resources are de-allocated by operating system
• Parent may terminate execution of children processes (abort) under
the following conditions:
– Child has exceeded allocated resources
– Task assigned to child is no longer required
– If parent is exiting
• Some operating systems do not allow child to continue if its
parent terminates
– All children terminated - cascading termination
22
Inter-process Communication (IPC)
23
Cooperating Processes and the need for
Interprocess Communication
• Processes within a system may be independent or cooperating
– 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
Application
• Reasons for process cooperation
– Information sharing
Process Process Process
– Computation speed-up
– Modularity (application will
be divided into modules/sub-tasks)
– Convenience (may be better to cooperating process
work with multiple processes)
The overall application is designed
to consist of cooperating processes
24
IPC Mechanisms
• Cooperating processes require a facility/mechanism for inter-process
communication (IPC)
• There are two basic IPC models provided by most systems:
1) Shared memory model
processes use a shared memory to exchange data
2) Message passing model
processes send messages to each other through the kernel
25
Communication Models
message passing approach shared memory approach
26
Shared Memory IPC Mechanism
• A region of shared memory is
established between (among) two or
more processes. Process A
– via the help of the operating system
kernel (i.e. system calls).
shared region
• Processes can read and write shared
memory region (segment) directly as
ordinary memory access (pointer Process B
access)
– During this time, kernel is not
involved.
– Hence it is fast
Kernel
27
Shared Memory IPC Mechanism
• To illustrate use of an IPC mechanism, a general model problem, called
producer-consumer problem, can be used. A lot of problems look like this.
– We have a producer, a consumer, and data is sent from producer to
consumer.
– We need buffer of items that can be filled by producer and emptied by
consumer.
• unbounded-buffer places no practical limit on the size of the buffer
• bounded-buffer assumes that there is a fixed buffer size
Buffer
Producer Produced Items Consumer
Process Process
We can solve this problem via shared memory IPC mechanism
28
Message Passing IPC Mechanism
• Another mechanism for processes to communicate and to synchronize their
actions
• With message passing system processes communicate with each other
without resorting to shared variables
• This IPC facility provides two operations: messages
– send(message) – message size fixed or variable passed
– receive(message) through
• If P and Q wish to communicate, they need to: P Q
– establish a (logical) communication link
between them Logical
– exchange messages via send/receive Communication
Link
29
Implementation in a system
• The messaging passing facility can be • Implementation Questions
implemented in various ways. • How are links established?
– Explicitly by the process? Or
• That means the facility may have implicitly by the kernel?
different features depending on the
system • Can a link be associated with more
than two processes?
• How many links can there be between
every pair of communicating processes?
• What is the capacity of a link?
• Is the size of a message that the link
can accommodate fixed or variable?
• Is a link unidirectional or bi-directional?
30
Naming: Identifying the receiver
• Naming (how do we identify the receiver)
– Direct naming and communication
• Receiver processes is explicitly specified
– send (P, message) – send a message to process P
– receive(Q, message) – receive a message from process Q
– Indirect naming and communicaiton
• Messages are directed and received from mailboxes (also
referred to as ports)
– send (mqid, message) Process
Process
– receive (mqid, message)
send() receive()
Mailbox (mqid)
{.. {…
{ }
Kernel
31
Mailbox operations, Problems, and
Solutions
• Operations
– create a new mailbox
– send/receive messages through mailbox
– destroy a mailbox
• Problems
– P1, P2 and P3 share mailbox A.
– P1 sends message, P2 and P3 receive… who gets message??
• Solutions
– disallow links between more than 2 processes
– allow only one process at a time to execute receive operation
– allow system to arbitrarily select receiver and then notify sender
32
Synchronization
• How does the sender/receiver behave if it can not send/receive the
message immediately
– Depend if Blocking or Non-Blocking communication is used
• Blocking is considered synchronous
– Sender blocks block until receiver or kernel receives
– Receiver blocks until message available
• Non-blocking is considered asynchronous
– Sender sends the message really or tries later, but always returns
immediately
– Receiver receives a valid message or null, but always returns
immediately
33
References
• 1. Operating System Concepts, 7th and 8th editions, Silberschatz et al.
Wiley.
• 2. Modern Operating Systems, Andrew S. Tanenbaum, 3rd edition,
2009.
• 3. The slides here are adapted/modified from the textbook and its
slides: Operating System Concepts, Silberschatz et al., 7th & 8th
editions, Wiley.
34