OS-03 Process

Overview

Process is a program in execution.

Basics

Concept

  • sequential not parallel

Process vs Program

  • Program becomes process when an executable file is loaded into memory

AspectProgramProcess
DefinitionA static set of instructions stored in secondary storage (e.g. a hard disk); a passive entityA dynamic instance of a program in execution; an actively running entity
AttributesOnly a static description of instructions and dataIncludes runtime state such as the program counter, memory state, and register values
LifetimeLong-lived (unless deleted)Transient (from creation to termination)
Resource needsOccupies no system resources (only disk space for storage)Needs CPU time, memory, I/O devices, and other resources
Control structureNo control blockHas a process control block (PCB) recording its running state and resource allocation

Memory

  • Stack: local variables, function parameters, return address
  • Heap: dynamic memory allocation
  • Data: global variables fixed
  • Text: code fixed

memory

Current Process are recorded in PCB

  • Program Counter
  • Processor Registers

Process State

  • New: The process is being created
  • Ready: The process is waiting to be assigned to a processor
  • Running: Instructions are being executed
  • Waiting: The process is waiting for some event to occur
  • Terminated: The process has finished execution

process state

Process Control Block (PCB)

  • Process ID
  • Process State
  • Program Counter
  • CPU Registers
  • Process Memory Address
  • CPU Scheduling Information
  • Accounting Information
  • I/O Status Information

Scheduling

Process scheduler selects among available processes for next execution on CPU core.

Maintains scheduling queues of processes:

  • Ready queue
  • Wait queue

Representation of Process Scheduling

Context switch

  • Save current process state
  • Load next process state
  • Switch to the next process

The switch time is pure overhead, and depends on the hardware (number of registers)

context switch

Scheduler

  • Long-term scheduler: selects processes (which ones get admitted to the ready queue); slow
  • Short-term scheduler: CPU scheduling (which process runs next); provides concurrency; fast

Operations

Process Creation

  • A parent process creates child processes (forming a tree)
    • resource sharing
    • execution : concurrent / the parent waits for the child to terminate
    • address space: duplicate with fork() / load a new program with exec()

Process Creation in UNIX

When a process calls fork(), the child starts executing from the statement right after the one the parent last executed.

e.g. three fork() calls produce 8 processes.

Process Termination

  • The child terminates (exit)
  • The parent reclaims the child’s resources (via wait())
  • abort(): cascading termination
  • Orphan process: the parent terminates while the child keeps running
  • Zombie process: the child has terminated but the parent has not reclaimed its resources
  • Solution for orphan and zombie processes: the init process (pid=1)

Interprocess Communication


AspectShared memoryMessage passing
Core ideaShared memory, direct reads and writesMessages relayed through the kernel, indirect communication
SpeedFastSlow
ComplexityHigh (needs synchronization)Low (abstracted by the system)
Suited toSingle machine, high frequency, tight couplingDistributed, loose coupling
SynchronizationExplicit (e.g. mutexes, semaphores)Implicit (e.g. message queues)
ScalabilityPoor (limited by memory size)Good (scales to distributed systems)

Shared Memory

Producer process produces information that is consumed by a consumer process

  • Unbounded buffer
  • Bounded buffer

Message Passing

Direct Communication

Explicit naming

Communication link: must be explicitly established and closed

Example:
Process A calls send(B, “Hello”) to send a message directly to process B.
Process B receives the message from A via receive(A, msg).

Indirect Communication

Messages are directed to and received from mailboxes (also referred to as ports)

  • Each mailbox has a unique ID
  • Processes can communicate only if they share a mailbox

Synchronous

  • Blocking
    • The sender blocks until the receiver is ready and the message has been received
    • The receiver blocks until a message is received
  • Non-blocking

Pipe

A pipe is a half-duplex communication channel that lets the output of one process serve directly as the input of another.

In essence it is a pseudo-file implemented on top of a kernel buffer.

Require parent-child relationship.

Ordinary Pipe / Anonymous Pipe

Data can only flow from the write end of the pipe to the read end; it is unidirectional.

The communicating processes must have a parent-child relationship and be on the same machine.

Named Pipe

Communication is bidirectional.

No parent-child relationship

Communication in Client-Server Systems

Socket

An IP address uniquely identifies a device on the network, while a port number distinguishes the different network services on the same device and locates the specific process.

Create a socket:

  • Server side
    • bind()
    • listen()
    • accept()
    • read() / write()
    • close()
  • Client side
    • connect()
    • read() / write()
    • close()

Every connection has a unique pair of sockets; processes can only exchange plain byte streams and cannot forward structured packets to each other.

Remote Procedure Call

RPC is a communication protocol for distributed computing that allows one process to call a function in another process.

RPC turns a local function call into a remote function call by communicating over the network.

  • Local call:
    • Call the local function
    • Return the result

Translated from the Chinese original.

Welcome to my other publishing channels

中文