Chapter 3: Processes Silberschatz, Galvin and Gagne ©2013 – 2

advertisement
Chapter 3: Processes
Operating System Concepts Essentials – 2nd Edition
Silberschatz, Galvin and Gagne ©2013
Chapter 3: Processes
 Process Concept
 Process Scheduling
 Operations on Processes
 Interprocess Communication
 Examples of IPC Systems
 Remote Procedure Call (RPC)
Operating System Concepts Essentials – 2nd Edition
3.2
Silberschatz, Galvin and Gagne ©2013
Objectives
 Introduce notion of process

Program in execution

Basis of all computation
 Describe various features of processes

scheduling, creation, termination, communication
 Explore interprocess communication

Shared memory, message passing
Operating System Concepts Essentials – 2nd Edition
3.3
Silberschatz, Galvin and Gagne ©2013
Process Concept
 OS executes variety of programs:

Batch system – jobs

Time-shared systems – user programs or tasks
 Textbook uses the terms job and process almost
interchangeably
 Process – a program in execution; process execution
must progress in sequential fashion
Operating System Concepts Essentials – 2nd Edition
3.4
Silberschatz, Galvin and Gagne ©2013
Parts of a Process
 Program code (text section)
 Current activity including:

program counter

processor registers
 Stack containing temporary data

Function parameters, return addresses, local variables
 Data section containing global variables
 Heap containing memory dynamically allocated
Operating System Concepts Essentials – 2nd Edition
3.5
Silberschatz, Galvin and Gagne ©2013
Process Concept (Cont.)
 Program = passive entity stored on disk (executable)

Process is active

Program becomes process when exe loaded into memory
 One program can be several processes

E.g., multiple users executing same program
Operating System Concepts Essentials – 2nd Edition
3.6
Silberschatz, Galvin and Gagne ©2013
Process in Memory
Operating System Concepts Essentials – 2nd Edition
3.7
Silberschatz, Galvin and Gagne ©2013
Process State
 As process executes, it changes state

new: process being created

running: instructions being executed

waiting: process waiting for some event

ready: process waiting to be assigned to a processor

terminated: process finished execution
Operating System Concepts Essentials – 2nd Edition
3.8
Silberschatz, Galvin and Gagne ©2013
Diagram of Process State
Operating System Concepts Essentials – 2nd Edition
3.9
Silberschatz, Galvin and Gagne ©2013
Process Control Block (PCB)
 Info associated with process

(aka task control block)
 Process state
 Program counter
 CPU registers
 CPU scheduling info
 Memory-management info
 Accounting info
 I/O status info
Operating System Concepts Essentials – 2nd Edition
3.10
Silberschatz, Galvin and Gagne ©2013
CPU Switch From Process to Process
Operating System Concepts Essentials – 2nd Edition
3.11
Silberschatz, Galvin and Gagne ©2013
Threads
 So far, process has single thread of execution
 Consider multiple program counters per process

Multiple locations can execute at once
 Multiple
threads of control -> threads
 Must have storage for thread details

Multiple program counters in PCB
Operating System Concepts Essentials – 2nd Edition
3.12
Silberschatz, Galvin and Gagne ©2013
Process Representation in Linux
Represented by the C structure task_struct
pid t_pid; /* process identifier */
long state; /* state of the process */
unsigned int time_slice /* scheduling information */
struct task_struct *parent; /* this process’s parent */
struct list_head children; /* this process’s children */
struct files_struct *files; /* list of open files */
struct mm_struct *mm; /* address space of this process */
Operating System Concepts Essentials – 2nd Edition
3.13
Silberschatz, Galvin and Gagne ©2013
Process Scheduling
 Maximize CPU use => quickly switch processes
onto CPU for time sharing
 Process scheduler selects among available
processes for next execution on CPU
Operating System Concepts Essentials – 2nd Edition
3.14
Silberschatz, Galvin and Gagne ©2013
Process Scheduling
 Maintains scheduling queues of processes

Job queue – set of all processes in system

Ready queue – set of all processes residing in main
memory, ready and waiting to execute

Device queues – set of processes waiting for I/O

Processes migrate among these queues
Operating System Concepts Essentials – 2nd Edition
3.15
Silberschatz, Galvin and Gagne ©2013
Ready Queue And Various I/O Device Queues
Operating System Concepts Essentials – 2nd Edition
3.16
Silberschatz, Galvin and Gagne ©2013
Representation of Process Scheduling

