Download Processes

Document related concepts

Security-focused operating system wikipedia , lookup

Copland (operating system) wikipedia , lookup

Unix security wikipedia , lookup

CP/M wikipedia , lookup

VS/9 wikipedia , lookup

DNIX wikipedia , lookup

Burroughs MCP wikipedia , lookup

Distributed operating system wikipedia , lookup

Process management (computing) wikipedia , lookup

Transcript
Chapter 3: Processes
Operating System Concepts – 9th Edition
Silberschatz, Galvin and Gagne ©2013
Objectives
 To introduce the notion of a process
 To describe process scheduling, creation,
termination, and communication
 To explore interprocess communication using shared
memory and message passing
 To describe communication in client-server systems
Operating System Concepts – 9th Edition
3.2
Silberschatz, Galvin and Gagne ©2013
OS Major Functions
 Interleave the execution of multiple processes, to
maximize processor utilization while providing reasonable
response time
 Allocate resources to processes
 Support interprocess communication and user creation of
processes
Operating System Concepts – 9th Edition
3.3
Silberschatz, Galvin and Gagne ©2013
Process
 A program in execution
 An instance of a program running on a computer
 The entity that can be assigned to and executed on a
processor
 A unit of activity characterized by

the execution of a sequence of instructions

a current state

an associated set of system resources
Operating System Concepts – 9th Edition
3.4
Silberschatz, Galvin and Gagne ©2013
Process Concept
 Multiple parts

The program code, also called text section

Current activity context 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 during
run time
Operating System Concepts – 9th Edition
3.5
Silberschatz, Galvin and Gagne ©2013
Process Concept (Cont.)
 Program is passive entity stored on disk (executable
file), process is active

Program becomes process when executable file loaded
into memory
 How to execute a program?

Execution of a program starts via GUI mouse click,
command line entry of its name, etc.
 Can we have one program in several processes?

Consider multiple users executing the same program on
cse.unl.edu server
Operating System Concepts – 9th Edition
3.6
Silberschatz, Galvin and Gagne ©2013
Process in Memory
Operating System Concepts – 9th Edition
3.7
Silberschatz, Galvin and Gagne ©2013
Process Stack
Parameter passing in a read procedure call:
Figure (a) The stack before the call to read. (b) The stack
while the called procedure is active.
Operating System Concepts – 9th Edition
3.8
Silberschatz, Galvin and Gagne ©2013
Process State
 As a process executes, it changes state

new: The process is being created

running: Instructions are being executed

waiting: The process is waiting for some event to occur
 e.g.,
a
a completion of an I/O operation or
semaphore signal from another process

ready: The process is waiting to be assigned to a
processor

terminated: The process has finished execution
Operating System Concepts – 9th Edition
3.9
Silberschatz, Galvin and Gagne ©2013
Diagram of Process State
Operating System Concepts – 9th Edition
3.10
Silberschatz, Galvin and Gagne ©2013
Process Elements (I)
 Identifier
 State
 Priority
How to describe a process?
 What
Memory pointers:
to code and
data included in
elements
are
it?
 Program counter
Operating System Concepts – 9th Edition
3.11
Silberschatz, Galvin and Gagne ©2013
Process Elements (II)
 Context data: value of CPU registers
 I/O status information

Outstanding I/O requests

Assigned I/O devices and used files
 Accounting information

Amount of processor time & clock time used

Time limits
Operating System Concepts – 9th Edition
3.12
Silberschatz, Galvin and Gagne ©2013
Process Control Block (PCB)
 Each process is represented by a PCB
 Created and managed by the operating system
 Contains the process elements
Operating System Concepts – 9th Edition
3.13
Silberschatz, Galvin and Gagne ©2013
Process Control Block
• Allows support
for multiple
processes
Operating System Concepts – 9th Edition
3.14
Silberschatz, Galvin and Gagne ©2013
OS Control Tables
Tables are constructed
for each entity (i.e.,
process, resource) the
operating system
manages
Operating System Concepts – 9th Edition
3.15
Silberschatz, Galvin and Gagne ©2013
Process Tables
 Manage processes
 A process is composed of program, data, heap, stack, and
