Sunteți pe pagina 1din 41

Operating Systems

CMPSCI 377
Lecture 3: OS Structures
Lecture 4: Processes

Emery Berger
University of Massachusetts, Amherst

UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science


Last Time:
OS & Computer Architecture
 Modern OS Functionality (brief
review)
 Architecture Basics
 Hardware Support for OS Features

UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 2


This Time:
OS Structures & Processes
 Components
 OS Organizations (kernels)
 Processes

UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 3


Components
 Process
 Synchronization
 Memory management
 File system
 Secondary storage
 I/O systems
 Distributed systems

UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 4


Process
Memory mgmt.
File system
Secondary
storage
I/O systems
Distributed sys.

Key Component: The Process


 OS manages variety of activities:
 User programs, batch jobs, command

scripts
 Daemons: spoolers, name servers, file

servers, etc.
 Each activity encapsulated in process:
 Context (PC, registers, address space,

etc.) required for activity to run


 Process != program


One program may comprise many processes 5
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
Process
Memory mgmt.
File system
Secondary
storage
I/O systems
Distributed sys.

Processes
 OS manages & schedules
processes
 Creation, deletion
 Resource allocation (e.g., CPU,
memory)
 Suspension & resumption

 OS supports processes
 Inter-process communication
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 6
Process
Memory mgmt.
File system
Secondary

Process Synchronization storage


I/O systems
Distributed sys.

Example
 Banking transactions
 Cooperating processes operate on
single account
 ATM transactions
 Balance computation
 Monthly interest calculation &
addition

 All may access same account


simultaneously
UNIVERSITY OF
M ,A Department of Computer Science
ASSACHUSETTS MHERST • 7
Process
Memory mgmt.
File system
Secondary
storage
I/O systems
Distributed sys.

Memory Management
 Main memory:
 Direct access storage for CPU
 Processes: must be in main memory
to execute
 OS must:
 Maintain page tables
(virtual/physical memory)
 Allocate & deallocate memory
 Decide how much memory each
process gets
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 8
Process
Memory mgmt.
File system
Secondary
storage
I/O systems
Distributed sys.

File System
 Secondary storage devices (e.g.,
disks)
 Block-level: read, write to point on
disk
 Too crude to use directly for long-
term storage
 File system provides logical
objects (files) & operations on
these objects
UNIVERSITY
 OF MASSACHUSETTS, AMHERST • Department of Computer Science 9
Process
Memory mgmt.
File system
Secondary
storage
I/O systems
Distributed sys.

File System Organization


 Supports hierarchical organization
of files
 Files grouped in directories
 Maintains metadata about files:
 Date created
 Last modified date
 Controls access to files
 Who owns & can alter files
 Read-only, executable, etc.
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 10
Process
Memory mgmt.
File system
Secondary
storage
I/O systems
Distributed sys.

File System Management


 Standard interface to:
 Create & delete files, directories
 Manipulate files & directories
 Read, write, extend, rename, copy,
protect
 Map files into memory
 May provide other general
services:
 Backups
UNIVERSITY
 OF MASSACHUSETTS, AMHERST • Department of Computer Science 11
Process
Memory mgmt.
File system
Secondary

Secondary Storage (Disks, storage


I/O systems
Distributed sys.

etc.)
 Persistent memory
 Endures system failures

 Low-level OS routines handle disk


functions:
 Scheduling disk operations
 Head movement
 Error handling
 May keep track of free space
 Sometimes these routines are in
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 12
Process
Memory mgmt.
File system
Secondary
storage
I/O systems
Distributed sys.

I/O Systems
 Support communication with
external devices:
 Terminal, keyboard, printer, mouse
 Support buffering & spooling of
I/O
 Provide general device driver
interface
 Hides differences between devices
 Often mimics file system interface
 Provide implementations of device
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 13
Process
Memory mgmt.
File system
Secondary
storage
I/O systems
Distributed sys.

Distributed Systems
 Distributed system = collection of
processors that do not share memory or
clock
 Processes must communicate over network
 OS must deal with failures & deadlock
 Problems specific to distributed systems

 OS can support distributed file


system
 Manages multiple independent storage
devices
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 14
OS Structures & Processes
 Components
 OS Organizations (kernels)
 Processes

UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 15


Monolithic Kernel
 Classic
UNIX
approach
, Linux
 Everythi
ng in
kernel
+Fast
- Risky

UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 16


Layered OS Design
 “THE” operating system
 Dijkstra
+Modular, simple,
portable, easy to
design/debug
- Communication
overhead,
copying,
bookkeeping
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 17
The Microkernel Approach

 Goal:
