Instructor
Mr. S. Christalin Nelson
Process Management
Overview (1/8)
• Program
– Passive entity
• E.g. Executable file stored on disk (Contains a list of instructions)
• Process
– Active entity
• A program in execution (Executable file loaded into memory)
• E.g. Word-processing program run by an user on PC, System Task
(Sending output to printer)
– Alternatively called as a Job or time-shared program
• Batch Systems => Jobs
• Time-shared Systems => User program or tasks
– Single user System runs processes concurrently (Word, Browser, etc)
• Embedded Device with no multitasking -> OS performs its
internal activities
4/1/2022 Instructor: Mr.S.Christalin Nelson 3 of 66
Overview (2/8)
• Unit of work in modern systems
– Early Computers
• Execute one process at a time
• Process has access to all system resources
– Contemporary Computer Systems
• Allow multi-programing - Execute processes concurrently (switch
CPU between processes)
• Processes share the system resources
• System consists of collection of processes
– OS processes (Executes system code)
– User processes (Executes user code)
4/1/2022 Instructor: Mr.S.Christalin Nelson 4 of 66
Overview (3/8)
• Processes need
– Resources (physical/logical) to accomplish a task
• Example: CPU time, memory, files, I/O devices
• Resource allocation
– During process creation (OR) Execution
– Initialization data (inputs)
• Example of a Process: “Display file status on screen”
– Gets file name (I/P) -> Executes appropriate instructions &
system calls -> Displays desired information -> Terminate -> OS
reclaims the reusable resources
4/1/2022 Instructor: Mr.S.Christalin Nelson 5 of 66
Overview (4/8)
• A process can be executed by one or many threads
– Thread
• A component of a process (light-weight process)
• Example:
– MS Word as a process has Spell checker within it as thread
– Multithreaded Server Architecture
4/1/2022 Instructor: Mr.S.Christalin Nelson 6 of 66
Overview (5/8)
• Program – Process – Thread
– efficient to use one process that contains multiple threads
since process creation is time consuming and resource
intensive
4/1/2022 Instructor: Mr.S.Christalin Nelson 7 of 66
Overview (6/8)
• Single-threaded process
– No. of Program Counters: 1
– Sequentially execute the instructions in the process
• Multi-threaded process
– It is efficient to use one process that contains multiple threads
as process creation is time consuming and resource intensive
– No. of Program Counters: 1 per thread
4/1/2022 Instructor: Mr.S.Christalin Nelson
What is use of a PC?
Store Address of
next instruction to
be executed
8 of 66
Overview (7/8)
• Single & Multicore Systems
– Single-core systems – Concurrent execution of threads
– Multi-core systems – Parallel execution of threads
– Example: Consider an application with 4 threads
– Note: Parallelism vs. Concurrency
• Concurrency – Allow all the tasks to make progress
• Parallelism – Perform more than one task simultaneously
4/1/2022 Instructor: Mr.S.Christalin Nelson 9 of 66
Overview (8/8)
• OS Activities related to Process management
– CPU Scheduling (processes & threads)
– Create and delete processes
– Suspend and resume processes
– Process synchronization mechanisms
– Process communication mechanisms
4/1/2022 Instructor: Mr.S.Christalin Nelson 10 of 66
Process Structure in Memory (1/5)
4/1/2022 Instructor: Mr.S.Christalin Nelson 11 of 66
Process Structure in Memory (2/5)
• Text or Code Segment
– Stores executable instructions of a program (machine
instructions that CPU executes).
– ‘Sharable’
• Only a single copy needs to be in memory for frequently executed
programs (E.g. Text editors, C compiler).
– Often ‘Read-only’/Executable to prevent a program from
accidentally modifying its instructions.
4/1/2022 Instructor: Mr.S.Christalin Nelson 12 of 66
Process Structure in Memory (3/5)
• Data Segment or Initialized DS
– Stores static & global variables that are initialized by
programmer.
– Can be classified into ‘Read Only’ & ‘Read-Write’.
• BSS Segment or Uninitialized DS
– Stores static & global variables that are initialized to zero by
the system before program starts execution.
– Called “bss” segment for “block started by symbol”.
4/1/2022 Instructor: Mr.S.Christalin Nelson 13 of 66
Process Structure in Memory (4/5)
• Heap Segment
– Used for dynamic allocation when program runs.
– Shared by all shared libraries and dynamically loaded modules
in a process.
• Stack Segment
– Stores temporary data. i.e. Local (auto) variables, Stack frame
• Note: Stack frame
– Contains set of values pushed for one function call (function
parameters or at least caller’s return address)
– During each recursive function call a new stack frame is used
– Grows downward to lower addresses.
– ‘Stack pointer’ register points to top of stack.
4/1/2022 Instructor: Mr.S.Christalin Nelson 14 of 66
Process Structure in Memory (5/5)
• Methods for loading exe file into memory
– Double Click icon/app, Enter the name of executable (./a.out)
• Two processes associated with one program is considered as
2 separate execution sequences
– Text section –> Equivalent
– Data, Heap & Stack section –> Different
• A process can spawn many processes as it runs
• Process can be an execution environment for other code
– Example: Java programming environment
• Java executable program is executed within JVM. JVM executes
as a process that interprets the loaded Java code & takes actions
(via native machine instructions) on behalf of that code
– In simulation code is written for a different instruction set
4/1/2022 Instructor: Mr.S.Christalin Nelson 15 of 66
Process States (1/3)
• State is defined partly by current activity of a process
– i.e. During execution the state of a process changes
– Only 1 process runs on any processor
• Many processes may be ready & waiting
4/1/2022 Instructor: Mr.S.Christalin Nelson 16 of 66
Process States (2/3)
• Different states
– New: Process is being created
– Ready: Process is waiting to be assigned to a processor
– Running: Instructions are being executed
– Waiting: Process is waiting for some event to occur
• E.g. For I/O completion or reception of a signal
– Terminated: Process has finished execution
• Note: Names can vary across OSs
4/1/2022 Instructor: Mr.S.Christalin Nelson 17 of 66
Process States (3/3)
• What comprises state of a process or task?
– If P2 is to be created & run -> state of P1 must be saved
• P1 can be later resumed with no side-effects
• Only one copy of registers exist -> Must be saved in memory
4/1/2022 Instructor: Mr.S.Christalin Nelson 18 of 66
Process control block (1/6)
• Repository of information about a
specific process
• PCB contains
– (1) Process state
– (2) Process ID
– (3) Program counter
– (4) CPU registers (Vary in type/number)
• E.g. Accumulators, Index registers, Stack
pointers, & other General-purpose
registers, Condition-code register
– (5) CPU-scheduling information
• Includes process priority, pointers to
scheduling queues, & scheduling
parameters (if any)
4/1/2022 Instructor: Mr.S.Christalin Nelson 19 of 66
Process control block (2/6)
• PCB has information associated with a specific process
– (6) Memory-management information
• Includes value of base & limit registers, value of page/segment
tables (depending on memory system used by OS)
– (7) Accounting information
• Includes amount of CPU & real time used, time limits, account
numbers, job or process numbers
– (8) I/O status information
• Includes list of I/O devices allocated to process, list of open files
4/1/2022 Instructor: Mr.S.Christalin Nelson 20 of 66
Process control block (3/6)
4/1/2022 Instructor: Mr.S.Christalin Nelson
• Context Switching (1/2)
– Process Context
• Machine environment during the time the process is actively
using the CPU
– i.e. Context includes PC, general purpose registers, etc.
– CPU switches between processes
• The OS must
– Save context of currently executing process (if any) &
– Restore context of that process being resumed
• Time taken depends on hardware support
21 of 66
Process control block (4/6)
4/1/2022 Instructor: Mr.S.Christalin Nelson
• Context Switching (2/2)
22 of 66
Process control block (5/6)
• Paging
4/1/2022 Instructor: Mr.S.Christalin Nelson 23 of 66
Process control block (6/6)
• Segmentation
4/1/2022 Instructor: Mr.S.Christalin Nelson 24 of 66
Process scheduler
• Objectives of MP & timesharing systems
– Multiprogramming systems
• Maximize CPU utilization
• Have some process running at all times
– Time sharing systems
• Switch CPU among processes
• Process Scheduler?
– Meets the objectives by selecting an available process
4/1/2022 Instructor: Mr.S.Christalin Nelson 26 of 66
Scheduling Queues (1/5)
• Job Queue
– All processes in the system awaiting admission
• Ready Queue
– Set of all processes residing in main memory, ready to execute
• New process, Parent process return back to ready Queue after a
wait for its child process to complete
– Structure of Ready Queue
4/1/2022 Instructor: Mr.S.Christalin Nelson 27 of 66
Scheduling Queues (2/5)
• Device Queue(s)
– Set of processes waiting for an I/O device (or for other
processes)
– Each device has its own device queue
4/1/2022 Instructor: Mr.S.Christalin Nelson 28 of 66
Scheduling Queues (3/5)
• Queuing Diagram
– A common representation of process scheduling
• Notation used
– Rectangular Boxes: Queues
– Circles: Resources that serve the queues
– Arrows: Flow of processes in the system
4/1/2022 Instructor: Mr.S.Christalin Nelson
Job Queue
29 of 66
Scheduling Queues (4/5)
• After allocation of CPU, a Process can
– Execute
– Create new child process & Wait for child’s termination
• Eventually enters Ready Queue
– Get interrupted & Process removed from CPU
• Put back in Ready Queue
– Wait for event occurrence (e.g. Completion of an I/O request)
• Issue an I/O request & placed in I/O Queue -> eventually enters
Ready Queue.
– Finally Quit
• Removed from all queues, PCB & resources are deallocated.
4/1/2022 Instructor: Mr.S.Christalin Nelson 30 of 66
Scheduling Queues (5/5)
• Process migration among various scheduling queues
throughout its lifetime
4/1/2022 Instructor: Mr.S.Christalin Nelson 31 of 66
Schedulers
• System software used by OS to select processes from Queue
• Long-term, Short-term & Medium-term schedulers
– Job scheduler (also known as Long-term scheduler)
• Selects processes from Job pool in secondary into Ready Queue
– Careful mixture of I/O bound & CPU bound processes to yield
optimum system throughput
• Controls Degree of Multiprogramming
– CPU scheduler (also known as Short-term scheduler)
• Selects processes to be executed next & allocates CPU to it
– Process with a long burst time can lead to starvation
• Executes at least once every 100 milliseconds
– Medium-term Scheduler
• Swapping out a process from main memory (Suspension)
– Swapping in or Resumption could occur later from the point it
stopped executing
4/1/2022 Instructor: Mr.S.Christalin Nelson 32 of 66
Operations of Processes (1/3)
• Process Creation
– fork()
• Parent & child process competes for CPU
with all other processes in system
• OS decides
– Which process to execute
– When to execute
– How long to execute
4/1/2022 Instructor: Mr.S.Christalin Nelson 34 of 66
Operations of Processes (2/3)
• Process Creation (contd.)
– Orphans
• Process whose parent process has terminated,
though it remains running itself
• It will be immediately adopted by special “init”
system process with PID 1
• wait()
– Blocks caller until one of its child process terminates
– Parent can obtain exit status of terminated child
» Success: returns PID of child
» Failure (no child): returns -1
– Zombies
4/1/2022 Instructor: Mr.S.Christalin Nelson 35 of 66
Operations of Processes (3/3)
• Process Creation (contd.)
– exec() family
• Process Termination
4/1/2022 Instructor: Mr.S.Christalin Nelson 36 of 66
Cooperating Processes (1/2)
• Types of Concurrent processes
– Independent Processes
• Cannot affect or be affected by other processes executing in
system
• Does not share data with any other process
– Cooperating Processes
• Can affect or be affected by other processes executing in system
• Shares data with any other process
• Requires IPC mechanisms for data & information exchange
– IPC Models
» Shared Memory Model, Message Passing Model
» Note: Many systems implement both
4/1/2022 Instructor: Mr.S.Christalin Nelson 38 of 66
Cooperating Processes (2/2)
• Advantages of Process Cooperation
– Information sharing
• Concurrent access
– Computation speedup
• Subtasks execute in parallel with others multicore systems
– Modularity
• Dividing system functions into separate processes/threads
– Convenience
• Working with many processes at the same time
4/1/2022 Instructor: Mr.S.Christalin Nelson 39 of 66
Shared-memory model
• Establish a region of memory that can be shared
– Processes can exchange information by
reading/writing data (not simultaneously)
– Processes determine form of data & location
• Not under OS’s control
• Faster than message passing model
– System calls are required only to establish shared
memory region
– Does not require kernel intervention
• Difficult to implement in a distributed system
• Suffers from cache coherency issues in multicore
systems
4/1/2022 Instructor: Mr.S.Christalin Nelson 40 of 66
Producer-Consumer Problem
• Producer & Consumer processes should run concurrently
– Producer process produces information & Consumer process
consumes this information
– Examples
4/1/2022 Instructor: Mr.S.Christalin Nelson
Compiler Assembler Loader
Assembly Code Object Modules
Web Server Client
HTML files & images
• Solution: Use shared memory
– Types of Buffers
• Unbounded-buffer: No practical limit on buffer size
• Bounded-buffer: Assume a fixed buffer size
– Producer & Consumer must be synchronized
• Producer produces an item & Consumer consumes another item
• Code to access/manipulate shared memory - written by
application programmer
Who
waits?
41 of 66
Message-passing model (1/2)
4/1/2022 Instructor: Mr.S.Christalin Nelson
• Processes can exchange messages (smaller amounts of data)
• Slower than Shared memory model
– Implemented using system calls
– Require kernel intervention (more time)
• Easier to implement in a distributed system
– Example: Internet Chat
• Better performance in multicore systems
• Min. 2 operations
– Send & Receive message
• Message Size
– Fixed (or) Variable
42 of 66
Message-passing model (2/2)
4/1/2022 Instructor: Mr.S.Christalin Nelson
• Requirements of two communicating processes
– Establish Communication link between them
– Exchange messages via send/receive
• Implementation of communication link
– Physical (e.g., shared memory, hardware bus, network)
– Logical (e.g., logical properties)
• Methods (with Logical implementation & communication)
– Direct or Indirect communication
– Synchronous or Asynchronous communication
– Automatic or Explicit buffering
43 of 66
Naming (1/4)
• Identify communicating processes (say P & Q)
• Direct Communication
– Hard coded: Identifiers should be explicitly stated
• Symmetric Scheme of Addressing
– P: Send (Q, message)
– Q: Receive (P, message)
• Asymmetric Scheme of Addressing
– P: Send (Q, message)
– Q: Receive (id, message) – Receive from any process
– Properties of communication link
• Links are established automatically
• One link associated with exactly one pair of processes
• Between each pair there exists exactly one link
• Link may be unidirectional (usually bi-directional)
4/1/2022 Instructor: Mr.S.Christalin Nelson 44 of 66
Naming (2/4)
• Indirect Communication
– Each mailbox or port has a unique ID (say A). Mailbox is shared.
• Send (A, message)
• Receive (A, message)
– 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
(each link corresponds to a mailbox)
• Link may be unidirectional or bi-directional
4/1/2022 Instructor: Mr.S.Christalin Nelson 45 of 66
Naming (3/4)
• Indirect Communication (Contd.)
– Who owns the mailbox?
• Process owns mailbox (part of process’s address space)
– Receives messages only through this mailbox
– On process termination, the mailbox disappears
• OS owns mailbox (Independent & not attached to any particular
process). Process operations include
– Create a new mailbox
» Ownership & receiving privilege may be passed to other
processes through appropriate system calls
– Send & Receive messages through mailbox
– Destroy a mailbox
4/1/2022 Instructor: Mr.S.Christalin Nelson 46 of 66
Naming (4/4)
• Indirect Communication (Contd.)
– Mailbox sharing: Who gets the message?
• Consider P, Q, and R share mailbox A. P sends. R & Q receive
• Solutions
– Associate a link with at most two processes
– Only one process can execute a receive operation at a time
– System arbitrarily selects receiver & notifies Sender
» May use Round Robin
4/1/2022 Instructor: Mr.S.Christalin Nelson 47 of 66
Synchronization
• Message passing can be either synchronous (blocking) or
Asynchronous (non-blocking)
– Blocking send
• Sender blocked until receiver process or mailbox gets message
– Non-blocking send
• Sender can send message & continue
– Blocking receive
• Receiver blocked until message is available
– Non-blocking receive
• Receiver receives a valid message or null
• Different combinations are possible
– Rendezvous between sender & receiver -> Blocking Send &
Receive
4/1/2022 Instructor: Mr.S.Christalin Nelson 48 of 66
Buffering
• Direct or indirect communication – exchanged messages
reside in a temporary queue
– Queue Implementation
• Zero capacity – 0 waiting messages
– Blocking Send
• Bounded capacity – finite length of n messages
– If link not full: Non-Blocking Send
» Place message (copy entire message or pointer to it)
– If link full: Blocking Send
• Unbounded capacity – infinite length
– Non-Blocking Send (Sender never waits)
4/1/2022 Instructor: Mr.S.Christalin Nelson 49 of 66
Additional communication strategies
• Low level method
– Sockets
• Allow only unstructured stream of bytes exchanged between
communicating threads – Client/Server application could impose
a structure on the data
• High-level methods
– Remote Procedure Calls (RPCs)
– Pipes
4/1/2022 Instructor: Mr.S.Christalin Nelson 51 of 66
Sockets (1/2)
4/1/2022 Instructor: Mr.S.Christalin Nelson
• Endpoint for communication between distributed processes
– Identified by concatenation of IP address & Port
• i.e. Computer & Software/Service (Port vs. Socket ?)
• Communication consists between a pair of sockets
– Use Client-Server Architecture
• Server socket waits by listening to specified port
– IP: 161.25.19.8, Port: 80 => 161.25.19.8:80
• Client initiates request for connection -> System allocates unique
port (>1024) for each client process
– IP: 146.86.5.20, Port: 1625 => 146.86.5.20:1625
• Client request from client socket
• Server receives in server socket
• Server accepts a connection
52 of 66
Sockets (2/2)
• Ports
– 0 to 1023: Well known ports
• Assigned by IANA (common protocols & services)
– 1024 to 49151: Registered ports
• Assigned by ICANN to a specific service
– 49152 to 65535: Ephemeral or dynamic (private, high) ports
• Ephemeral ports: Used for shorter duration by client applications
– Persistent ports: Used for longer duration by server applications
4/1/2022 Instructor: Mr.S.Christalin Nelson 53 of 66
RPC (1/6)
• Used
– Between systems with network connections
– Implementing a distributed file system
• Message-based communication scheme to provide remote
service
– (Vs. IPC messages) Well structured & not just data packets
– RPC daemon listens to a port on Remote system
– Client send message to RPC daemon in the correct port
• Contains identifier (specifying function to execute) & fn.
parameters
– Function is executed as requested & Output is sent back to
requester in separate message
4/1/2022 Instructor: Mr.S.Christalin Nelson 54 of 66
RPC (2/6)
• RPC Semantics allows client to invoke procedure on remote
host as it would invoke a procedure locally
– Separate client-side Stub for each remote procedure
• Locates remote port -> Parameter marshalling (packaging) ->
Send message to server
– Server-side Stub
• Receives message -> Invokes procedure -> Returns value (if any)
• Note: Stub code in Windows
– Compiled from a specification written in Microsoft Interface
Definition Language (MIDL)
4/1/2022 Instructor: Mr.S.Christalin Nelson 55 of 66
RPC (3/6)
• Issues
– (1) Differences in data representation on client & server (Little-
Endian, Big-Endian)
• Solution: RPC systems have machine-independent data rep.
– E.g. External data rep. (XDR) used during parameter marshalling
– (2) Semantics of a call: Due to common network errors RPCs
can fail, duplicated & executed more than once (vs. Local call)
• Solution: OS ensures messages are acted on “exactly once”,
rather than “at most once”
– At most once
» Attach Timestamp with message -> Ignore incoming messages
that have a timestamp already in history
– Exactly once
» “At most once” protocol + Send acknowledgement to client
» Client resends each RPC call periodically until it receives ACK
4/1/2022 Instructor: Mr.S.Christalin Nelson 56 of 66
RPC (4/6)
• Issues (contd.)
– (3) Binding of client & server port (None have full information)
• Standard calls: Binding replaces procedure call’s name by its
memory address during link, load, or execution time
• Solutions
– Predetermine binding information (Fixed port no. for a service)
» (Compile time) Associate fixed server port no. with RPC ->
(After compilation) Server cannot change port no.
– Dynamic binding by rendezvous mechanism
» OS provides a rendezvous (called matchmaker) daemon on
fixed RPC port -> Client sends message to rendezvous daemon
contains RPC name & requests RPC port no. -> Port no. given ->
RPC sent to port no. until process terminates (or server crash)
» Requires extra overhead of initial request, More flexible
4/1/2022 Instructor: Mr.S.Christalin Nelson 57 of 66
4/1/2022 Instructor: Mr.S.Christalin Nelson 58 of 74
RPC (6/6)
• Implementing a distributed file system (DFS)
– Set of RPC daemons & clients
– Message sent to DFS port on a server with reqd. disk operation
(read, write, rename, delete, or status) -> Return message
contains data resulting from that call -> DFS daemon executes
on behalf of client
• Message might request whole file transfer or a simple block
– Whole file: several requests may be needed
4/1/2022 Instructor: Mr.S.Christalin Nelson 59 of 66
Pipes (1/6)
• Conduit allowing two processes to communicate
• Issues
– Allows bidirectional or unidirectional communication?
• If 2-way communication is allowed, is it half duplex or full duplex
– Must a relationship (such as parent–child) exist between
communicating processes?
– Can pipes communicate over network or must communicating
processes reside on same machine?
• Types
– Ordinary Pipes
– Named Pipes
4/1/2022 Instructor: Mr.S.Christalin Nelson 60 of 66
Pipe (2/6)
• Ordinary Pipe
– Unidirectional
• Producer (write-end) & Consumer (read-end)
• Bidirectional: Requires 2 pipes
– Requires parent–child relationship between communicating
processes (UNIX & Windows)
• Two processes communicate in producer-consumer fashion
• Cannot be accessed by other process
• Initially, parent & child process close their unused ends of pipe
– close()
– Used for communication between processes on same machine
4/1/2022 Instructor: Mr.S.Christalin Nelson 61 of 66
Pipe (3/6)
• Ordinary Pipe (contd.)
– Unix
• Creation: pipe(int fd[])
• Access: read(), write() system calls
– Windows systems (referred as "Anonymous pipes”)
• Creation: CreatePipe()
• Access: ReadFile(), WriteFile()
• Programmer specifies the attributes to be inherited by child
4/1/2022 Instructor: Mr.S.Christalin Nelson
Read
End
Write
End
62 of 66
Pipe (4/6)
• Named Pipes
– Communication can be bidirectional
– Parent–child relationship is not required
• Several processes can use a pipe (can have several writers)
– Pipe continue to exist after communicating processes have
finished
– Supported by UNIX & Windows systems
4/1/2022 Instructor: Mr.S.Christalin Nelson 63 of 66
Pipe (5/6)
• Named Pipes (contd.)
– UNIX (referred as FIFOs)
• Appear as typical files in the file system
• FIFOs allow bidirectional communication (only half-duplex is
permitted)
– 2 FIFOs required
• Creation: mkfifo() system call
• Manipulation: open(), read(), write(), close() system calls
• Continue to exist until it is explicitly deleted from file system
• Communicating processes must reside on same machine
– Inter-machine communication requires use of sockets
• Transmits only byte-oriented data
4/1/2022 Instructor: Mr.S.Christalin Nelson 64 of 66
Pipe (6/6)
• Named Pipes (contd.)
– Windows
• Full-duplex communication is allowed
• Creation: CreateNamedPipe()
• Communication: ReadFile(), WriteFile()
– Client connects to a named pipe using ConnectNamedPipe()
• Communicating processes can reside on same or different
machines
• Transmits either byte or message oriented data
4/1/2022 Instructor: Mr.S.Christalin Nelson 65 of 66
Process Management

Process Management

  • 1.
    Instructor Mr. S. ChristalinNelson Process Management
  • 3.
    Overview (1/8) • Program –Passive entity • E.g. Executable file stored on disk (Contains a list of instructions) • Process – Active entity • A program in execution (Executable file loaded into memory) • E.g. Word-processing program run by an user on PC, System Task (Sending output to printer) – Alternatively called as a Job or time-shared program • Batch Systems => Jobs • Time-shared Systems => User program or tasks – Single user System runs processes concurrently (Word, Browser, etc) • Embedded Device with no multitasking -> OS performs its internal activities 4/1/2022 Instructor: Mr.S.Christalin Nelson 3 of 66
  • 4.
    Overview (2/8) • Unitof work in modern systems – Early Computers • Execute one process at a time • Process has access to all system resources – Contemporary Computer Systems • Allow multi-programing - Execute processes concurrently (switch CPU between processes) • Processes share the system resources • System consists of collection of processes – OS processes (Executes system code) – User processes (Executes user code) 4/1/2022 Instructor: Mr.S.Christalin Nelson 4 of 66
  • 5.
    Overview (3/8) • Processesneed – Resources (physical/logical) to accomplish a task • Example: CPU time, memory, files, I/O devices • Resource allocation – During process creation (OR) Execution – Initialization data (inputs) • Example of a Process: “Display file status on screen” – Gets file name (I/P) -> Executes appropriate instructions & system calls -> Displays desired information -> Terminate -> OS reclaims the reusable resources 4/1/2022 Instructor: Mr.S.Christalin Nelson 5 of 66
  • 6.
    Overview (4/8) • Aprocess can be executed by one or many threads – Thread • A component of a process (light-weight process) • Example: – MS Word as a process has Spell checker within it as thread – Multithreaded Server Architecture 4/1/2022 Instructor: Mr.S.Christalin Nelson 6 of 66
  • 7.
    Overview (5/8) • Program– Process – Thread – efficient to use one process that contains multiple threads since process creation is time consuming and resource intensive 4/1/2022 Instructor: Mr.S.Christalin Nelson 7 of 66
  • 8.
    Overview (6/8) • Single-threadedprocess – No. of Program Counters: 1 – Sequentially execute the instructions in the process • Multi-threaded process – It is efficient to use one process that contains multiple threads as process creation is time consuming and resource intensive – No. of Program Counters: 1 per thread 4/1/2022 Instructor: Mr.S.Christalin Nelson What is use of a PC? Store Address of next instruction to be executed 8 of 66
  • 9.
    Overview (7/8) • Single& Multicore Systems – Single-core systems – Concurrent execution of threads – Multi-core systems – Parallel execution of threads – Example: Consider an application with 4 threads – Note: Parallelism vs. Concurrency • Concurrency – Allow all the tasks to make progress • Parallelism – Perform more than one task simultaneously 4/1/2022 Instructor: Mr.S.Christalin Nelson 9 of 66
  • 10.
    Overview (8/8) • OSActivities related to Process management – CPU Scheduling (processes & threads) – Create and delete processes – Suspend and resume processes – Process synchronization mechanisms – Process communication mechanisms 4/1/2022 Instructor: Mr.S.Christalin Nelson 10 of 66
  • 11.
    Process Structure inMemory (1/5) 4/1/2022 Instructor: Mr.S.Christalin Nelson 11 of 66
  • 12.
    Process Structure inMemory (2/5) • Text or Code Segment – Stores executable instructions of a program (machine instructions that CPU executes). – ‘Sharable’ • Only a single copy needs to be in memory for frequently executed programs (E.g. Text editors, C compiler). – Often ‘Read-only’/Executable to prevent a program from accidentally modifying its instructions. 4/1/2022 Instructor: Mr.S.Christalin Nelson 12 of 66
  • 13.
    Process Structure inMemory (3/5) • Data Segment or Initialized DS – Stores static & global variables that are initialized by programmer. – Can be classified into ‘Read Only’ & ‘Read-Write’. • BSS Segment or Uninitialized DS – Stores static & global variables that are initialized to zero by the system before program starts execution. – Called “bss” segment for “block started by symbol”. 4/1/2022 Instructor: Mr.S.Christalin Nelson 13 of 66
  • 14.
    Process Structure inMemory (4/5) • Heap Segment – Used for dynamic allocation when program runs. – Shared by all shared libraries and dynamically loaded modules in a process. • Stack Segment – Stores temporary data. i.e. Local (auto) variables, Stack frame • Note: Stack frame – Contains set of values pushed for one function call (function parameters or at least caller’s return address) – During each recursive function call a new stack frame is used – Grows downward to lower addresses. – ‘Stack pointer’ register points to top of stack. 4/1/2022 Instructor: Mr.S.Christalin Nelson 14 of 66
  • 15.
    Process Structure inMemory (5/5) • Methods for loading exe file into memory – Double Click icon/app, Enter the name of executable (./a.out) • Two processes associated with one program is considered as 2 separate execution sequences – Text section –> Equivalent – Data, Heap & Stack section –> Different • A process can spawn many processes as it runs • Process can be an execution environment for other code – Example: Java programming environment • Java executable program is executed within JVM. JVM executes as a process that interprets the loaded Java code & takes actions (via native machine instructions) on behalf of that code – In simulation code is written for a different instruction set 4/1/2022 Instructor: Mr.S.Christalin Nelson 15 of 66
  • 16.
    Process States (1/3) •State is defined partly by current activity of a process – i.e. During execution the state of a process changes – Only 1 process runs on any processor • Many processes may be ready & waiting 4/1/2022 Instructor: Mr.S.Christalin Nelson 16 of 66
  • 17.
    Process States (2/3) •Different states – New: Process is being created – Ready: Process is waiting to be assigned to a processor – Running: Instructions are being executed – Waiting: Process is waiting for some event to occur • E.g. For I/O completion or reception of a signal – Terminated: Process has finished execution • Note: Names can vary across OSs 4/1/2022 Instructor: Mr.S.Christalin Nelson 17 of 66
  • 18.
    Process States (3/3) •What comprises state of a process or task? – If P2 is to be created & run -> state of P1 must be saved • P1 can be later resumed with no side-effects • Only one copy of registers exist -> Must be saved in memory 4/1/2022 Instructor: Mr.S.Christalin Nelson 18 of 66
  • 19.
    Process control block(1/6) • Repository of information about a specific process • PCB contains – (1) Process state – (2) Process ID – (3) Program counter – (4) CPU registers (Vary in type/number) • E.g. Accumulators, Index registers, Stack pointers, & other General-purpose registers, Condition-code register – (5) CPU-scheduling information • Includes process priority, pointers to scheduling queues, & scheduling parameters (if any) 4/1/2022 Instructor: Mr.S.Christalin Nelson 19 of 66
  • 20.
    Process control block(2/6) • PCB has information associated with a specific process – (6) Memory-management information • Includes value of base & limit registers, value of page/segment tables (depending on memory system used by OS) – (7) Accounting information • Includes amount of CPU & real time used, time limits, account numbers, job or process numbers – (8) I/O status information • Includes list of I/O devices allocated to process, list of open files 4/1/2022 Instructor: Mr.S.Christalin Nelson 20 of 66
  • 21.
    Process control block(3/6) 4/1/2022 Instructor: Mr.S.Christalin Nelson • Context Switching (1/2) – Process Context • Machine environment during the time the process is actively using the CPU – i.e. Context includes PC, general purpose registers, etc. – CPU switches between processes • The OS must – Save context of currently executing process (if any) & – Restore context of that process being resumed • Time taken depends on hardware support 21 of 66
  • 22.
    Process control block(4/6) 4/1/2022 Instructor: Mr.S.Christalin Nelson • Context Switching (2/2) 22 of 66
  • 23.
    Process control block(5/6) • Paging 4/1/2022 Instructor: Mr.S.Christalin Nelson 23 of 66
  • 24.
    Process control block(6/6) • Segmentation 4/1/2022 Instructor: Mr.S.Christalin Nelson 24 of 66
  • 26.
    Process scheduler • Objectivesof MP & timesharing systems – Multiprogramming systems • Maximize CPU utilization • Have some process running at all times – Time sharing systems • Switch CPU among processes • Process Scheduler? – Meets the objectives by selecting an available process 4/1/2022 Instructor: Mr.S.Christalin Nelson 26 of 66
  • 27.
    Scheduling Queues (1/5) •Job Queue – All processes in the system awaiting admission • Ready Queue – Set of all processes residing in main memory, ready to execute • New process, Parent process return back to ready Queue after a wait for its child process to complete – Structure of Ready Queue 4/1/2022 Instructor: Mr.S.Christalin Nelson 27 of 66
  • 28.
    Scheduling Queues (2/5) •Device Queue(s) – Set of processes waiting for an I/O device (or for other processes) – Each device has its own device queue 4/1/2022 Instructor: Mr.S.Christalin Nelson 28 of 66
  • 29.
    Scheduling Queues (3/5) •Queuing Diagram – A common representation of process scheduling • Notation used – Rectangular Boxes: Queues – Circles: Resources that serve the queues – Arrows: Flow of processes in the system 4/1/2022 Instructor: Mr.S.Christalin Nelson Job Queue 29 of 66
  • 30.
    Scheduling Queues (4/5) •After allocation of CPU, a Process can – Execute – Create new child process & Wait for child’s termination • Eventually enters Ready Queue – Get interrupted & Process removed from CPU • Put back in Ready Queue – Wait for event occurrence (e.g. Completion of an I/O request) • Issue an I/O request & placed in I/O Queue -> eventually enters Ready Queue. – Finally Quit • Removed from all queues, PCB & resources are deallocated. 4/1/2022 Instructor: Mr.S.Christalin Nelson 30 of 66
  • 31.
    Scheduling Queues (5/5) •Process migration among various scheduling queues throughout its lifetime 4/1/2022 Instructor: Mr.S.Christalin Nelson 31 of 66
  • 32.
    Schedulers • System softwareused by OS to select processes from Queue • Long-term, Short-term & Medium-term schedulers – Job scheduler (also known as Long-term scheduler) • Selects processes from Job pool in secondary into Ready Queue – Careful mixture of I/O bound & CPU bound processes to yield optimum system throughput • Controls Degree of Multiprogramming – CPU scheduler (also known as Short-term scheduler) • Selects processes to be executed next & allocates CPU to it – Process with a long burst time can lead to starvation • Executes at least once every 100 milliseconds – Medium-term Scheduler • Swapping out a process from main memory (Suspension) – Swapping in or Resumption could occur later from the point it stopped executing 4/1/2022 Instructor: Mr.S.Christalin Nelson 32 of 66
  • 34.
    Operations of Processes(1/3) • Process Creation – fork() • Parent & child process competes for CPU with all other processes in system • OS decides – Which process to execute – When to execute – How long to execute 4/1/2022 Instructor: Mr.S.Christalin Nelson 34 of 66
  • 35.
    Operations of Processes(2/3) • Process Creation (contd.) – Orphans • Process whose parent process has terminated, though it remains running itself • It will be immediately adopted by special “init” system process with PID 1 • wait() – Blocks caller until one of its child process terminates – Parent can obtain exit status of terminated child » Success: returns PID of child » Failure (no child): returns -1 – Zombies 4/1/2022 Instructor: Mr.S.Christalin Nelson 35 of 66
  • 36.
    Operations of Processes(3/3) • Process Creation (contd.) – exec() family • Process Termination 4/1/2022 Instructor: Mr.S.Christalin Nelson 36 of 66
  • 38.
    Cooperating Processes (1/2) •Types of Concurrent processes – Independent Processes • Cannot affect or be affected by other processes executing in system • Does not share data with any other process – Cooperating Processes • Can affect or be affected by other processes executing in system • Shares data with any other process • Requires IPC mechanisms for data & information exchange – IPC Models » Shared Memory Model, Message Passing Model » Note: Many systems implement both 4/1/2022 Instructor: Mr.S.Christalin Nelson 38 of 66
  • 39.
    Cooperating Processes (2/2) •Advantages of Process Cooperation – Information sharing • Concurrent access – Computation speedup • Subtasks execute in parallel with others multicore systems – Modularity • Dividing system functions into separate processes/threads – Convenience • Working with many processes at the same time 4/1/2022 Instructor: Mr.S.Christalin Nelson 39 of 66
  • 40.
    Shared-memory model • Establisha region of memory that can be shared – Processes can exchange information by reading/writing data (not simultaneously) – Processes determine form of data & location • Not under OS’s control • Faster than message passing model – System calls are required only to establish shared memory region – Does not require kernel intervention • Difficult to implement in a distributed system • Suffers from cache coherency issues in multicore systems 4/1/2022 Instructor: Mr.S.Christalin Nelson 40 of 66
  • 41.
    Producer-Consumer Problem • Producer& Consumer processes should run concurrently – Producer process produces information & Consumer process consumes this information – Examples 4/1/2022 Instructor: Mr.S.Christalin Nelson Compiler Assembler Loader Assembly Code Object Modules Web Server Client HTML files & images • Solution: Use shared memory – Types of Buffers • Unbounded-buffer: No practical limit on buffer size • Bounded-buffer: Assume a fixed buffer size – Producer & Consumer must be synchronized • Producer produces an item & Consumer consumes another item • Code to access/manipulate shared memory - written by application programmer Who waits? 41 of 66
  • 42.
    Message-passing model (1/2) 4/1/2022Instructor: Mr.S.Christalin Nelson • Processes can exchange messages (smaller amounts of data) • Slower than Shared memory model – Implemented using system calls – Require kernel intervention (more time) • Easier to implement in a distributed system – Example: Internet Chat • Better performance in multicore systems • Min. 2 operations – Send & Receive message • Message Size – Fixed (or) Variable 42 of 66
  • 43.
    Message-passing model (2/2) 4/1/2022Instructor: Mr.S.Christalin Nelson • Requirements of two communicating processes – Establish Communication link between them – Exchange messages via send/receive • Implementation of communication link – Physical (e.g., shared memory, hardware bus, network) – Logical (e.g., logical properties) • Methods (with Logical implementation & communication) – Direct or Indirect communication – Synchronous or Asynchronous communication – Automatic or Explicit buffering 43 of 66
  • 44.
    Naming (1/4) • Identifycommunicating processes (say P & Q) • Direct Communication – Hard coded: Identifiers should be explicitly stated • Symmetric Scheme of Addressing – P: Send (Q, message) – Q: Receive (P, message) • Asymmetric Scheme of Addressing – P: Send (Q, message) – Q: Receive (id, message) – Receive from any process – Properties of communication link • Links are established automatically • One link associated with exactly one pair of processes • Between each pair there exists exactly one link • Link may be unidirectional (usually bi-directional) 4/1/2022 Instructor: Mr.S.Christalin Nelson 44 of 66
  • 45.
    Naming (2/4) • IndirectCommunication – Each mailbox or port has a unique ID (say A). Mailbox is shared. • Send (A, message) • Receive (A, message) – 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 (each link corresponds to a mailbox) • Link may be unidirectional or bi-directional 4/1/2022 Instructor: Mr.S.Christalin Nelson 45 of 66
  • 46.
    Naming (3/4) • IndirectCommunication (Contd.) – Who owns the mailbox? • Process owns mailbox (part of process’s address space) – Receives messages only through this mailbox – On process termination, the mailbox disappears • OS owns mailbox (Independent & not attached to any particular process). Process operations include – Create a new mailbox » Ownership & receiving privilege may be passed to other processes through appropriate system calls – Send & Receive messages through mailbox – Destroy a mailbox 4/1/2022 Instructor: Mr.S.Christalin Nelson 46 of 66
  • 47.
    Naming (4/4) • IndirectCommunication (Contd.) – Mailbox sharing: Who gets the message? • Consider P, Q, and R share mailbox A. P sends. R & Q receive • Solutions – Associate a link with at most two processes – Only one process can execute a receive operation at a time – System arbitrarily selects receiver & notifies Sender » May use Round Robin 4/1/2022 Instructor: Mr.S.Christalin Nelson 47 of 66
  • 48.
    Synchronization • Message passingcan be either synchronous (blocking) or Asynchronous (non-blocking) – Blocking send • Sender blocked until receiver process or mailbox gets message – Non-blocking send • Sender can send message & continue – Blocking receive • Receiver blocked until message is available – Non-blocking receive • Receiver receives a valid message or null • Different combinations are possible – Rendezvous between sender & receiver -> Blocking Send & Receive 4/1/2022 Instructor: Mr.S.Christalin Nelson 48 of 66
  • 49.
    Buffering • Direct orindirect communication – exchanged messages reside in a temporary queue – Queue Implementation • Zero capacity – 0 waiting messages – Blocking Send • Bounded capacity – finite length of n messages – If link not full: Non-Blocking Send » Place message (copy entire message or pointer to it) – If link full: Blocking Send • Unbounded capacity – infinite length – Non-Blocking Send (Sender never waits) 4/1/2022 Instructor: Mr.S.Christalin Nelson 49 of 66
  • 51.
    Additional communication strategies •Low level method – Sockets • Allow only unstructured stream of bytes exchanged between communicating threads – Client/Server application could impose a structure on the data • High-level methods – Remote Procedure Calls (RPCs) – Pipes 4/1/2022 Instructor: Mr.S.Christalin Nelson 51 of 66
  • 52.
    Sockets (1/2) 4/1/2022 Instructor:Mr.S.Christalin Nelson • Endpoint for communication between distributed processes – Identified by concatenation of IP address & Port • i.e. Computer & Software/Service (Port vs. Socket ?) • Communication consists between a pair of sockets – Use Client-Server Architecture • Server socket waits by listening to specified port – IP: 161.25.19.8, Port: 80 => 161.25.19.8:80 • Client initiates request for connection -> System allocates unique port (>1024) for each client process – IP: 146.86.5.20, Port: 1625 => 146.86.5.20:1625 • Client request from client socket • Server receives in server socket • Server accepts a connection 52 of 66
  • 53.
    Sockets (2/2) • Ports –0 to 1023: Well known ports • Assigned by IANA (common protocols & services) – 1024 to 49151: Registered ports • Assigned by ICANN to a specific service – 49152 to 65535: Ephemeral or dynamic (private, high) ports • Ephemeral ports: Used for shorter duration by client applications – Persistent ports: Used for longer duration by server applications 4/1/2022 Instructor: Mr.S.Christalin Nelson 53 of 66
  • 54.
    RPC (1/6) • Used –Between systems with network connections – Implementing a distributed file system • Message-based communication scheme to provide remote service – (Vs. IPC messages) Well structured & not just data packets – RPC daemon listens to a port on Remote system – Client send message to RPC daemon in the correct port • Contains identifier (specifying function to execute) & fn. parameters – Function is executed as requested & Output is sent back to requester in separate message 4/1/2022 Instructor: Mr.S.Christalin Nelson 54 of 66
  • 55.
    RPC (2/6) • RPCSemantics allows client to invoke procedure on remote host as it would invoke a procedure locally – Separate client-side Stub for each remote procedure • Locates remote port -> Parameter marshalling (packaging) -> Send message to server – Server-side Stub • Receives message -> Invokes procedure -> Returns value (if any) • Note: Stub code in Windows – Compiled from a specification written in Microsoft Interface Definition Language (MIDL) 4/1/2022 Instructor: Mr.S.Christalin Nelson 55 of 66
  • 56.
    RPC (3/6) • Issues –(1) Differences in data representation on client & server (Little- Endian, Big-Endian) • Solution: RPC systems have machine-independent data rep. – E.g. External data rep. (XDR) used during parameter marshalling – (2) Semantics of a call: Due to common network errors RPCs can fail, duplicated & executed more than once (vs. Local call) • Solution: OS ensures messages are acted on “exactly once”, rather than “at most once” – At most once » Attach Timestamp with message -> Ignore incoming messages that have a timestamp already in history – Exactly once » “At most once” protocol + Send acknowledgement to client » Client resends each RPC call periodically until it receives ACK 4/1/2022 Instructor: Mr.S.Christalin Nelson 56 of 66
  • 57.
    RPC (4/6) • Issues(contd.) – (3) Binding of client & server port (None have full information) • Standard calls: Binding replaces procedure call’s name by its memory address during link, load, or execution time • Solutions – Predetermine binding information (Fixed port no. for a service) » (Compile time) Associate fixed server port no. with RPC -> (After compilation) Server cannot change port no. – Dynamic binding by rendezvous mechanism » OS provides a rendezvous (called matchmaker) daemon on fixed RPC port -> Client sends message to rendezvous daemon contains RPC name & requests RPC port no. -> Port no. given -> RPC sent to port no. until process terminates (or server crash) » Requires extra overhead of initial request, More flexible 4/1/2022 Instructor: Mr.S.Christalin Nelson 57 of 66
  • 58.
  • 59.
    RPC (6/6) • Implementinga distributed file system (DFS) – Set of RPC daemons & clients – Message sent to DFS port on a server with reqd. disk operation (read, write, rename, delete, or status) -> Return message contains data resulting from that call -> DFS daemon executes on behalf of client • Message might request whole file transfer or a simple block – Whole file: several requests may be needed 4/1/2022 Instructor: Mr.S.Christalin Nelson 59 of 66
  • 60.
    Pipes (1/6) • Conduitallowing two processes to communicate • Issues – Allows bidirectional or unidirectional communication? • If 2-way communication is allowed, is it half duplex or full duplex – Must a relationship (such as parent–child) exist between communicating processes? – Can pipes communicate over network or must communicating processes reside on same machine? • Types – Ordinary Pipes – Named Pipes 4/1/2022 Instructor: Mr.S.Christalin Nelson 60 of 66
  • 61.
    Pipe (2/6) • OrdinaryPipe – Unidirectional • Producer (write-end) & Consumer (read-end) • Bidirectional: Requires 2 pipes – Requires parent–child relationship between communicating processes (UNIX & Windows) • Two processes communicate in producer-consumer fashion • Cannot be accessed by other process • Initially, parent & child process close their unused ends of pipe – close() – Used for communication between processes on same machine 4/1/2022 Instructor: Mr.S.Christalin Nelson 61 of 66
  • 62.
    Pipe (3/6) • OrdinaryPipe (contd.) – Unix • Creation: pipe(int fd[]) • Access: read(), write() system calls – Windows systems (referred as "Anonymous pipes”) • Creation: CreatePipe() • Access: ReadFile(), WriteFile() • Programmer specifies the attributes to be inherited by child 4/1/2022 Instructor: Mr.S.Christalin Nelson Read End Write End 62 of 66
  • 63.
    Pipe (4/6) • NamedPipes – Communication can be bidirectional – Parent–child relationship is not required • Several processes can use a pipe (can have several writers) – Pipe continue to exist after communicating processes have finished – Supported by UNIX & Windows systems 4/1/2022 Instructor: Mr.S.Christalin Nelson 63 of 66
  • 64.
    Pipe (5/6) • NamedPipes (contd.) – UNIX (referred as FIFOs) • Appear as typical files in the file system • FIFOs allow bidirectional communication (only half-duplex is permitted) – 2 FIFOs required • Creation: mkfifo() system call • Manipulation: open(), read(), write(), close() system calls • Continue to exist until it is explicitly deleted from file system • Communicating processes must reside on same machine – Inter-machine communication requires use of sockets • Transmits only byte-oriented data 4/1/2022 Instructor: Mr.S.Christalin Nelson 64 of 66
  • 65.
    Pipe (6/6) • NamedPipes (contd.) – Windows • Full-duplex communication is allowed • Creation: CreateNamedPipe() • Communication: ReadFile(), WriteFile() – Client connects to a named pipe using ConnectNamedPipe() • Communicating processes can reside on same or different machines • Transmits either byte or message oriented data 4/1/2022 Instructor: Mr.S.Christalin Nelson 65 of 66