attributes ---- process image

User Data
 The
modifiable part of the user space. May include program
data, a heap area, and programs that may be modified.

User Program
 The

program to be executed.
Stack
 Each
process has one or more last-in-first-out (LIFO) stacks
associated with it. A stack is used to store parameters and
returning addresses for procedure and system calls.

Process Control Block
 Data
needed by the OS to control the process
Operating System Concepts – 9th Edition
3.16
Silberschatz, Galvin and Gagne ©2013
Process Location
 Where are the processes located?

To manage and execute a process, at least a small portion
of its image must be maintained in main memory
 OS must know the location of each page of each
process image, achieved by process tables
Operating System Concepts – 9th Edition
3.17
Silberschatz, Galvin and Gagne ©2013
Process Images
Operating System Concepts – 9th Edition
3.18
Silberschatz, Galvin and Gagne ©2013
PCB: Process Control Information
 Data structuring: a process may be linked to other
process in a queue, ring, or some other structure.

For example, all ready processes for a particular priority
level may be linked in a queue.

A process may exhibit a parent-child (creator-created)
relationship with another process.

The process control block may contain pointers to other
processes to support these structures.
Operating System Concepts – 9th Edition
3.19
Silberschatz, Galvin and Gagne ©2013
Process Creation
 Assign a unique process identifier
 Allocate space for the process
 Initialize process control block
 Set up appropriate linkages
 Create or expand other data structures
Operating System Concepts – 9th Edition
3.20
Silberschatz, Galvin and Gagne ©2013
When to Switch Process
 Clock interrupt

process has executed for the maximum allowable time
slice
 I/O interrupt
 Memory fault

memory address is in disk so it must be brought into main
memory
Operating System Concepts – 9th Edition
3.21
Silberschatz, Galvin and Gagne ©2013
When to Switch Process
 Trap

error or exception occurred

may cause process to be moved to Exit state
 System Call

Inter-process communication

I/O operation such as file open

Lead to a transfer to an OS routine
Operating System Concepts – 9th Edition
3.22
Silberschatz, Galvin and Gagne ©2013
How to Switch Process?
Operating System Concepts – 9th Edition
3.23
Silberschatz, Galvin and Gagne ©2013
CPU Switch From Process to Process
Operating System Concepts – 9th Edition
3.24
Silberschatz, Galvin and Gagne ©2013
Process (Context) Switch
 When CPU switches to another process, the system
must save the state of the old process and load the
saved state for the new process via a context switch
 Context of a process represented in the PCB
 Context-switch time is overhead; the system does no
useful work while switching

The more complex the OS and the PCB  the longer the
context switch
 Time dependent on hardware support

Some hardware provides multiple sets of registers per
CPU  multiple contexts loaded at once
Operating System Concepts – 9th Edition
3.25
Silberschatz, Galvin and Gagne ©2013
Process (Context) Switch
 Save context of processor including program counter
and other registers
 Update the process control block of the process that is
currently in the Running state
 Move process control block to appropriate queue – e.g.,
ready; blocked
 Select another process for execution
 Update the process control block of the process selected
 Restore context of the selected process
 Update memory-management data structures (page
tables, TLB, etc.)
Operating System Concepts – 9th Edition
3.26
Silberschatz, Galvin and Gagne ©2013
 Process Switch
vs.
 Mode Switch
Operating System Concepts – 9th Edition
3.27
Silberschatz, Galvin and Gagne ©2013
Execution of the Operating System
 Process-based operating system

Implement the OS as a collection of system processes

Process switch for OS service
Operating System Concepts – 9th Edition
3.28
Silberschatz, Galvin and Gagne ©2013
Execution of the Operating System
 Execution Within User Processes