Queueing diagram represents queues, resources, flows
Operating System Concepts Essentials – 2nd Edition
3.17
Silberschatz, Galvin and Gagne ©2013
Schedulers
 Short-term scheduler (or CPU scheduler) – selects
process to be executed next and allocates CPU

Sometimes only scheduler in system

Short-term scheduler is invoked frequently
 Order

of milliseconds
must be fast
Operating System Concepts Essentials – 2nd Edition
3.18
Silberschatz, Galvin and Gagne ©2013
Schedulers
 Long-term scheduler (or job scheduler) – selects
processes to be brought into ready queue

Invoked infrequently
 Order


of seconds, minutes
may be slow
Controls degree of multiprogramming
Operating System Concepts Essentials – 2nd Edition
3.19
Silberschatz, Galvin and Gagne ©2013
Schedulers
 Processes described as either:

I/O-bound– spends more time doing I/O than
computations, many short CPU bursts

CPU-bound – spends more time doing
computations; few very long CPU bursts
 Long-term scheduler strives for good process mix
Operating System Concepts Essentials – 2nd Edition
3.20
Silberschatz, Galvin and Gagne ©2013
Addition of Medium Term Scheduling

Medium-term scheduler added if degree of
multiprogramming needs to decrease

Remove process from memory, store on disk, bring
back from disk to continue execution: swapping
Operating System Concepts Essentials – 2nd Edition
3.21
Silberschatz, Galvin and Gagne ©2013
Multitasking in Mobile Systems
 Some mobile systems (e.g., early version of iOS)
allow only one process to run, others suspended
 Due to screen real estate, user interface limits iOS

Single foreground process
 Controlled

via user interface
Multiple background processes
 In
memory, running, but not on display
Operating System Concepts Essentials – 2nd Edition
3.22
Silberschatz, Galvin and Gagne ©2013
Multitasking in Mobile Systems
 Android runs foreground and background processes

Background process uses services to perform tasks

Services keep running even if background process is
suspended

Service has no user interface, small memory use
Operating System Concepts Essentials – 2nd Edition
3.23
Silberschatz, Galvin and Gagne ©2013
Context Switch
 When CPU switches to another process, system must
perform context switch

Save the state of old process

Load saved state for new process
 Context of process represented in PCB
 Context-switch time is true overhead

System does no useful work while switching

The more complex the OS and PCB

the longer the context switch
Operating System Concepts Essentials – 2nd Edition
3.24
Silberschatz, Galvin and Gagne ©2013
Operations on Processes
 System must provide mechanisms for:

process creation,

process termination,

and so on...
Operating System Concepts Essentials – 2nd Edition
3.25
Silberschatz, Galvin and Gagne ©2013
Process Creation
 Parent process create children processes

Children can create other processes

Forms tree of processes
 Process identified, managed via process identifier

PID
Operating System Concepts Essentials – 2nd Edition
3.26
Silberschatz, Galvin and Gagne ©2013
Process Creation
 Resource sharing options

Parent and children share all resources

Children share subset of parent’s resources

Parent and child share no resources
 Execution options

Parent and children execute concurrently

Parent waits until children terminate
Operating System Concepts Essentials – 2nd Edition
3.27
Silberschatz, Galvin and Gagne ©2013
A Tree of Processes in Linux
init
pid = 1
login
pid = 8415
khelper
pid = 6
bash
pid = 8416
ps
pid = 9298
Operating System Concepts Essentials – 2nd Edition
sshd
pid = 3028
kthreadd
pid = 2
pdflush
pid = 200
sshd
pid = 3610
tcsch
pid = 4005
emacs
pid = 9204
3.28
Silberschatz, Galvin and Gagne ©2013
Process Creation (Cont.)
 Address space

Child duplicate of parent

Child has a program loaded into it
 UNIX examples

fork() creates new process

exec() replaces process memory space with new
program
Operating System Concepts Essentials – 2nd Edition
3.29
Silberschatz, Galvin and Gagne ©2013
C Program Forking Separate Process
Review...
Operating System Concepts Essentials – 2nd Edition
3.30
Silberschatz, Galvin and Gagne ©2013
Creating a Separate Process via Windows API
YUCK!!!
Operating System Concepts Essentials – 2nd Edition
3.31
Silberschatz, Galvin and Gagne ©2013
Process Termination
 Process executes last statement
 Asks operating system to delete it using exit()

