You are on page 1of 20

MEngg (CS)

Fall 2015 Semester

CS-531: Advanced Operating Systems

Lecture #2
Review of Relevant Topics (Process Concept)
Communication b/w processes

S. Zaffar Qasim
Assistant Professor (CIS)

Process Description
A fundamental question: What information does the OS need
to control processes and manage resources for them?
 Operating System Control Structures
 The OS constructs and maintains tables of information
about each entity that it is managing.
 Four different types of tables maintained by the OS:-
o memory,
o I/O,
o file and
o process.

CS-531: Advanced Operating Systems 1


OS Control Structures
1. Memory Tables
 Must include the following information:-
o The allocation of main memory to processes.
o The allocation of secondary memory to processes.
o Any protection attributes of blocks of main or virtual
memory, such as which processes may access certain
shared memory regions.
o Any information needed to manage virtual memory.
2. I/O tables
 Used by OS to manage the I/O devices and channels of
the computer system.
 At any given time, an I/O device may be either available
or assigned to a particular process.

OS Control Structures

 If an I/O operation is in progress,


o the OS needs to know the status of the operation and
o the location in main memory being used as the source or
destination of the I/O transfer.
3. File tables
– provide information about
o the existence of files,
o their location on secondary memory,
o their current status and other attributes.
o Much of this information may be maintained and
used by a file management system in which case the OS
has little or no knowledge of files.
4. Process tables are maintained by OS to manage processes.
4

CS-531: Advanced Operating Systems 2


OS Control Structures
 Two further points:
I. First, As Memory, I/O and files managed on behalf of
processes.
o These tables must be linked or cross-referenced in some
fashion.
o The files in file tables are accessible via an I/O device and will,
at some times, be in main/virtual memory.
II. Second, how does the OS know to create the tables in the
first place?
• An issue of configuration.
• That is, when the OS initialized,
o must have access to some configuration data defining the
basic environment, and
o these data must be created outside the OS
o with human assistance or by some configuration
software. 5

Process Control Structures


 In order to manage and control a process, the OS must
know
1. Process Location
2. Attributes of the process.
 Process Control Block (PCB): Collection of attributes about
a process.
 Process image: Collection of program (code or text), data,
stack and attributes.
1. Process Location
 The location of process image will depend on the memory
management scheme being used.
 In the simplest case, the process image is maintained as a
contiguous block of memory.

CS-531: Advanced Operating Systems 3


Process Control Structures
• Most modern OSs use a memory-management scheme in
which a process image consists of a set of blocks that need
not be stored contiguously.
• Depending on the scheme used,
– these blocks may be variable length (called segments) or
– fixed length (called pages) or a combination.
• In any case, such schemes allow the OS to bring only a
portion of any particular process in MM.
• The remainder is in secondary memory.
• Therefore, process tables must show the location of each
segment and/or page of each process image.

Process Control Structures


• Fig 1 depicts the generic structure of the location information.
• There is a primary process table with one entry for each process.
• Each entry contains, at least, a pointer to a process image.
• If the process image contains multiple blocks, this information is
contained directly in the primary process table or is available by cross-
reference to entries in memory tables.

Fig 1: Multiple processes and modes of execution 8

CS-531: Advanced Operating Systems 4


Process Control Block
 Three general categories of PCB information:-
1. Process Identification
2. Processor State Information
3. Process Control Information
1. Process Identification
• Numeric ids that may be stored with the PCB include
a. ID of this process
b. ID of the parent process
c. User identifier

PCB
• Process identifier is useful in several ways.
a. Many of the other tables controlled by the OS may use
process IDs to cross-reference process tables.
b. When processes communicate with one another, the
process ID informs the OS of the destination of a
particular communication.
c. When processes create other processes, identifiers
indicate the parent and descendants of each process.
2. Processor State Information
 It consists of the contents of processor registers.
 When a process is interrupted, all of register information
must be saved so that it can be restored when the process
resumes execution.
 The register set will include user-visible registers, control and
status registers and stack pointers. 10

CS-531: Advanced Operating Systems 5


PCB
3. Process Control Information
 Scheduling and State Information
– Process State: e.g. running, ready, waiting, halted.
– Priority: One or more fields may be used to describe the
scheduling priority of the process.
– Scheduling-related information: Depends on the
scheduling algo used.
o Examples are the amount of time that the process has
been waiting and
o the amount of time that the process executed the last
time it was running.
– Event: Identity of event the process is awaiting before it
can be resumed.

11

PCB
 Data Structuring