Operating
Mode
system software within context of a user process
switch for OS service
Operating System Concepts – 9th Edition
3.29
Silberschatz, Galvin and Gagne ©2013
OS Executes in User Space
• Mode switch for OS service
Operating System Concepts – 9th Edition
3.30
Silberschatz, Galvin and Gagne ©2013
Process vs. Mode Switch
 Save context of processor including program counter
and other registers
 Update the process control block of the process that is
currently in the Running state
 Move process control block to appropriate queue –
ready; blocked; ready/suspend
 Select another process for execution
 Update the process control block of the process selected
 Update memory-management data structures
 Restore context of the selected process
Operating System Concepts – 9th Edition
3.31
Silberschatz, Galvin and Gagne ©2013
Process vs. Mode Switch
 Save context of processor including program counter
and other registers
 Update the process control block of the process that is
currently in the Running state
 Move process control block to appropriate queue –
ready; blocked; ready/suspend
 Select another process for execution
 Update the process control block of the process selected
 Restore context of the selected process
 Update memory-management data structures
Operating System Concepts – 9th Edition
3.32
Silberschatz, Galvin and Gagne ©2013
Mode Switch
 Save context of processor including program counter and
other registers
 Update the process control block of the running process
 Mode changes to kernel mode, finishes the OS routine
 Mode changes back, restore the context, and continue the
running process in user mode
Operating System Concepts – 9th Edition
3.33
Silberschatz, Galvin and Gagne ©2013
Process Scheduling (I)
 What does process scheduling do?
 What is the objective of doing process scheduling?
Operating System Concepts – 9th Edition
3.34
Silberschatz, Galvin and Gagne ©2013
Process Scheduling (II)
 To maximize CPU use and for time sharing, quickly
switch processes onto CPU
 Process scheduler selects among available processes
for next execution on CPU
 Maintains scheduling queues of processes

Ready queue – set of all processes residing in main
memory, ready and waiting to execute

Device queues – set of processes waiting for an I/O
device

Processes migrate among the various queues
Operating System Concepts – 9th Edition
3.35
Silberschatz, Galvin and Gagne ©2013
Process Scheduling (III)
 Three different categories of process scheduler

Short-term scheduler

Long-term scheduler

Medium-term scheduler
Operating System Concepts – 9th Edition
3.36
Silberschatz, Galvin and Gagne ©2013
Short-term Scheduler
 Short-term scheduler (or CPU scheduler or
Dispatcher) – selects which process should be executed
next and allocates CPU

Sometimes the only scheduler in a system

Short-term scheduler is invoked frequently (milliseconds) 
(must be fast)
 Trace of the Process

Sequence of instructions that execute for a process

Dispatcher switches the processor from one process to
another  Interleaving process traces
Operating System Concepts – 9th Edition
3.37
Silberschatz, Galvin and Gagne ©2013
Example Execution
Operating System Concepts – 9th Edition
3.38
Silberschatz, Galvin and Gagne ©2013
Trace of Process
Operating System Concepts – 9th Edition
3.39
Silberschatz, Galvin and Gagne ©2013
Combined Trace of Process
Operating System Concepts – 9th Edition
3.40
Silberschatz, Galvin and Gagne ©2013
Long-term Scheduler
 Long-term scheduler (or job scheduler) – selects which
processes should be admitted (i.e., brought into ready queue)

The long-term scheduler controls the (maximum) degree of
multiprogramming

Long-term scheduler is invoked infrequently (seconds, minutes)
 (may be slow)
 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
 Long-term scheduler strives for good process mix
Operating System Concepts – 9th Edition
3.41
Silberschatz, Galvin and Gagne ©2013
Addition of Medium Term Scheduling

Medium-term scheduler can be added if (actual) degree
of multiprogramming needs to change

Remove process from memory, store on disk, bring back in
from disk to continue execution: swapping

