Docsity

Lecture Slides on Process and Threads - Operating Systems | CMSC 412, Study Guides, Projects, Research of Operating Systems

Partial preview of the text

Download Lecture Slides on Process and Threads - Operating Systems | CMSC 412 and more Study Guides, Projects, Research Operating Systems in PDF only on Docsity!

CMSC 412

Fall 2005

Processes and Threads

Announcements

• Reading

  • Chapter 3-
  • Chapter 6
    • Skipping chapter 5 for now

Processes

  • What is a process?
    • A program in execution
      • Either sequentially or with multiple “threads of control.”
  • What’s not a process?
    • A program on a disk - a process is an active object, but a program is just a file

Computation Abstractions

CPU 1 CPU 2

p1 p2 p3 p t t t t t t t t

A dual-processor computer

Processes Threads

Process State Transitions

new ready running waiting terminated admitted (^) interrupt dispatch I/O request or^ I/O request or event wait event wait done Kill exit

Components of a Process

  • Memory Segments
    • Program - often called the text segment
    • Data - global variables
    • Stack - contains activation records
    • Heap - contains dynamically-allocated data
  • Processor Registers
    • Control registers
      • program counter - next instruction to execute
      • stack pointer
      • processor status word (from cmp instructions)
    • General purpose registers
    • Floating-point registers

Scheduling

  • OS must decide when a process is allowed

to run; I.e. it must schedule processes

  • Long-term scheduler (or job scheduler) –

selects which processes should be brought

into the ready queue.

  • Short-term scheduler (or CPU scheduler) –

selects which process should be executed

next and allocates CPU. A.k.a the

dispatcher.

Schedulers

  • Short-term scheduler is invoked very

frequently (milliseconds) ⇒ (must be fast).

  • Long-term scheduler is invoked very

infrequently (seconds, minutes) ⇒ (may be

slow).

  • The long-term scheduler controls the

degree of multiprogramming.

  • Processes can be described as either:
    • I/O- bound process – spends more time doing I/O than computations, many short CPU bursts.
    • CPU - bound process – spends more time doing computations; few very long CPU bursts.

Context Switch

  • When the OS switches a CPU to another

process, it is called a context switch.

  • The system must save the state of the old

process and load the saved state for the

new process.

  • Context-switch time is overhead; the

system does no useful work while switching.

  • Total time depends on hardware support.
  • Writing context-switch routines almost always requires some assembly language

OS Process Control Block

  • Stores all of the information about a process
  • PCB contains
    • Process state: new, ready, etc.
    • (saved) processor registers
    • Memory Management Information
      • page tables, limit registers for segments
    • CPU scheduling information
      • process priority; pointers to process queues
    • Accounting information
      • time used (and limits); files used; program owner; parent process id
    • I/O status information
      • list of open files; pending I/O operations

Sample PCB

Context Switch using PCBs

Storing PCBs

  • Track which processes are in which states
    • Collection of PCBs is called a process table
  • How to store the process table?
  • First Option:
    • Simple, but slow to find processes
    • Also need additional datastructures for fairness P1 P2 P2 P3 (^) P4 P Ready Waiting (^) New Term Waiting (^) Ready

Queues of Processes

• Store processes in state-based queues

P1 P Ready Queue P3 P Disk Queue P Network Queue

State Transitions with Queues

new term

Forking a New Process

• New process is called the child; the

process that created it is the parent

• Create a PCB for the new process

  • copy most entries from the parent
  • clear accounting fields
  • buffer pending I/O
  • allocate a PID for the new process

Process Termination

  • Process can terminate itself
    • via the exit system call
  • One process can terminate another process
    • use the kill system call
    • can any process kill any other process?
      • No, that would be bad.
      • Normally an ancestor can terminate a descendant
  • OS kernel can terminate a process
    • exceeds resource limits
    • tries to perform an illegal operation

Orphan Processes

• What if a parent terminates before

the child?

  • the child called an orphan process
    • in UNIX - becomes child of the root process
    • in VMS - terminated

UNIX example

  • Terminated process
    • signals parent of its death (SIGCHLD)
    • is called a zombie in UNIX
    • remains around waiting to be reclaimed
  • Parent process
    • wait system call retrieves info about the dead process - exit status - accounting information
    • signal handler is generally called the reaper
      • since its job is to collect the dead processes

Kernel Mode and User Mode

User Process Kernel User Process Unique: Program Stack Heap Unique: Program Stack Heap Idle Thread Kernel Threads: Each has own stack (separate from user mode) Share heap with other kernel threads Run same program (kernel) as other kernel threads System Calls Initial Thread Kernel Mode thread of A user process

Execution Abstractions

  • Kernel Threads
    • Threads that run with kernel privileges
  • User Threads
    • Threads running in user space
    • Kernel may not be aware of them
  • Processes
    • An execution context with an address space
    • Visible to and scheduled by the kernel
  • Light-Weight Processes
    • An execution context sharing an address space
    • Visible to and scheduled by the kernel

Multithreading Models

• Many-to-One

• One-to-One

• Many-to-Many

Many-to-One Model

One-to-one Model

Why not multiple threads?

• Complexity:

  • Dealing with safety, liveness, composition

• Overhead

  • Higher resource usage

• Check out CMSC 433 for lots of

information on threads and their

alternatives!

Thread Systems

• Specifications

  • POSIX Threads (Pthreads)
  • WinThreads
  • Java Threads

• See the text for many examples