• A process may be linked to other process in a queue, ring or
some other structure.
o For example, all processes in a waiting state for a
particular priority level may be linked in a queue.
o A process may exhibit a parent-child relationship with
another process.
o The PCB may contain pointers to other processes to
support these structures.
 Interprocess Communication
• Various flags, signals and messages may be associated with
communication between two independent processes.

12

CS-531: Advanced Operating Systems 6


PCB
 Process Privileges
• Processes are granted privileges in terms of
o the memory that may be accessed,
o the types of instructions that may be executed and
o system utilities and services that may be used.
 Memory Management
• Pointers to segment and page tables that describe the virtual
memory assigned to this process.
 Resource Ownership and Utilization
• Resources controlled by the process may be indicated such
as opened files.
• A history of utilization of the processor or other resources
may also be included; this information may be needed by
the scheduler.
13

Structure of process Images in Virtual Memory


• Each process image consists of a PCB, a user stack, the
private address space of the process and any other address
space that the process shares with other processes.
• In fig 2, the process image appears as a contiguous range of
addresses.

Fig 2: User Processes in Virtual Memory 14

CS-531: Advanced Operating Systems 7


Structure of a process in Unix
• Every process has an entry in the kernel process table,
and
– each process is allocated a u area that contains private
data manipulated only by the kernel.
• The process table contains (or points to) a per process
region table, whose entries point to entries in a region
table.
• A region is a contiguous area of a process’s address
space, such as text, data and stack.

Fig 3 15

Structure of a process in Unix


• Region table entries describe the attributes of the region, such as
– whether it contains text or data,
– whether it is shared or private and
– where the data of the region is located in memory.
• The extra level of indirection (from the per process region table to the
region table) allows independent processes to share regions.
• When a process invokes the exec system call, the kernel allocates regions
for its text, data and stack after freeing the old regions the process had
been using.

16

CS-531: Advanced Operating Systems 8


Structure of a process in Unix
• When a process invokes fork, the kernel duplicates the
address space of the old process,
– allowing processes to share regions when possible and
making a physical copy otherwise.
• When a process invokes exit, the kernel frees the regions the
process had used.
• The process table entry and the u area contain control and
status information about the process.
• The u area is an extension of the process table entry.
• Fields in the process table discussed ahead are
 A state field,
 Identifiers indicating process owner (user IDs, or UIDs),
 An event descriptor set when a process is suspended (in
the sleep state)
17

Structure of a process in Unix


• The u area contains information needs to be accessible only
when the process is executing.
• The important fields are
o a pointer to the process table slot of the currently
executing process,
o parameters of the current system call, return values and
error codes
o file descriptors for all open files,
o internal I/O parameters,
o current directory and current root
o process and file size limits.

18

CS-531: Advanced Operating Systems 9


Structure of a process in Unix
• Internally, the kernel references the structure variable u to
access the u area of the currently running process,
o and when another process executes, the kernel
rearranges its virtual address space
o so that the structure u refers to the u area of the new
process.
• The implementation gives the kernel an easy way to identify
the current process by following the pointer from the u area
to its process table entry.

19

PROCESS CONTROL

• To ensure proper operation, we must protect the OS and


all other programs and their data from any
malfunctioning program or errant users.
• Protection is also needed for any shared resource.

1. Modes of Processor Execution


2. Context of a process
3. Mode Switching
4. Process Switching

20

CS-531: Advanced Operating Systems 10


Modes of Processor Execution
 Modern machines, at the very least, operate in two basic modes:-
1. Kernel mode and
2. User mode.
1. The kernel mode (also called supervisor or privileged mode)
has following features:-
i. Provides protection of key OS tables such as Process Control
Blocks, Memory tables etc. from interference by user programs.
ii. Some instructions can only be executed in the more-privileged
mode which includes
reading or altering program status word (control/status register)
primitive I/O instructions and
instructions that relate to memory management.
iii. Certain regions of memory can only be accessed in the kernel mode.
 If privileged instruction is attempted in user mode,
o the hardware treats it as illegal and traps to the OS.
21

Modes of Processor Execution

2. The less privileged mode is user mode and the user


programs typically would execute in this mode.
 A high level of control is not necessary.
 Even if the user makes no explicit requests for OS services,
o the OS still does bookkeeping operations that relate to
the user process,
o handling interrupts, scheduling processes,
o managing memory and so on.
 At system boot time, the hardware starts in kernel mode.
 The OS is then loaded, and starts user processes in user
mode.

22

CS-531: Advanced Operating Systems 11


Functions found in the OS Kernel
1. Process Management
 Process creation and termination
 Process scheduling and dispatching
 Process switching
 Process synchronization and support for interprocess
communication
 Management of process control blocks