Present in all systems with virtual memory (Chap 9)
Operating System Concepts – 9th Edition
3.42
Silberschatz, Galvin and Gagne ©2013
Suspended Processes
 Processor is faster than I/O so all executable processes
could be waiting for I/O, while there are some new
processes waiting to be admitted
 Swap these processes to disk to free up more memory
to admit new processes
 Blocked state becomes suspend state when swapped to
disk
 Two new states

Blocked/Suspend

Ready/Suspend
Operating System Concepts – 9th Edition
3.43
Silberschatz, Galvin and Gagne ©2013
Two Suspend States
Operating System Concepts – 9th Edition
3.44
Silberschatz, Galvin and Gagne ©2013
Operations on Processes
 System must provide mechanisms for:

process creation,

process termination,

and so on as detailed next
Operating System Concepts – 9th Edition
3.45
Silberschatz, Galvin and Gagne ©2013
Process Creation
 Parent process create children processes, which, in turn
create other processes, forming a tree of processes
 Generally, process identified and managed via a process
identifier (pid)
 Resource sharing options

Parent and children share all resources

Children share subset of parent’s resources

Parent and child share no resources

For example, rfork in FreeBSD permits fine-grained sharing of
resources between parent and child processes
 Execution options

Parent and children execute concurrently (e.g., fork)

Parent waits until children terminate (e.g., vfork)
Operating System Concepts – 9th Edition
3.46
Silberschatz, Galvin and Gagne ©2013
Process Creation (Cont.)
 Address space

Child duplicate of parent
 UNIX example

fork() system call creates new process
Operating System Concepts – 9th Edition
3.47
Silberschatz, Galvin and Gagne ©2013
fork()
 Process creation in Unix could be made by means of the
kernel system call, fork()
 When a process issues a fork request, the OS:

Allocates a slot in the process table for the new process;

Assigns a unique process ID to the child process;

Makes a copy of the process image of the parent, with the
exception of any shared memory;

Increments counters for any files owned by the parent, to reflect
that an additional process now also owns those files;

Assigns the child process to the Ready to Run state;

Returns the ID number of the child to the parent process, and a 0
value to the child process.
Operating System Concepts – 9th Edition
3.48
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
3.49
Silberschatz, Galvin and Gagne ©2013
Process Creation (Cont.)
 Address space

Child duplicate of parent

Child has a program loaded into it
 UNIX examples

fork() system call creates new process

exec() system call used after a fork() to replace the
process’ memory space with a new program
Operating System Concepts – 9th Edition
3.50
Silberschatz, Galvin and Gagne ©2013
C Program Forking Separate Process
Operating System Concepts – 9th Edition
3.51
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 – 9th Edition
sshd
pid = 3028
kthreadd
pid = 2
pdflush
pid = 200
sshd
pid = 3610
tcsch
pid = 4005
emacs
pid = 9204
3.52
Silberschatz, Galvin and Gagne ©2013
Process Termination
 Process executes last statement and then asks the
operating system to delete it using the exit() system
call

Returns status data from child to parent (via wait())

Process’ resources are deallocated by operating system
 Parent may terminate the execution of children
processes using the abort() system call.
Operating System Concepts – 9th Edition
3.53
Silberschatz, Galvin and Gagne ©2013
Process Termination
 Some operating systems do not allow child to exists if its
parent has terminated. If a process terminates, then all its
children must also be terminated
cascading
termination: all children, grandchildren, etc. are
terminated. The termination is initiated by the operating system.
 The parent process may wait for termination of a child process
by using the wait()system call. The call returns status
information and the pid of the terminated process
pid = wait(&status);
Operating System Concepts – 9th Edition
3.54
Silberschatz, Galvin and Gagne ©2013
Process Termination
 If no parent waiting (did not invoke wait()), the terminated
child process is a zombie, exit status kept in process table.
 Some operating systems allow child to exists if its parent has
