Chapter 3: Processes
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Outline
▪ Process Concept
▪ Process Scheduling
▪ Operations on Processes
▪ Interprocess Communication
▪ IPC in Shared-Memory Systems
▪ IPC in Message-Passing Systems
▪ Examples of IPC Systems
▪ Communication in Client-Server 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 inter-process communication using shared
memory and message passing.
▪ Design programs that uses pipes and POSIX shared memory to
perform inter-process communication.
▪ Describe client-server communication using sockets and remote
procedure calls.
▪ Design kernel modules that interact with the Linux operating system.
Operating System Concepts – 10th Edition 3.3 Silberschatz, Galvin and Gagne ©2018
Process Concept
▪ An operating system executes a variety of programs that run as a process
▪ Early computers were batch systems that executed jobs, followed by the emergence
of time-shared systems that ran user programs, or tasks.
▪ Now a days, Computers are multitasking
▪ Embedded Computers can only do single task at a time
▪ The operating system may need to support its own internal programmed activities,
such as memory management
▪ In many respects, all these activities are similar, so we call all of them processes
▪ 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 and processor registers
• Stack containing temporary data
Function parameters, return addresses, local variables
• Data section containing global variables
• Heap
Operating containing
System Conceptsth memory dynamically3.4allocated during run time
– 10 Edition Silberschatz, Galvin and Gagne ©2018
Process Concept (Cont.)
▪ The sizes of the text and data sections are fixed, as their sizes do not change
during program run time
▪ Stack and heap sections can shrink and grow dynamically during program
execution
▪ Each time a function is called, an activation record containing function
parameters, local variables, and the return address is pushed onto the
stack
▪ When control is returned from the function, the activation record is popped
from the stack.
▪ Similarly, the heap will grow as memory is dynamically allocated, and will
shrink when memory is returned to the system
▪ Although the stack and heap sections grow toward one another, the
operating system must ensure they do not overlap one another
Operating System Concepts – 10th Edition 3.5 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
• Although two processes may be associated with the same program, they are
nevertheless considered two separate execution sequences. For instance,
several users may be running different copies of the mail program, or
• Each of these is a separate process; and although the text sections are
equivalent, the data, heap, and stack sections vary. It is also common to have
a process that spawns many processes as it runs
▪ A process can itself be an execution environment for other code
• The Java programming environment provides a good example. In most
circumstances, an executable Java program is executed within the Java
virtual machine (JVM). The JVM executes as a process that interprets the loaded
Java code and takes actions (via native machine instructions) on behalf of that
code. For example, to run the compiled Java program Program.class, we would
enter java Program The command java runs the JVM as an ordinary process,
which in turns executes the Java program Program in the virtual machine
Operating System Concepts – 10th Edition 3.6 Silberschatz, Galvin and Gagne ©2018
Process in Memory
Operating System Concepts – 10th Edition 3.7 Silberschatz, Galvin and Gagne ©2018
Memory Layout of a C Program
▪ The global data section is
divided into different
sections for
▪ (a) initialized data and
▪ (b) uninitialized data
▪ A separate section is provided for
the argc and argv parameters
passed to the main() function
Operating System Concepts – 10th Edition 3.8 Silberschatz, Galvin and Gagne ©2018
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 (such as
an 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
▪ These names are arbitrary, and they vary across operating system
▪ The states that they represent are found on all systems, however.
Certain operating systems also more finely delineate process
states
▪ It is important to realize that only one process can be running on any
processor core at any instant
▪ Many processes may be ready and waiting
Operating System Concepts – 10th Edition 3.9 Silberschatz, Galvin and Gagne ©2018
Diagram of Process State
Operating System Concepts – 10th Edition 3.10 Silberschatz, Galvin and Gagne ©2018
Process Control Block (PCB)
➢ Each Process in represented in OS by a Process Control
➢ Block Information associated with each process(also
called task control block)
▪ Process state – may be new, ready, running, waiting, halted, and so
on
▪ Program counter – location of instruction to next execute
▪ CPU registers – The registers vary in number and type, depending on
the computer architecture. They include accumulators, index registers,
stack pointers, and general-purpose registers, plus any condition-
code information. Along with the program counter, this state information
must be saved when an interrupt occurs, to allow the process to be
continued correctly afterward when it is rescheduled to run
▪ CPU scheduling information- priorities, scheduling queue pointers
▪ Memory-management information – memory allocated to the
process (base and limit registers and the page tables, or the segment
tables)
▪ Accounting information – CPU used, clock time elapsed since start,
time limits, Job or Process numbers,
▪ I/O status information – I/O devices allocated to process, list of open
files
Operating System Concepts – 10th Edition 3.11 Silberschatz, Galvin and Gagne ©2018
Threads
▪ A thread is a single sequential flow of execution of tasks of a process so it is
also known as thread of execution
▪ So far, process has a single thread of execution
▪ For example, when a process is running a word-processor program, a single thread
of instructions is being executed. This single thread of control allows the process to
perform only one task at a time. Thus, the user cannot simultaneously type in
characters and run the spell checker
▪ Consider having multiple program counters per process
• Multiple locations can execute at once
Multiple threads of control -> threads
▪ Must then have storage for thread details, multiple program counters in PCB
▪ This feature is especially beneficial on multicore systems, where multiple threads
can run in parallel. A multithreaded word processor could, for example, assign one
thread to manage user input while another thread runs the spell checker
▪ Explore in detail in Chapter 4
Operating System Concepts – 10th Edition 3.12 Silberschatz, Galvin and Gagne ©2018
Process Representation in Linux
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 – 10th Edition 3.13 Silberschatz, Galvin and Gagne ©2018
Process Scheduling
▪ The objective of multiprogramming is to have some process running at all
times so as to maximize CPU utilization
▪ The objective of time sharing is to switch a CPU core among processes so
frequently that users can interact with each program while it is running
▪ Process scheduler selects among available processes for next execution on CPU
core
1. Each CPU core can run one process at a time
2. For a system with a single CPU core, there will never be more than one process
running at a time
3. Whereas a multicore system can run multiple processes at one time
4. More processes than cores, excess processes wait until a core is free and can be
rescheduled
▪ Degree of multiprogramming: The number of processes currently in memory
▪ Goal -- Maximize CPU use, quickly switch processes onto CPU core
▪ Maintains scheduling queues of processes
• Ready queue – set of all processes residing in main memory, ready and waiting to execute
• Wait queues – set of processes waiting for an event (i.e., I/O)
• Processes migrate among the various queues
Operating System Concepts – 10th Edition 3.14 Silberschatz, Galvin and Gagne ©2018
Process Scheduling
▪ General Behavior: Balancing the objectives of multiprogramming and time
sharing also requires taking the general behavior of a process into account
▪ Most processes can be described as either I/O bound or CPU bound
▪ An I/O-bound process is one that spends more of its time doing I/O than it
spends doing computations
▪ A CPU-bound process, in contrast, generates I/O requests infrequently, using
more of its time doing computations
Operating System Concepts – 10th Edition 3.15 Silberschatz, Galvin and Gagne ©2018
Ready and Wait Queues
▪ As processes enter the system, they are put into a ready queue, where they
are ready and waiting to execute on a CPU’s core
• This queue is generally stored as a linked list
a ready-queue header contains pointers to the first PCB in the list
Each PCB includes a pointer field that points to the next PCB in the
ready queue
▪ Processes that are waiting for a certain event to occur — such as
completion of I/O — are placed in a wait queue
Operating System Concepts – 10th Edition 3.16 Silberschatz, Galvin and Gagne ©2018
Ready and Wait Queues
Operating System Concepts – 10th Edition 3.17 Silberschatz, Galvin and Gagne ©2018
Representation of Process Scheduling
❑ Two types of queues are present: the ready queue and a set of wait
queues
❑ The circles represent the resources that serve the queues, and the
arrows indicate the flow of processes in the system
❑ A new process is initially put in the ready queue
a) It waits there until it is selected for execution, or dispatched
b) Once the process is allocated a CPU core and is executing, one of
several events could occur
c) The process could issue an I/O request and then be placed in an
I/O wait queue
d) The process could create a new child process and then be placed
in a wait queue while it awaits the child’s termination
e) The process could be removed forcibly from the core, as a result
of an interrupt or having its time slice expire, and be put back in
the ready queue
Operating System Concepts – 10th Edition 3.18 Silberschatz, Galvin and Gagne ©2018
Representation of Process Scheduling
Operating System Concepts – 10th Edition 3.19 Silberschatz, Galvin and Gagne ©2018
CPU Scheduling
❑ A process migrates among the ready queue and various wait queues
throughout its lifetime
❑ The role of the CPU scheduler is to select from among the processes
that are in the ready queue and allocate a CPU core to one of them
❑ The CPU Scheduler must select a new process for the CPU frequently
❑ An I/O-bound process may execute for only a few milliseconds before
waiting for an I/O request
❑ A CPU-bound process will require a CPU core for longer durations, the
scheduler is unlikely to grant the core to a process for an extended period
❑ the CPU scheduler executes at least once every 100 milliseconds
❑ Some operating systems have an intermediate form of scheduling, known
as Swapping, whose key idea is that sometimes it can be advantageous to
remove a process from memory and thus reduce the degree of
multiprogramming
Operating System Concepts – 10th Edition 3.20 Silberschatz, Galvin and Gagne ©2018
Context Switch
▪ Interrupts cause the operating system to change a CPU core from its current task
and to run a kernel routine
▪ Occurs frequently on general purpose computer
▪ 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
▪ It includes the value of the CPU registers, the process state and memory-
management information
▪ Save State: When switching from one mode to another
▪ Resume State: To resume operation
▪ Switching speed varies from machine to machine, depending on the memory
speed, the number of registers special instructions (a single instruction to load or store all registers)
▪ 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
Operating System Concepts – 10th Edition 3.21 Silberschatz, Galvin and Gagne ©2018
CPU Switch From Process to Process
A context switch occurs when the
CPU switches from one process to
another.
the more complex the operating system, the
greater the amount of work that must be
done during a context switch
Operating System Concepts – 10th Edition 3.22 Silberschatz, Galvin and Gagne ©2018
Multitasking in Mobile Systems
▪ Some mobile systems (e.g., early version of iOS) allow only one
process to run, others suspended
▪ Due to screen real estate, user interface limits iOS provides for a
• Single foreground process- controlled via user interface
• Multiple background processes– in memory, running, but not
on the display, and with limits
• Limits include single, short task, receiving notification of events,
specific long-running tasks like audio playback
▪ Android runs foreground and background, with fewer limits
• Background process uses a service to perform tasks
• Service can keep running even if background process is
suspended
• Service has no user interface, small memory use
Operating System Concepts – 10th Edition 3.23 Silberschatz, Galvin and Gagne ©2018
Operations on Processes
▪ The processes in most systems can execute concurrently
▪ They may be created and deleted dynamically
▪ System must provide mechanisms for:
• Process creation
• Process termination
Operating System Concepts – 10th Edition 3.24 Silberschatz, Galvin and Gagne ©2018
Process Creation
▪ A process may create several new processes
▪ The creating process is called a parent process, and the new processes are
called the children of that process
▪ 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)
• Used as an index to access various attributes of a process within the kernel
▪ Resource sharing options (CPU time, Memory Files, I/O Devices, Data Input)
• Parent and children share all resources (Direct from OS)
• Children share subset of parent’s resources
may have to partition its resources among its children, or it may be able to share some
resources (such as memory or files) among several of its children
• Parent and child share no resources
▪ Execution options
• Parent and children execute concurrently
• Parent waits until children terminate
▪ Linux systems provide the pstree command, which displays a tree of all
processes in the system
Operating System Concepts – 10th Edition 3.25 Silberschatz, Galvin and Gagne ©2018
Process Creation (Cont.)
▪ Address space
• Child duplicate of parent
• Child has a program loaded into it
▪ In UNIX,
• each process is identified by its process identifier
• A new process is created by the fork() system call
• The new process consists of a copy of the address space of the original process. This
mechanism allows the parent to communicate with its child
• Both processes (the parent and the child) continue execution at the instruction after the
fork(), with one difference: the return code for the fork() is zero for the new (child)
process, whereas the (nonzero) process identifier of the child is returned to the
parent
• After a fork() system call, one of the two processes typically uses the exec() system
call to replace the process’s memory space with a new program
• The exec() system call loads a binary file into memory (destroying the memory image
of the program containing the exec() system call) and starts its execution
• The parent can then create more children
• Parent process calls wait()waiting for the child to terminate
Operating System Concepts – 10th Edition 3.26 Silberschatz, Galvin and Gagne ©2018
A Tree of Processes in Linux
Root Parent: First Process
created when Computer Booted
Root Parent
Used to
logind: Manages clients that
manage
login to computer directly
clients that
connects to
system by
SSH shell
Client loggedin using bash shell
using bash command line interface user
created the process ps and vim editor
On UNIX and Linux systems, we can obtain a listing of processes by using
the ps command. For example, the command ps -el
Operating System Concepts – 10th Edition 3.27 Silberschatz, Galvin and Gagne ©2018
C Program Forking Separate Process
Signed Integer Type
Function to specify
path of file
Operating System Concepts – 10th Edition 3.28 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
• TerminateProcess() in Windows
Operating System Concepts – 10th Edition 3.29 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 wait() system call is passed a parameter that allows the parent to obtain the
exit status of the child
▪ 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.30 Silberschatz, Galvin and Gagne ©2018
Android Process Importance Hierarchy
▪ Mobile operating systems often have to terminate processes to reclaim system
resources such as memory. From most to least important:
• Foreground process
The current process visible on the screen, application user is currently
interacting with
• Visible process
A process that is not directly visible on the foreground but that is performing an
activity that the foreground process is referring to (a process performing an
activity whose status is displayed on the foreground process)
• Service process
A process that is similar to a background process but is performing an activity
that is apparent to the user (such as streaming music)
• Background process
A process that may be performing an activity but is not apparent to the user
• Empty process
A process that holds no active components associated with any
application
Android will begin terminating processes that are least important.
Operating System Concepts – 10th Edition 3.31 Silberschatz, Galvin and Gagne ©2018