2. Memory Management
 Allocation of address space for processes
 Swapping
 Page and segment management
3. I/O Management
 Buffer management
 Allocation of I/O channels and devices to processes
4. Support Functions
 Interrupt handling
 Accounting
23
 Monitoring

Changing Mode of Operation


 A bit, called mode bit, in the program status word (PSW)
indicates the mode of execution:
o usually kernel (0) or user (1).
 This bit is changed in response to certain events.
o when a user makes a call to an OS service or
o when an interrupt transfers control to a system routine.
 Typically this is done by executing an instruction that
changes the mode.
o e.g. Change Mode (CHM) instruction on the VAX.

24

CS-531: Advanced Operating Systems 12


The Trap Instruction
 If the CPU is running in user mode and a process wishes to
begin executing in supervisor mode,
o the environment must ensure that when the mode bit is
switched to supervisor mode,
o the processor will then begin executing kernel code.
 Otherwise, there would be no mechanism to prevent any
process from executing the privileged instructions.
 The supervisor mode is set by a trap instruction also called
a supervisor call instruction.
 Suppose the assembly language representation for a trap is
o trap argument

25

The Trap Instruction


 Fig 4 pictorially represents the indirect behavior of a trap
instruction (to accomplish the effect of the dotted line branch);
o it switches the CPU to supervisor mode by changing mode bit
o and then branches to an OS trap handler at a fixed location in
the system space.
 A trap produces the same action as a vectored interrupt.

Fig 4: The Trap Instruction


Operation

 Since only system code is loaded in the system space, only system code
can be invoked by a trap.
 When the OS has completed the supervisor call, it resets the mode bit to
user mode prior to the return.
26

CS-531: Advanced Operating Systems 13


Dual Mode in UNIX
 Although the system executes in one of two modes, the
kernel runs on behalf of a user process.
 The kernel is not a separate set of processes but it is part of
each user process.
 When we say that kernel is allocating resources, it is really
meant that a process executing in kernel mode allocates the
resources.
 For example, the shell reads user terminal input via a
system call:-
o The kernel executes on behalf of the shell process and returns
the typed character to the shell.
 The shell then executes in user mode,
o interprets the typed character stream and does the specified
actions possibly requiring invocation of other system calls.

27

Context of a process
 The context of a process defined by
o its text (i.e. code),
o the values of its global user variables and data structures,
o the values of machine registers it uses,
o the values stored in its process table slot and u area, and
o the contents of its user and kernel stacks.
 The text of OS and its global data structures shared by all
processes
o but do not constitute part of the context of a process.
 When executing a process, the system is said to be executing
in the context of the process.
 When the kernel decides that it should execute another
process, it does a context switch, so that the system executes
in the context of the other process.
28

CS-531: Advanced Operating Systems 14


Context of a process
• The kernel does a context switch when it changes context
from process A to process B.
• The kernel services interrupts in the context of the
interrupted process even though it may not have caused the
interrupt.
• The interrupted process may have been executing in user
mode or in kernel mode.
• The kernel saves enough information so that it can later
resume execution of the interrupted process.
• The kernel does not spawn or schedule a special process to
handle interrupts.

29

Mode Switching
 If an interrupt is pending, the processor does the following:-
o Saves the context of the program being executed.
o Sets the PC to the starting address of an interrupt-
handler program.
o It switched then to kernel mode so that the interrupt
processing code may include privileged instructions.
 The processor now fetches the first instruction of the
interrupt-handler program.
 The interrupt handler is typically a short program that
performs a few basic service tasks related to an interrupt.
 For example, it resets that flag or indicator that signals the
presence of an interrupt.
 It may send an acknowledgement to the entity that issued
the interrupt such as an I/O module.
30

CS-531: Advanced Operating Systems 15


Mode Switching

 And it may do some basic housekeeping related to the


effects of the event that caused the interrupt.
 For example, if the interrupt related to an I/O event and an
error has occurred,
o the interrupt handler may send a signal to the process
that originally requested the I/O operation.
 If the interrupt is by the clock, then the handler will hand
control over to the dispatcher
o which will want to pass control to another process
o because the time slice allotted to the currently running
process has expired.

31

Change of process state


 Mode switch may occur without changing the running state
of the process.
 However, if running process to be moved to another state,
then OS must make substantial changes in environment.
 Context-switch time pure overhead; system does no useful work.
 Its speed varies from machine to machine depending on
– the memory speed,
– the number of registers that must be copied and
– the existence of special instructions (such as a single
instruction to load or store all registers).
• Typical speeds range from 1 to 1000 microseconds.

32