terminated.
parent terminated without invoking wait , process is an
orphan, Linux and UNIX assign the init process as the new parent
to orphaned processes, who invokes wait periodically to collect the
exit status of any orphaned process and release its pid and
process-table entry.
If
Operating System Concepts – 9th Edition
3.55
Silberschatz, Galvin and Gagne ©2013
Multiprocess Application
 What are the reasons that we sometimes want to
develop multiprocess applications?
Operating System Concepts – 9th Edition
3.56
Silberschatz, Galvin and Gagne ©2013
Cooperating Processes
 Processes within a system may be independent or
cooperating
 Independent process cannot affect or be affected by the
execution of another process
 Cooperating process can affect or be affected by the
execution of another process
 Advantages of process cooperation

Information sharing: video surveillance

Computation speed-up: merge sort

Modularity
 multiprocess

browser; concurrent server system
Convenience: a user’s multiple processes
Operating System Concepts – 9th Edition
3.57
Silberschatz, Galvin and Gagne ©2013
Multiprocess Architecture – Chrome Browser
 Google Chrome Browser is multiprocess with 3 different types
of processes:
Browser
process manages user interface, disk and network I/O
Renderer
process renders web pages, deals with HTML,
Javascript. A new renderer created for each website opened
 Runs
in sandbox restricting disk and network I/O, minimizing effect
of security exploits
Plug-in
process for each type of plug-in
Operating System Concepts – 9th Edition
3.58
Silberschatz, Galvin and Gagne ©2013
Example: Concurrent Server
Dispatcher
process
Request dispatched to
Server
a worker process
Worker
process
Operating System Concepts – 9th Edition
3.59
Silberschatz, Galvin and Gagne ©2013
Interprocess Communication
Cooperating processes need interprocess
communication (IPC)
Two models of IPC

Shared memory

Message passing
Operating System Concepts – 9th Edition
3.60
Silberschatz, Galvin and Gagne ©2013
Communications Models
(a) Message Passing
Operating System Concepts – 9th Edition
(b) Shared Memory
3.61
Silberschatz, Galvin and Gagne ©2013
Producer-Consumer Problem
 Paradigm for cooperating processes: producer process
produces information that is consumed by a consumer
process
Operating System Concepts – 9th Edition
3.62
Silberschatz, Galvin and Gagne ©2013
Interprocess Communication – Shared Memory
 An area of memory shared among the processes that wish to
communicate
 The communication is under the control of the user
processes, not the operating system
 Major issue is to provide mechanism that will allow the user
processes to synchronize their actions when they access
shared memory
 Synchronization is discussed in great detail in Chapter 5
Operating System Concepts – 9th Edition
3.63
Silberschatz, Galvin and Gagne ©2013
Examples of IPC Systems - POSIX
 POSIX Shared Memory

Process first creates shared memory segment
shm_fd = shm_open(name, O CREAT | O RDWR,
0666);

Also used to open an existing segment to share it

Set the size of the object
ftruncate(shm_fd, 4096);

Now the process could write to the shared memory
sprintf(shared memory, "Writing to shared
memory");
Operating System Concepts – 9th Edition
3.64
Silberschatz, Galvin and Gagne ©2013
IPC POSIX Producer
Operating System Concepts – 9th Edition
3.65
Silberschatz, Galvin and Gagne ©2013
IPC POSIX Consumer
Operating System Concepts – 9th Edition
3.66
Silberschatz, Galvin and Gagne ©2013
Interprocess Communication – Message Passing
 Mechanism for processes to communicate and to
synchronize their actions
 Message system – processes communicate with each
other without resorting to shared variables
 IPC facility provides two operations:

send(message)

receive(message)
 The message size is either fixed or variable
 Self-read Chap 3.4 on message passing and understand
how to solve the producer and consumer problem using
message passing
Operating System Concepts – 9th Edition
3.67
Silberschatz, Galvin and Gagne ©2013
Reading Assignment
 By next Wednesday, finish reading Chap 4
Operating System Concepts – 9th Edition
3.68
Silberschatz, Galvin and Gagne ©2013