Tải bản đầy đủ (.pdf) (61 trang)

Lecture Operating systems: A concept-based approach (2/e): Chapter 3 - Dhananjay M. Dhamdhere

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.13 MB, 61 trang )

PROPRIETARY MATERIAL. ©  2007 The McGraw­Hill Companies, Inc. All rights reserved. No part of this PowerPoint slide  may be displayed, reproduced or distributed 
in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw­Hill 
for their individual course preparation. If you are a student using this PowerPoint slide, you are using it without permission. 

Chapter 3:
Processes and Threads

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2 ed 

Slide No: 1
Copyright © 2008


What is a process?



A process is an execution of a program
(Note the emphasis on ‘an’)
– A programmer uses the notion of a process to achieve
concurrency within an application program
* Such a program can complete earlier than a sequential program

– An OS uses the notion of a process to control execution of a
program
* This way, the OS can handle execution of sequential and
concurrent programs in a uniform manner

Chapter 3:
Processes and Threads



Dhamdhere: Operating Systems—
A Concept­Based Approach, 2 ed 

Slide No: 2
Copyright © 2008


Example of processes in an application



Consider a satellite data logging application
– Specification
* The satellite sends samples to earth periodically
* The computer receives them in a special register
* The application periodically copies a sample into a file on disk
* This is a real time application — a sample has to be copied before
the next sample arrives

– Four processes are created for the application (see next slide)
* The OS creates the primary process
* This process creates three processes as its child processes by
making system calls

Chapter 3:
Processes and Threads

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2 ed 


Slide No: 3
Copyright © 2008


Tasks in a real time application for data logging

• Process 1 copies the sample into a buffer in memory
• Process 2 copies the sample from the buffer into the file
• Process 3 performs housekeeping and statistical analysis
Chapter 3:
Processes and Threads

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2 ed 

Slide No: 4
Copyright © 2008


Process tree for the real time application

• The OS creates the primary process when the application is
initiated; it is called ‘main’ in this diagram
• The primary process creates the other three processes through
system calls; they are its child processes
Chapter 3:
Processes and Threads

Dhamdhere: Operating Systems—

A Concept­Based Approach, 2 ed 

Slide No: 5
Copyright © 2008


Benefits of child processes



Use of child processes has three benefits
– Computation speed-up
* OS may interleave operation of CPU-bound and I/O-bound child
processes of an application; it speeds up operation of the
application

– Priority for critical functions
* A child process could be created to perform a critical function. It can
be assigned a high priority to satisfy its time constraints

– Protecting a parent process from errors
* The kernel terminates a child process if an error occurs during its
operation; however, the parent process is not affected
* Beneficial to execute non-trusted code as a child process

Chapter 3:
Processes and Threads

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2 ed 


Slide No: 6
Copyright © 2008


Concurrency and Parallelism



Child processes of a process may be able to operate
independent of one another

Q: Is this concurrency or parallelism?

Chapter 3:
Processes and Threads

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2 ed 

Slide No: 7
Copyright © 2008


Concurrency and Parallelism



Child processes of a process may be able to operate
independent of one another. Is this concurrency or

parallelism?
– Parallelism: Operation at the same time. Parallelism is not
possible unless
* The computer has many CPUs
* The processes are truly independent of one another, so that they
can be scheduled simultaneously

– Concurrency: Operation in a manner that gives the impression of
parallelism, but actually only one process can operate at any
time

Chapter 3:
Processes and Threads

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2 ed 

Slide No: 8
Copyright © 2008


Process interaction



Processes of an application may interact among
themselves in four different ways
– Data sharing
* Data updates must be coordinated to ensure consistency


– Message passing
* Used for exchanging information

– Synchronization
* Used to ensure that processes perform their actions in a desired
order

– Signals
* A signal conveys occurrence of an exceptional situation

Chapter 3:
Processes and Threads

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2 ed 

Slide No: 9
Copyright © 2008


OS view of a process



The OS uses the notion of processes to organize
execution of programs
– The OS views a process as
* An entity to which resources are allocated
* A unit of work to be performed


– The OS performs scheduling to organize operation of processes

Chapter 3:
Processes and Threads

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2 ed 

Slide No: 10
Copyright © 2008


OS view of a process



A process is a tuple with the following components
(id, code, data, stack, resources, CPU state)
– The process id is used by the OS to uniquely identify the
process
– Code, data and the stack form the address space of the process
– Resources are allocated to the process by the OS
– The CPU state is comprised of the values in the CPU registers
and in the fields of the PSW

Chapter 3:
Processes and Threads

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2 ed 


Slide No: 11
Copyright © 2008


OS view of a process

• The process environment consists of the process address space and
information concerning various resources allocated to the process
• The PCB contains execution state of the process, e.g., its CPU state
Chapter 3:
Processes and Threads

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2 ed 

Slide No: 12
Copyright © 2008


Process environment



The process environment consists all information
needed for accessing and controlling resources allocated
to a process (it is also called the process context ):