CS-531: Advanced Operating Systems 16


Steps of process switch
1. Save the contents of PC, other registers and memory maps.
2. Update the PCB to change the state of the process to one of
the other states (Ready, Blocked, Ready/Suspend or Exit).
o Other relevant fields must also be updated including the
reason for leaving and accounting information.
3. Move the PCB of this process to appropriate queue (Ready,
Blocked), flushing and reloading the memory cache, etc.
4. Select another process for execution.
5. Update the PCB of the process selected (e.g. changing the
state to Running).
6. Update memory-management data structures.
7. Restore the context of the processor to that which existed at
the time the selected process was last switched out of the
Running state, loading memory maps.
33

Increasing efficiency of context switching

 Context-switch times are highly dependent on


hardware support.
 For instance, some processors (such as the Sun
UltraSPARC) provide multiple sets of registers.
 A context switch simply includes changing the
pointer to the current register set.
 Of course, if there are more active processes than
there are register sets, the system resorts to copying
register data to and from memory as before.

34

CS-531: Advanced Operating Systems 17


Communication between Processes: Signals
 Signals: facility for handling exceptional conditions similar
to software interrupts.
 A process Pi wishing to send a signal to another process Pj
invokes the library function signal with two parameters:
o id of the destination process, i.e., Pj and
o a signal number that indicates the kind of the signal to be
passed.
 This function uses the software interrupt instruction
<SI_instrn> <interrupt_code> to make a system call signal.
 The parameters (or their addresses) are put in the CPU
registers before making the call.
 Two interesting issues in implementation of signals:-
i. The sending process requires ID of the destination process.
ii.The kernel action if the receiving process in blocked state.
35

Communication between Processes: Signals

 The kernel would have to change the state of the process


temporarily to ready to execute its signal handling code.
 For each process a signal vector exists for every signal that
may be sent to it.
 It contains the address of a handler.
 A process can make the addresses of handlers known to the
kernel through the init_signal call.
 If no handler specified, the kernel would execute a default
handler for that signal.
 This scheme can be implemented by adding a field to the
PCB to hold the address of the signal vector area.

36

CS-531: Advanced Operating Systems 18


Signals: Example
 Process Pi makes a library call init_sig(sig1, sh1); sig1 is the ID of
signal to be initialized and sh1 is the address of the signal handler.
 The library routine init_sig makes the system call init_signal.
 While handling this call, the kernel accesses the PCB of Pi,
o obtains the start address of the signal vectors area, and
o enters the address sh1 in the signal vector of signal sig1 (see fig 5).
 Control now returns to Pi, which operates normally until sig1 is sent to it.

Fig 5: Arrangement used for


handling signals 37

Signals: Example
 Any process Pj sends sig1 to Pi using system call signal (Pi, sig1).
 The kernel locates the signal vector for sig1 through the PCB.
 It now arranges for process Pi to execute the signal handler starting
at address sh1 before resuming its execution.
o saving the state of Pi,
o changing the contents of the PC to address sh1, and
o Pi would now execute the signal handler.
 After that, the state of Pi reset to saved state to resume normal
operation.

38

CS-531: Advanced Operating Systems 19


Signals in Unix
 20 different signals, each corresponds to a distinct condition.
 A signal may be generated by
o a keyboard interrupt,
o an error in a process (such as bad memory reference), or
o a number of asynchronous events (such as timers or job-control
signals from the shell).
 Almost any signal may also be generated by the kill system call.
 SIGINT, interrupt signal, used to stop command before
completion.
o Usually provided by the ^C character (ASCII 3).
 The quit signal, SIGQUIT, usually produced by the ^bs character
(ASCII 28).
o It both stops the currently executing program and dumps its current
memory image to a file named core in the current directory.
 SIGILL produced by an illegal instruction.
 SIGSEGV produced by an attempt to address memory outside of
the legal virtual-memory space of a process. 39

Signals in Unix
 SIGFPE (floating-point exception) signal used if a process doing
floating-point arithmetic inadvertently divides by zero.
 Processes have several choices for handling signals
o ignore it,
o catch it, or
o let the signal kill the process (default for most signals).
 For catching signals, process must specify a signal handling procedure.
• There is one signal (the kill signal, number 9, SIGKILL) that cannot
be ignored or caught by a signal handler.
o used, e.g., to kill a runaway process ignoring other signals such
as SIGINT or SIGQUIT.
 A process can only send signals to members of its process group,
which consists of its parent (and further ancestors), siblings and
children (and further descendants).
 A process may also send a signal to all members of its process
group with a single system call.
40

CS-531: Advanced Operating Systems 20

You might also like