BITS Pilani
Tutorial session 8: Structure of a Process
in Unix Operating System
Process and process state
• A process is a program in execution.
• A process includes all relevant information
associated with the piece of program being
currently executed.
• A process includes the text section, program
counter, contents of processor’s registers,
process stack and a data section.
• As a process executes, its state changes.
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 2
Process states
• 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
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 3
Process control block
• A process in operating system is represented
by a process control block (PCB).
• A PCB contains information about process
state, process number, program counter,
registers, memory limits, list of open files etc.
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 4
Kernel data structures
• The process table entry - accessible to the
kernel.
• The u area – accessible only to the running
process
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 5
Process data table
• State field – identifies the process state
• Links to process’ u area in the main memory or in
secondary storage.
• Context switch information
• Process size
• Processor privileges in terms of user ids (UID)
• Process identifiers (PID) – set up when a process is
created
• Event descriptor
• Scheduling parameters
• Etc.
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 6
U area
• Pointer to the process table
• User ids
• Timer fields- to record time the process is in
user state or in kernel state
• An array indicating how the process wishes to
react to signals
• Return value
• I/O parameters
• etc.
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 7
Layout of system memory
• The process on UNIX system mainly consists of
three logical sections- text, data and stack.
• The text section contains
the set of instructions the machine executes for
the process
Addresses such as text addresses for branch
instructions, data addresses, and stack addresses
• The system V kernel divides the virtual
address space of a process into logical
regions.
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 8
Region
• A virtual address space is the set of ranges of
virtual addresses an operating system makes
available to a process.
• A region is a contiguous area of the virtual
address space of a process.
• A region can be treated as a distinct object that
can be shared or protected.
• The text, data and stack form separate regions of
a process.
• Several processes can also share a region.
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 9
Region table
• The kernel contains a region table and
allocates an entry from the table for each
active region in the system.
• Region table contains the information to
determine where its contents are located in
physical memory.
• Each process contains a process region table
called pregion.
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 10
Processes and regions
Per process region tables Regions
(Virtual address)
Text 8K b
Process Shared region
A Data 16K
Stack 32K
Virtual
c
addresses a
Text 4K
Process
Data
8K e
B
Stack
32K
Processes A and B
access the same
d
physical location
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 11
Shared regions
• The shared regions may have different virtual
addresses in each process.
• The pregion entry also contains a permission
field that indicates the type of access allowed
to the process- read only, read write or read
execute.
• Several processes can share parts of their
address space via a region.
• Each process accesses the region via a private
pregion entry.
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 12
Pages and page tables
• The physical memory is divided into a set of
equal sized blocks called pages.
• Typical page sizes may range from 512 bytes
to 4K bytes.
• Every addressable memory is contained in a
page and can be addressed by a (page
number, offset in page) pair.
• The purpose of paged memory is to allow
flexibility in assigning physical memory.
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 13
Example
• If a machine has 232 bytes of physical memory and a page size is 210, then, the
number of pages is 222.
• Assume word size = 1 byte
• Number of bits required for an address in the memory = 32
• Number of bits needed for unique page numbers = 22
• Then the number of addressable locations with in a page = 210
• Number of bits required for an offset with in the page = 10.
• Therefore, the pair (page number, offset in page) for each addressable location
has 22 bits (out of 32 bits) for page addressing and offset of 10 bits (out of 32
bits)
• Question: If the total memory is 20 GB, page size is 2KB, word size is 4B, then
compute the sizes of addresses for page numbers and offset in page.
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 14
Mapping virtual address to
physical memory
• Assume the size of page is 1 KB and the
process wants to access virtual memory
address 68432.
• Consider the following per process region
table as follows.
Text 8K The virtual address 68432
Data 32K is in the stack region
Stack 64K starting at virtual address
64K (=64x1024 = 65536)
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 15
Mapping virtual address to
physical memory
• Byte offset of 68432 = 68432 – 65536 = 2896
• Since page size = 1KB = 1024 B
• The byte offset 2896 is in page 2 (starting
numbering with 0) as 2896 < 3*1024 (=3072)
• Page number = 2
And byte offset of 68432 is = 2896-2048 = 848
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 16
Mapping virtual address to
physical memory
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 17
Layout of the kernel
• The code and data for the kernel reside in the system
permanently and all processes share it.
• When the system is booted, it loads the kernel code
into the memory.
• It also sets up the necessary tables and registers to
map its virtual addresses into physical memory
addresses.
• The kernel page tables are analogous to the page
tables associated with a process.
• While executing in kernel mode, the system permits
access to kernel addresses, but it prohibits such access
when executing in user mode.
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 18
Layout of the kernel
• The kernel is loaded into the memory such
that most kernel virtual addresses are
identical to their physical addresses.
• The mapping of these virtual addresses to
physical addresses is simply an identity
function.
• The u area requires virtual to physical address
mapping in the kernel.
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 19
The u area
• Every process has a private u area, yet the kernel
accesses it as if there were only one u area in the
system, that of the running process.
• A process can access its u area when it executes kernel
mode and not when it executes in user mode.
• The kernel accesses only one u area at a time by its
virtual address.
• The u area then partially defines the context of the
running process.
• When the kernel schedules a process for execution, it
finds the corresponding u area in physical memory and
makes it accessible by its virtual address.
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 20
The context of a process
• The context of a process consists of the
contents of its user address space and
contents of a hardware registers and kernel
data structures that relate to the process.
• The context of a process is the union of its
user-level context, register context, and
system level context
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 21
User level context
• The user level context consists of the process
text, data, user stack, and shared memory that
occupy the virtual address space of the
process
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 22
Register context
• It consists of the following components
Program counter – address of next instruction
Processor status (PS) register – specifies the
hardware status of the machine.as it relates to the
process.
Stack pointer – contains the current address of the
next entry in the kernel or user stack, determined
by the mode of execution
General purpose registers- contains the data
generated by the process
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 23
System level context
• It consists of the following
Process table entry – contains control information
that describes the state of a process
U area of the process – contains process control
information
Pregion entries, region tables and page tables – these
define the mapping from virtual to physical addresses.
Kernel stack- contains the stack frame of the kernel
procedures as a process executes in kernel mode
(process maintains a private copy of kernel stack being
used by the process)
System level context layer – contains the necessary
information to recover the previous layer
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 24
The context of a process
• The kernel pushes a context layer when an
interrupt occurs, when a process makes a
system call , or when a process does a context
switch.
• It pops the context layer when the kernel
returns from handling an interrupt, when a
process returns to user mode after the kernel
completes execution of a system call, or when
the process does the context switch
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 25
Components of the context of a
process
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 26
Saving the context of a process
• The kernel saves the context of a process
whenever it pushes a new system context
layer.
• The context is saved when system receives an
interrupt, when a process executes a system
call, or when the kernel does a context switch.
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 27
System calls for process control
• fork()- creates a new process
• exit()-terminates the process execution
• wait()- allows a parent process to synchronize its
execution with the exit of a child process.
• signal()- inform processes of the occurrence of
asynchronous events. The kernel synchronizes
execution of exit and wait via signals
• exec()- allows a process to invoke a new program
overlaying its address space with the executable
image of a file.
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 28
Process creation
• The fork() system call is invoked by a parent process to
create child processes.
• The syntax is
pid = fork()
• The kernel assigns a new ID to the child process.
• It makes a logical copy of the parent process.
• It allocates a slot in the process table for the new
process.
• The kernel increments file and inode table counters for
files associated with the process.
• It returns the ID number of the child to parent process
and a 0 value to a child process.
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 29
Seeing the running processes
• ps -el : This command lists the complete
information of all processes currently active
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 30
Understanding fork() system call
14
Copy of process A
Process A Shell
is created
Or a browser
Or a
program 14 0
p1.c
if (pid == 0) {
fork() Operating System printf(“this is the child process\n”);
exit (0);
}
else if (pid>0) {
printf(“this is the parent process\n”);
Hardware wait (0);
}
else printf(“ERROR\n”);
Assuming uni-processor model, only one The parent process waits to
process can be in execution at a time synchronize with the child process
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 31
Example: using fork()
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 32
Example: Understand the execution sequence of
two processes created with fork() system call
November 13 , 2020 OS Tutorial 8 (Sections 3 and 4) 33