Address space of the process, i.e., code, data, and stack
Memory allocation information
Status of file processing activities, e.g., file pointers
Process interaction information
Resource information
Miscellaneous information

Chapter 3:
Processes and Threads

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2 ed 

Slide No: 13
Copyright © 2008


The process control block (PCB)



The PCB contains information needed for controlling
operation of the process. Its fields are:










Process id
Ids of parent and child processes
Priority
Process state (defined later)
CPU state, which is comprised of the PSW and CPU registers
Event information
Signal information
PCB pointer (used for forming a list of PCBs)

Chapter 3:
Processes and Threads

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2 ed 

Slide No: 14
Copyright © 2008


Fundamental functions for controlling processes

• Occurrence of an event causes an interrupt
• The context save function saves the state of the process that was in operation
• Scheduling selects a process; dispatching switches the CPU to its execution

Chapter 3:
Processes and Threads

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2 ed 

Slide No: 15
Copyright © 2008


Context save



The notion of state
– The CPU state indicates what the process in operation is doing
at any moment (see Chapter 2)
– Every resource allocated to a process has a state; every activity
also has a state. For example,
* The state of a file is the information in its records,
* The state of a file processing activity is information about which
record was accessed last



The context save function saves the CPU state of the
process, and states of its resource access activities
– The saved states of the CPU and resources are used for
resuming the process at a later time


Chapter 3:
Processes and Threads

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2 ed 

Slide No: 16
Copyright © 2008


Event handling



An event is a situation which is of interest to the OS
– Occurrence of an event is notified to the kernel through an
interrupt. It now performs the following tasks:
* Find which event caused the interrupt, e.g.
 A process made a resource request
 An I/O operation completed
* Take appropriate actions, e.g.,
 service a system call
 service a timer interrupt, realize that a time slice has elapsed,
and preempt the process in operation
 note that the I/O operation started by a process has completed

Chapter 3:
Processes and Threads

Dhamdhere: Operating Systems—

A Concept­Based Approach, 2 ed 

Slide No: 17
Copyright © 2008


Scheduling and dispatching



After event handling, the kernel should switch the CPU
to servicing of a process
– The scheduling function identifies a process for servicing
– The dispatching function loads the context, i.e., process
environment, of the identified process so that it starts or resumes
its operation
* It involves loading CPU registers and the PSW, and other state
information from the process environment

Chapter 3:
Processes and Threads

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2 ed 

Slide No: 18
Copyright © 2008


Context switch




When an event occurs, OS may decide to switch the
CPU to a new process
– This action is called a context switch
– It is implemented through the following actions
* save the context of the process that was in operation before the
event occurred
* load the context of a new process so that it would start or resume its
operation

Q: When does a context switch become necessary?

Chapter 3:
Processes and Threads

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2 ed 

Slide No: 19
Copyright © 2008


Process States



A process state is an indicator of the current activity of a
process

– An OS designer defines process states to simplify functioning of
the OS, e.g., to simplify scheduling
– Some sample process states
* A process is waiting for an I/O operation to complete: blocked state
* The CPU is executing instructions of a process: running state

– The kernel keeps track of the state of a process and changes it
as its activity changes
– Different operating systems may use different process states

Chapter 3:
Processes and Threads

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2 ed 

Slide No: 20
Copyright © 2008


Fundamental process states



The fundamental process states are:
– Running
* A CPU is allocated to the process and is executing its instructions

– Blocked
* The process is waiting for a resource to be allocated or a specified

event to occur
 The process should not be scheduled until the awaited event
occurs

– Ready
* Process is not blocked but it is not running
 It can be considered for scheduling

– Terminated
* Operation of the process has completed
Chapter 3:
Processes and Threads

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2 ed 

Slide No: 21
Copyright © 2008


State transitions



Operation of a process
– A process has a state
– The state of a process changes when the nature of its activity
changes
* This change of state is called a state transition
* It is caused by an event


– Several state transitions can occur before the process
terminates
Q: What are the events that cause transitions between the
fundamental process states?
Chapter 3:
Processes and Threads

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2 ed 

Slide No: 22
Copyright © 2008


Fundamental state transitions for a process

• The transition ready → running occurs when the process is dispatched
• running → blocked occurs when it starts I/O or makes a request
• blocked → ready occurs when its I/O completes or its request is granted
Chapter 3:
Processes and Threads

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2 ed 

Slide No: 23
Copyright © 2008



Swapping and process states



Implementation of swapping
– The OS implements swapping through swap-in and swap-out
actions
– It uses some new states to implement swapping
* So that it can differentiate between processes that have been
swapped out and those that have not been
* State transitions are defined through which a process can enter and
exit these new states
 ready → ready swapped when a ready process is swapped out
and ready swapped → ready when it is swapped in
 Similar transitions between blocked and blocked swapped
states

Chapter 3:
Processes and Threads

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2 ed 

Slide No: 24
Copyright © 2008


Process states and state transitions in swapping

Two new states:

– Ready swapped
– Blocked swapped

Chapter 3:
Processes and Threads

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2 ed 

Slide No: 25
Copyright © 2008


×