Processes

Lecture 3, Week 1 September 29, 2016


Review

  • Interrupts halt the execution of a process and transfer control (execution) to the operating system
    • Interrupts are ranked in terms of priority:
      • Higher priority (lower rank) interrupts are handled first.
    • Interrupts can spawn while the OS is executing

  • Interrupts are used by devices to have the OS do stuff
    • Alternatives:
      • Polling: The CPU loops until the OS is done.

The Process

The process is the OS abstraction for allowing programs to use the CPU (execution)

  • The unit of execution and scheduling (what runs what doesn’t run)
  • The dynamic execution context of a program

Equivalent terms: job, task, sequential process

A sequential process is program in execution; in other words, a process is a running program.

We must load a program into a process to run the program.

Process Components

The process contains all state for a program in execution.

A process has a unique identifier, called a process ID (PID)

Basic Process Address Space

Everything the process needs is stored in its address space.

The address space is partioned into a few components, some are defined below:

Code
Text segment
Static Data
Data Segment
Heap
Dynamic Memory Alloc
Stack

Function calls, etc.; the stack pointer “points” to the beginning of the stack (first in, last out).

This is dynamic as well!

Process State

Each process has an execution state that indicates what it is currently doing:

Runing
Execution instruction on the CPU (scheduled on the CPU)
Ready
Waiting to be assigned to the CPU (scheduled)
Waiting

Waiting for an event; e.g., I/O completion

The process can not continue or make progress until the event completes.

As a process executes, it moves from state to state * Unix ps‘s STAT/S column indicates execution state

Note

A system can only support a finite number of processes – this number is set by the system adminstrator. The system can also hit this limit if it runs out of space to allocate to another process.

Process Data Structures

The OS has a data stucture, called the Process Control Block (PCB) that stores all the processes.

The PCB contains all of the info about a process and hardware execution state (PC, SP, regs, etc.) when the process is not running:

  • This state is everything that is needed to restore the hardware to the same configuration it was in when the process was switched out of the hardware.

PCB Data Structure

Reffered to as a heavyweight (i.e., a lot is stored inside of it) abstraction.

Note

Keep this is mind for next week’s lecture on threads, as processes are large.

PCBs and Hardware State

When the process is running, its hardware state (PC, SP, regs, etc.) is in the CPU.
  • The hardware registers contain the current value.

When the OS stops running a process, it saves the current values of the registers into the process’ PCB

When the OS is ready to start executing a new process, it loads the hardware registers from the values stored in that process’ PCB.

Context Switch
The process of changing the CPU hardware state from one process to another

Note

This can happen 100 or 1000 times a second

State Queues

The OS maintains a collection of queues that represent the state of all processes in the system

Typically, the OS has one queue for each state
  • Ready, waiting, etc.

Each PCB is queued on a state queue according to its current state

As a process changes state, its PCB is unlinked from one queue and linked into another.

Note

There may be many wait queues, one for each type of wait (disk, etc.)

PCBs and State Queues

PCBs are data structures dynamically created in OS address space

Process Creation

A process is created by another process:

Parent Process
The creator
Child Process
Created by parent (Unix: ps PPID field)

The first process is a special kind: init. All other process are either create by init or children of init.

A system call invokes the creation of a new process. * on Windows: CreateProcess * on Unix: fork()

Process Creation: Unix

Unix fork()

Unlike Window’s equivalent CreateProcess, fork takes no arguments.

It initializes a new process by copying (not sharing) the address space of the parent.

It returns twice:
  • Returns the child’s PID to the parent, and 0 to the child.

Note

When it comes to sheduling, there is no way to know which process will run first.

Why fork()?

It is very useful when the child...:
  • is cooperating with the parent
  • relies upon the parent’s data to accomplish its task

exec

To start running a new program, Unix provides the exec function.

What does exec() do?
  • Stops the current process
  • Loads the program prog into the process’ address space
  • Initializes hardware context and args for the new program
  • Places the PCB onto the ready queue

Note

It does not create a new process

exec() changes the program that is running inside a process shell.

What is difference between fork and exec

fork() is used to create a new process, exec is used to load a program into the address space.

fork() will return an error if it runs out of processes. exec() returns an error when given a program that doesn’t exist.

Process Termination

To terminate a process on...
  • Windows: ExitProcess(int status)
  • Unix: exit(int status)
Essentially, free resources and terminates:
  • Terminates all threads (next lecture)
  • Close open files, network connections
  • Allocated memory (and VM pages out on disk)
  • Remove PCB from kernel data structures, delete

Process Waiting

Often it is convenient to pause until a child process has finished.
  • Think of executing commands in a shell

On Unix: wait() On Windows: WaitForSingleObject

wait() returns the status code of the child process that it returned by invoking exit()

Note

Every process must be “reaped” by a parent. If a child process exits, and no ancestor can reap it, the process is known as a zombie process