Minimize
contents of
kernel
 Why?

UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 18


Microkernel: Motivation
 Minimize contents of kernel:
 Reduces risk of crashing OS
 Put functionality in user-level
processes
 Simplifies extension & customization
 First μ-kernel: Hydra (CMU)
 Current systems: Mach (also CMU),
by Rick Rashid et al. (now head of
Microsoft Research)
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 19
μ-kernels vs. Monolithic
Kernels
 Past conventional wisdom: (1990’s)
 Mach – beautiful research idea but “failed”
in practice

Too slow!
 Linux – ugly, monolithic, but fast

 Today: much faster computers


 Mach: fast enough (Mac OS X)

 Reliability, simplicity, robustness


now more important than performance
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 20
OS Structures & Processes
 Components
 OS Organizations (kernels)
 Processes

UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 21


Processes
 Process Concept
 Process States
 Process Scheduling
 Process Management
 Interprocess Communication

UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 22


Process Concept
 OS executes variety of programs:
 Batch system – jobs
 Time-shared systems – user programs or
tasks
 Process – program in execution
 process execution sequential (kind of)
 Process includes:
 program counter
 stack
 data section
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 23
Process States
 New
 Process being created

 Running
 Instructions being executed

 Waiting
 Process waiting for some event to occur

 Ready
 Process waiting to be assigned to a

processor
 Terminated
 (duh)
UNIVERSITY OF
M ,A Department of Computer Science
ASSACHUSETTS MHERST • 24
Process State Diagram
 Transitions:
 Program
actions
(system
calls)
 OS actions
(schedulin
g)
 External
events
(interrupts)

UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 25


Process Execution Example
1. New
void main() { 2. Ready
printf (“Hello world\n”); 3. Running
} 4. Waiting for I/O
5. Ready
6. Running
7. Terminated

UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 26


Process Data Structures
 Process Control Block:
 Tracks state

 OS allocates, places on

queue
 OS deallocates on

termination

 Lots of info:
 Process state

 Program counter

 CPU registers

 CPU scheduling

information
 Memory-management

information
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 27
Process Scheduling Queues
 Job queue
 Set of all processes in system
 Ready queue
 Set of processes residing in main memory
ready & waiting to execute
 Device queues
 Set of processes waiting for I/O device
 One per device

 Process migration between the various


queues.
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 28
Process Queues Example

UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 29


CPU Scheduling

UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 30


Process Scheduling

UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 31


PCBs and Hardware State
 Switching processes: context
switch
 Relatively expensive
 Time between switches (quantum)
must be long enough to amortize
this cost
 Start:
 OS picks ready process
 Loads register values from PCB
 Stop:
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 32
Process Creation
 One process can create other processes
 Creator = parent, new processes =
children
 Parent can wait for child to complete,
or continue in parallel

 UNIX: fork() used to create child


processes
 Copies variables & registers from parent to
child
 Memory lazily copied (copy-on-write)
 Only difference between parent & child:
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 33
Process Creation Example
 Logging into UNIX creates shell process
 Every command typed into shell:
 Child of shell process (spawned by fork)
 Executes command via exec
 Example:
 Type “emacs”
 OS forks new process
 exec executes emacs
 If followed by “&”, runs in parallel;
otherwise, waits until done
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 34
Example UNIX Program: Fork

UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 35


Process Termination
 On termination, OS reclaims all
resources assigned to process
 UNIX processes:
 Can terminate self via exit system
call
 Can terminate child via kill system
call

UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 36


Example: Process
Termination

UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 37


Cooperating Processes
 Cooperating processes work
together to accomplish a task
 Advantages:
 Can improve performance by
overlapping activities or performing
work in parallel
 Can enable simpler program design
 Simplifies distribution
 Processes can live on different
UNIVERSITY OF Mmachines
ASSACHUSETTS, AMHERST • Department of Computer Science 38
Interprocess Communication
Processes communicate in one of
two ways:
 Message passing:
 Send and receive information
 Numerous means: sockets, pipes,
etc.
 Shared memory:
 Establish mapping to named
memory object
UNIVERSITY
 OF MASSACHUSETTS, AMHERST • Department of Computer Science 39
Process Summary
 Process = unit of execution
 Represented by Process Control
Blocks
 Contain process state, scheduling info, etc.
 Process state:
 New, Ready, Waiting, Running, or
Terminated
 One running process at a time (on a
uniprocessor)
 Context switch when changing process
executing on CPU
 Communicate by message passing or
UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 40
The End

UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 41

S-ar putea să vă placă și