Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Chapter 3: Processes
Operating System Concepts – 9th Edition
Modified by Dr. Neerja Mhaskar for CS 3SH3
Silberschatz, Galvin and Gagne ©2013
Process
Process is a program in execution
Program is passive entity stored on
disk (executable file), process is
active
Program
becomes process when
executable file loaded into memory
Operating System Concepts – 9th Edition
3.2
Silberschatz, Galvin and Gagne ©2013
Questions
Can you have one program and many processes?
Operating System Concepts – 9th Edition
3.3
Silberschatz, Galvin and Gagne ©2013
Process in Memory
Text section - compiled program code, read in from the non-
volatile storage when the program is launched.
Data Section - global and static variables, which are allocated
and initialized prior to executing main.
Heap - Dynamic Memory Allocation
managed via calls to new, delete, malloc, free, etc.
Stack - is used for local variables. Space on the stack is reserved
for local variables when they are declared ( at function entrance
or elsewhere, depending on the language ), and the space is
freed up when the variables go out of scope.
Operating System Concepts – 9th Edition
3.4
Silberschatz, Galvin and Gagne ©2013
Process in Memory
Operating System Concepts – 9th Edition
3.5
Silberschatz, Galvin and Gagne ©2013
Question
Stack and Heap start at the opposite ends of the process's
free space and grow towards each other.
What happens if the stack and the heap
meet?
Operating System Concepts – 9th Edition
3.6
Silberschatz, Galvin and Gagne ©2013
Process State
As a process executes, it changes state
new: The process is being created
ready: The process is waiting to be assigned to a
processor
running: Instructions are being executed
waiting: The process is waiting for some event to
occur
terminated: The process has finished execution
Operating System Concepts – 9th Edition
3.7
Silberschatz, Galvin and Gagne ©2013
Diagram of Process State
Operating System Concepts – 9th Edition
3.8
Silberschatz, Galvin and Gagne ©2013
Process Control Block (PCB)
Information associated with each process
(also called task control block)
Process state – running, waiting, etc
Program number – Process ID
CPU registers and program counter –
contents of all process-centric registers
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 – 9th Edition
3.9
Silberschatz, Galvin and Gagne ©2013
Process Representation in Linux
• Processes in Linux are referred to as tasks.
Represented by the C structure task_struct
pid t_pid; /* process identifier */
long state; /* state of the process */
unsigned int time_slice /* scheduling information */
struct task_struct *parent; /* this process’s parent */
struct list_head children; /* this process’s children */
struct files_struct *files; /* list of open files */
struct mm_struct *mm; /* address space of this process */
Operating System Concepts – 9th Edition
3.10
Silberschatz, Galvin and Gagne ©2013
Threads
Modern systems allow a process to
have multiple threads associated
with it.
These threads can execute
concurrently.
More on Threads in the next
chapter.
Operating System Concepts – 9th Edition
3.11
Silberschatz, Galvin and Gagne ©2013
Types of Processes
Processes can be described as either:
process – spends more time
doing I/O than computations, many short CPU
bursts
I/O-bound
process – spends more time
doing computations; few very long CPU
bursts
CPU-bound
Operating System Concepts – 9th Edition
3.12
Silberschatz, Galvin and Gagne ©2013
Process Scheduling
Objective: Maximize CPU use, quickly switch processes onto CPU
for time sharing
Process scheduler must meet the above objectives by implementing
suitable scheduling policies.
Operating 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.
Usually a separate device queue for each device.
Processes migrate among the various queues
Scheduler: selects a process from a queue.
Operating System Concepts – 9th Edition
3.13
Silberschatz, Galvin and Gagne ©2013
Ready Queue And Various I/O Device Queues
Operating System Concepts – 9th Edition
3.14
Silberschatz, Galvin and Gagne ©2013
Representation of Process Scheduling
Queueing diagram represents queues, resources, flows
Operating System Concepts – 9th Edition
3.15
Silberschatz, Galvin and Gagne ©2013
Schedulers
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)
Long-term scheduler (or job scheduler) – selects which processes should
be brought into the ready queue
Long-term scheduler is invoked infrequently (seconds, minutes)
(may be slow). Therefore can use sophisticated scheduling algorithms.
Typically, seen on a process intensive systems.
The long-term scheduler controls the degree of multiprogramming
and good process mix
Swapping: is the term used for removing a process from main memory store
on disk and later bring it back in from disk to continue execution
Medium – term scheduler: used to queue swapped jobs
can be added if degree of multiple programming needs to decrease.
Operating System Concepts – 9th Edition
3.16
Silberschatz, Galvin and Gagne ©2013
Context Switch
Context of a process is represented in the PCB (value of CPU
registers, the process state etc. See slide 3.1)
When CPU switches to another process, the system must save
the context of the old process and load the saved context for
the new process. The process is called context switching
Context-switch time is 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
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
Operating System Concepts – 9th Edition
3.19
Silberschatz, Galvin and Gagne ©2013
Process Creation
Parent process create children processes (using
system calls), which, in turn create other
processes, forming a tree of processes
A process is given an integer identifier called the
process identifier (pid)
In addition to PID of a process, its parent PID
(termed as PPID ) is stored as well.
Operating System Concepts – 9th Edition
3.20
Silberschatz, Galvin and Gagne ©2013
A Tree of Processes in Linux
init
pid = 1
login
pid = 8415
khelper
pid = 6
bash
pid = 8416
ps
pid = 9298
sshd
pid = 3028
kthreadd
pid = 2
pdflush
pid = 200
sshd
pid = 3610
tcsch
pid = 4005
emacs
pid = 9204
At system startup, init processes is executed
Hence the process PID = 1.
Init then launches all system daemons and user logins, and becomes
the parent of all other processes.
Operating System Concepts – 9th Edition
3.21
Silberschatz, Galvin and Gagne ©2013
Parent – Child Sharing
Resource sharing options
Parent and children share all resources
Children share subset of parent’s resources
Parent and child share no resources
Execution options
Parent and children execute concurrently
Parent waits until children terminate
Parent makes a wait() system call for a single child or
all its children.
Address space sharing options:
Child duplicate of parent (has the same program and data
as the parent)
Child has a new program loaded into it
Operating System Concepts – 9th Edition
3.22
Silberschatz, Galvin and Gagne ©2013
Unix fork() and exec()
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.23
Silberschatz, Galvin and Gagne ©2013
C Program Forking Separate Process
Operating System Concepts – 9th Edition
3.24
Silberschatz, Galvin and Gagne ©2013
Question?
How many processes created when the below code is executed?
int main()
{
printf("%d\n",getpid());
fork();
printf("%d\n",getpid());
fork();
printf("%d\n",getpid());
fork();
printf("%d\n",getpid());
return 0;
}
Operating System Concepts – 9th Edition
3.25
Silberschatz, Galvin and Gagne ©2013
Question
Construct the process tree (or tree of processes)
for the processes created by the code on the
previous slide (3.27)?
Operating System Concepts – 9th Edition
3.26
Silberschatz, Galvin and Gagne ©2013
Process Termination
After executing the last statement a process is terminated
Implicitly – using the return statement
Explicitly – using the exit() system call
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 – 9th Edition
3.27
Silberschatz, Galvin and Gagne ©2013
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.
Operating System Concepts – 9th Edition
3.28
Silberschatz, Galvin and Gagne ©2013
Child Process – Termination status
When a child process terminates, the parent can know the status of exit
using the wait()system call.
The call returns status information and the pid of the terminated process
pid = wait(&status);
Unix maintains a table of processes. This table contains the list of all
processes running and includes the process status.
If a parent process terminates, its entry is removed from the table.
If a child process terminates, its entry is removed from the table only after
the parent process invokes a wait().
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 – 9th Edition
3.29
Silberschatz, Galvin and Gagne ©2013
Wait()
wait() used to wait for the state changes in a child of
the calling process
Blocked until a child changes its status
Note that status change of any child will unblock the
parent
To block a particular child process use waitpid()
To block the parent till all child processes change
state loop through all child processes
Operating System Concepts – 9th Edition
3.30
Silberschatz, Galvin and Gagne ©2013
Interprocess Communication
Processes within a system may be
Independent or
Cooperating (it can affect or be affected by other processes,
including sharing data)
Reasons for cooperating processes:
Information sharing, computation speedup, modularity and
convenience
Cooperating processes need interprocess communication (IPC)
Two models of IPC
Shared memory
Message passing
Operating System Concepts – 9th Edition
3.31
Silberschatz, Galvin and Gagne ©2013
Communications Models
(a) Message passing.
Operating System Concepts – 9th Edition
(b) shared memory.
3.32
Silberschatz, Galvin and Gagne ©2013
Producer Consumer Problem
Buffer
Producer
Operating System Concepts – 9th Edition
Consumer
3.33
Silberschatz, Galvin and Gagne ©2013
Producer-Consumer Problem
Paradigm for cooperating processes, producer
process produces information that is consumed
by a consumer process
unbounded-buffer places no practical limit on
the size of the buffer
bounded-buffer assumes that there is a fixed
buffer size
Operating System Concepts – 9th Edition
3.34
Silberschatz, Galvin and Gagne ©2013
IPC – Shared Memory
An area of memory shared among the processes that wish
to communicate
The communication is under the control of the user
processes not the operating system.
Used for sharing large amount of data.
Major issues – synchronize process actions when
accessing shared memory.
Synchronization is discussed in details in Chapter 5.
Operating System Concepts – 9th Edition
3.35
Silberschatz, Galvin and Gagne ©2013
IPC – Message Passing
Mechanism for processes to communicate using
messages (and not a shared space).
Operating system provides message passing
capability.
Operating systems do so by providing two
operations:
send(message)
receive(message)
The message size is either fixed or variable
Operating System Concepts – 9th Edition
3.36
Silberschatz, Galvin and Gagne ©2013
Message Passing (Cont.)
If processes P and Q wish to communicate, they need to:
Establish a communication link between them
Exchange messages via send/receive
Implementation of communication link
Physical: (not in the scope of this course)
Logical:
Direct or indirect communication
Synchronous or asynchronous communication
Automatic or explicit buffering
Operating System Concepts – 9th Edition
3.37
Silberschatz, Galvin and Gagne ©2013
Direct Communication - Naming
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 pair of communicating
processes
Between each pair there exists exactly one link
The link may be unidirectional, but is usually bi-directional
Operating System Concepts – 9th Edition
3.38
Silberschatz, Galvin and Gagne ©2013
Indirect Communication - Mailbox
Messages are directed to 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
Operating System Concepts – 9th Edition
3.39
Silberschatz, Galvin and Gagne ©2013
Indirect Communication
Operations
create a new mailbox (port)
send and receive messages through mailbox
destroy 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 – 9th Edition
3.40
Silberschatz, Galvin and Gagne ©2013
Synchronization
Message passing may be either blocking or non-blocking
Blocking is considered synchronous
Blocking send -- the sender is blocked until the message is
received
Blocking receive -- the receiver is blocked until a message
is available
Non-blocking is considered asynchronous
Non-blocking send -- the sender sends the message and
continue
Non-blocking receive -- the receiver receives:
A valid message, or
Null message
Different combinations possible
If both send and receive are blocking, we have a rendezvous
Operating System Concepts – 9th Edition
3.41
Silberschatz, Galvin and Gagne ©2013
Buffering
Queue of messages attached to the link.
Implemented in one of three ways
1. Zero capacity – no messages are queued on a
link.
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
Operating System Concepts – 9th Edition
3.42
Silberschatz, Galvin and Gagne ©2013
End of Chapter 3
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013