Returns status data from child to parent (via wait())

Process’ resources deallocated by operating system
Operating System Concepts Essentials – 2nd Edition
3.32
Silberschatz, Galvin and Gagne ©2013
Process Termination
 Parent may terminate execution of children via
abort() system call
 Some reasons for doing so:

Child has exceeded allocated resources

Task assigned to child no longer required

Parent is exiting, and OS does not allow orphans
Operating System Concepts Essentials – 2nd Edition
3.33
Silberschatz, Galvin and Gagne ©2013
Process Termination
 Some Operating Systems do not allow children to
exists if parent has terminated
 If process terminates, then all children also terminated

Cascading termination: all children, grandchildren, etc.

Termination initiated by OS
Operating System Concepts Essentials – 2nd Edition
3.34
Silberschatz, Galvin and Gagne ©2013
Process Termination
 Parent may wait for termination of child

Using wait()system call
 wait()
returns status info and PID of terminated child
 If no parent waiting, child becomes zombie
 If parent terminated without wait(), child becomes
orphan
Operating System Concepts Essentials – 2nd Edition
3.35
Silberschatz, Galvin and Gagne ©2013
Multiprocess Architecture – Chrome Browser
 Many web browsers run as single process

If one web site causes trouble, entire browser can hang
 Google Chrome is a multiprocess, 3 different :

Browser

Renderer

Plug-in
Operating System Concepts Essentials – 2nd Edition
3.36
Silberschatz, Galvin and Gagne ©2013
Interprocess Communication
 Processes may be independent or cooperating

Cooperating: can affect or be affected by others

Independent: cannot affect or be affected by others
 Reasons for cooperating processes:

Information sharing

Computation speedup

Modularity

Convenience
Operating System Concepts Essentials – 2nd Edition
3.37
Silberschatz, Galvin and Gagne ©2013
Interprocess Communication
 Cooperating processes need interprocess
communication (IPC)
 Two models of IPC

Shared memory

Message passing
Operating System Concepts Essentials – 2nd Edition
3.38
Silberschatz, Galvin and Gagne ©2013
Communications Models
(a) Message passing.
Operating System Concepts Essentials – 2nd Edition
(b) shared memory.
3.39
Silberschatz, Galvin and Gagne ©2013
Producer-Consumer Problem
 Paradigm for cooperating processes
 Producer process produces information that is
consumed by a consumer process

unbounded-buffer places no limit on buffer size

bounded-buffer assumes fixed buffer size
Operating System Concepts Essentials – 2nd Edition
3.40
Silberschatz, Galvin and Gagne ©2013
Interprocess Communication – Shared Memory
 Area of memory shared among processes
 Communication under control of users, not OS
 Requires synchronization of shared memory access
 Synchronization is discussed in Chapter 5
Operating System Concepts Essentials – 2nd Edition
3.41
Silberschatz, Galvin and Gagne ©2013
Interprocess Communication – Message Passing
 Mechanism for processes to communicate and
synchronize actions
 Message system – communication without shared
variables
 IPC facility provides two operations:

send(message)

receive(message)
 Message size is either fixed or variable
Operating System Concepts Essentials – 2nd Edition
3.42
Silberschatz, Galvin and Gagne ©2013
Message Passing (Cont.)
 If two processes wish to communicate, they must:

Establish a communication link between them

Exchange messages via send/receive
Operating System Concepts Essentials – 2nd Edition
3.43
Silberschatz, Galvin and Gagne ©2013
Message Passing (Cont.)
 Implementation issues:

How are links established?

Can link be associated with more than two processes?

How many links can there be between every pair of
communicating processes?

What is the link capacity?

Is message size fixed or variable?

Is a link unidirectional or bi-directional?
Operating System Concepts Essentials – 2nd Edition
3.44
Silberschatz, Galvin and Gagne ©2013
Message Passing (Cont.)
 Implementation of communication link

Physical:
 Shared
memory
 Hardware bus
 Network

Logical:



Direct or indirect
Synchronous or asynchronous
Automatic or explicit buffering
Operating System Concepts Essentials – 2nd Edition
3.45
Silberschatz, Galvin and Gagne ©2013
Direct Communication
 Processes must name each other explicitly:

send (P, message) – send a message to process P

receive(Q, message) – receive a message from Q
 Properties of communication link

Links established automatically

Link associated with exactly one pair of processes

Exactly one link between pairs

Link may be unidirectional or bi-directional
Operating System Concepts Essentials – 2nd Edition
3.46
Silberschatz, Galvin and Gagne ©2013
Indirect Communication
 Messages are directed and received from
mailboxes (AKA ports)

Each mailbox has unique id

Processes can communicate only if share a mailbox
 Properties of communication link

Link established if processes share common mailbox

Link may be associated with many processes

Each pair of processes may share several
communication links

Link may be unidirectional or bi-directional
Operating System Concepts Essentials – 2nd Edition
3.47
Silberschatz, Galvin and Gagne ©2013
Indirect Communication
 Operations

create new mailbox (port)

send / receive messages through mailbox

destroy mailbox
 Primitives defined as:
send(A, message) – send message to mailbox A
receive(A, message) – receive message from
mailbox A
Operating System Concepts Essentials – 2nd Edition
3.48
Silberschatz, Galvin and Gagne ©2013
Indirect Communication
 Mailbox sharing

P1, P2, and P3 share mailbox A

P1, sends; P2 and P3 receive

Who gets the message?
 Solutions

Allow link to associate with at most two processes

Allow only one process at a time to execute receive

Allow system to arbitrarily select receiver
 Sender
is notified who receiver is
Operating System Concepts Essentials – 2nd Edition
3.49
Silberschatz, Galvin and Gagne ©2013
Synchronization
 Message passing may be blocking or non-blocking
 Blocking is considered synchronous

Blocking send - sender blocks until message recv

Blocking receive - receiver blocks until message
available
Operating System Concepts Essentials – 2nd Edition
3.50
Silberschatz, Galvin and Gagne ©2013
Synchronization
 Non-blocking is asynchronous

Non-blocking send - sender sends message,
continues

Non-blocking receive - receiver receives:

A valid message, or

Null message
 Different combinations possible

rendezvous - If both send and receive are blocking
Operating System Concepts Essentials – 2nd Edition
3.51
Silberschatz, Galvin and Gagne ©2013
Synchronization (Cont.)
 Producer-consumer becomes trivial
message next_produced;
while (true) {
/* produce an item in next produced */
send(next_produced);
}
message next_consumed;
while (true) {
receive(next_consumed);
/* consume the item in next consumed */
}
Operating System Concepts Essentials – 2nd Edition
3.52
Silberschatz, Galvin and Gagne ©2013
Buffering
 Queue of messages attached to link
 Implemented in one of three ways

Zero capacity - no messages are queued


Bounded capacity - finite length of n messages
 If

Sender must wait for receiver (rendezvous)
link full, sender waits
Unbounded capacity - infinite length
 Sender
never waits
Operating System Concepts Essentials – 2nd Edition
3.53
Silberschatz, Galvin and Gagne ©2013
Examples of IPC Systems - POSIX
 POSIX Shared Memory

Process creates shared memory segment

shm_fd = shm_open(name, O CREAT | O RDWR, 0666);

Also used to open existing segment to share

Set size of shared memory object
 ftruncate(shm

fd, 4096);
Now process can write to shared memory

sprintf(shared memory, "Writing to shared memory");
Operating System Concepts Essentials – 2nd Edition
3.54
Silberschatz, Galvin and Gagne ©2013
Remote Procedure Calls
 Remote procedure call (RPC) abstracts procedure
calls between processes on networked systems

Uses ports for service differentiation
 Stubs - client-side proxy for procedure on server
 Client-side stub locates server, marshalls parameters
Operating System Concepts Essentials – 2nd Edition
3.55
Silberschatz, Galvin and Gagne ©2013
Remote Procedure Calls
 Server-side stub receives message

Unpacks marshalled parameters

Performs procedure on server
 On Windows, stub code compile from specification

Microsoft Interface Definition Language (MIDL)
Operating System Concepts Essentials – 2nd Edition
3.56
Silberschatz, Galvin and Gagne ©2013
Execution of RPC
Operating System Concepts Essentials – 2nd Edition
3.57
Silberschatz, Galvin and Gagne ©2013
Live Coding
 Review of POSIX API

create threads

join threads

mutexes

semaphores

condition variables
Operating System Concepts Essentials – 2nd Edition
3.58
Silberschatz, Galvin and Gagne ©2013
Download