Dhamdhere OS2E Chapter 03 Power Point Slides 2
-
Upload
praveen-kulkarni -
Category
Documents
-
view
229 -
download
0
Transcript of Dhamdhere OS2E Chapter 03 Power Point Slides 2
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 1/61
Chapter 3:Processes and Threads Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed Slide No: 1Copyright © 2008
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.
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 2/61
Chapter 3:Processes and Threads Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed Slide No: 2Copyright © 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
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 3/61
Chapter 3:Processes and Threads Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed Slide No: 3Copyright © 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
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 4/61
Chapter 3:Processes and Threads Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed Slide No: 4Copyright © 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
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 5/61
Chapter 3:Processes and Threads Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed Slide No: 5Copyright © 2008
Process tree for the real time application
• The OS creates the primary process when the application isinitiated; it is called ‘main’ in this diagram
• The primary process creates the other three processes through
system calls; they are its child processes
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 6/61
Chapter 3:Processes and Threads Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed Slide No: 6Copyright © 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 itsoperation; however, the parent process is not affected
* Beneficial to execute non-trusted code as a child process
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 7/61
Chapter 3:Processes and Threads Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed Slide No: 7Copyright © 2008
Concurrency and Parallelism
• Child processes of a process may be able to operate
independent of one another
Q: Is this concurrency or parallelism?
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 8/61
Chapter 3:Processes and Threads Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed Slide No: 8Copyright © 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
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 9/61
Chapter 3:Processes and Threads Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed Slide No: 9Copyright © 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
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 10/61
Chapter 3:Processes and Threads Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed Slide No: 10Copyright © 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
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 11/61
Chapter 3:Processes and Threads Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed Slide No: 11Copyright © 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
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 12/61
Chapter 3:Processes and Threads Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed Slide No: 12Copyright © 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
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 13/61
Chapter 3:Processes and Threads Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed Slide No: 13Copyright © 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
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 14/61
Chapter 3:Processes and Threads Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed Slide No: 14Copyright © 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)
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 15/61
Chapter 3:Processes and Threads Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed Slide No: 15Copyright © 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
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 16/61
Chapter 3:Processes and Threads
Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed Slide No: 16Copyright © 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
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 17/61
Chapter 3:Processes and Threads
Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed Slide No: 17Copyright © 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
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 18/61
Chapter 3:Processes and Threads
Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed
Slide No: 18Copyright © 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 resumesits operation
* It involves loading CPU registers and the PSW, and other state
information from the process environment
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 19/61
Chapter 3:Processes and Threads
Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed
Slide No: 19Copyright © 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?
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 20/61
Chapter 3:Processes and Threads
Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed
Slide No: 20Copyright © 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
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 21/61
Chapter 3:Processes and Threads
Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed
Slide No: 21Copyright © 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 specifiedevent to occur
The process should not be scheduled until the awaited eventoccurs
– Ready
* Process is not blocked but it is not running
It can be considered for scheduling
– Terminated
* Operation of the process has completed
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 22/61
Chapter 3:Processes and Threads
Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed
Slide No: 22Copyright © 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?
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 23/61
Chapter 3:Processes and Threads
Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed
Slide No: 23Copyright © 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
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 24/61
Chapter 3:Processes and Threads
Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed
Slide No: 24Copyright © 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 beenswapped 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
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 25/61
Chapter 3:Processes and Threads
Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed
Slide No: 25Copyright © 2008
Process states and state transitions in swapping
Two new states:
– Ready swapped – Blocked swapped
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 26/61
Chapter 3:Processes and Threads
Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed
Slide No: 26Copyright © 2008
Event handling actions of an OS
• The OS performs the following event handling actions
– Create a process
– Block a process
– Unblock a process
– Terminate a process
– Initiate an I/O operation
– Grant a resource
– Accept an interprocess message
– Deliver an interprocess message
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 27/61
Chapter 3:Processes and Threads
Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed
Slide No: 27Copyright © 2008
Event handling actions of an OS
Q: Are any arrows
missing?
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 28/61
Chapter 3:Processes and Threads
Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed
Slide No: 28Copyright © 2008
Event handling
• Occurrence of an event causes an interrupt
– The OS has to change states of affected processes
* The OS has to determine
which event caused the interrupt
which processes are affected by it and in what manner
– The OS uses an event control block (ECB) to find the process
affected by an event
* The ECB stores information about
an anticipated event
Id of the process whose state would be affected by the event
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 29/61
Chapter 3:Processes and Threads
Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed
Slide No: 29Copyright © 2008
Event Control Block (ECB)
• An event control block is formed for an event when the OS knows
that the event will occur
– e.g., it knows about an I/O completion event for a processwhen it initiates an I/O operation for it
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 30/61
Chapter 3:Processes and Threads
Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed
Slide No: 30Copyright © 2008
PCB-ECB interrelationship
• Process P i starts an I/O operation
• The OS forms an ECB for the ‘end of I/O operation’ event and
mentions P i as the process that will be affected by it
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 31/61
Chapter 3:Processes and Threads
Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed
Slide No: 31Copyright © 2008
Threads
• A thread is a program execution that uses the resources
of a process
– Threads are created within a process
– Switching between threads of a process causes less overhead
than switching between processes (Q: Why?)
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 32/61
Chapter 3:Processes and Threads
Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed
Slide No: 32Copyright © 2008
Thread switching overhead
• Why threads have a smaller switching overhead
– A process context switch involves:
1. Saving the context of the process in operation
2. Saving its CPU state
3. Loading the context of the new process
4. Loading its CPU state
– A thread is a program execution within the context of a process
(i.e., it uses the resources of a process); many threads can be
created within the same process* Hence process context need not be switched while switching
between threads of the same process
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 33/61
Chapter 3:Processes and Threads
Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed
Slide No: 33Copyright © 2008
Threads
• Where are threads useful?
– If two processes share the same address space and the same
resources, they have identical context
* Switching between these processes involves saving and reloading
of identical contexts. This overhead is redundant – In such situations, it is better to use threads
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 34/61
Chapter 3:Processes and Threads
Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed
Slide No: 34Copyright © 2008
Threads in Process P i :
(a) Concept, (b) Implementation
• Each thread has a stack of its own• Execution of a thread is managed by creating a thread control block (TCB) for it
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 35/61
Chapter 3:Processes and Threads
Dhamdhere: Operating Systems— A Concept-Based Approach, 2 ed
Slide No: 35Copyright © 2008
Benefits of threads
• Threads have three key benefits
– Low switching overhead
– Computation speed-up
* By exploiting concurrency within an application
* Parallelism can be exploited through some specific kinds of threads(see next slide)
– Efficient communication
* Threads of a process can communicate through shared data space
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 36/61
Chapter 3:
Processes and Threads
Dhamdhere: Operating Systems—
A Concept-Based Approach, 2 ed Slide No: 36
Copyright © 2008
Kinds of threads
• Three kinds of threads are used – Kernel-level threads
* Threads are created through system calls. The kernel is aware of their existence, and schedules them
– User-level threads
* Threads are created and maintained by a thread library, whoseroutines are linked to become a part of the process code. Thekernel is oblivious of user-level threads
– Hybrid threads
* A combination of the above two
Q: Why have three kinds of threads?
A: They have different properties concerning switching overhead,concurrency and parallelism
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 37/61
Chapter 3:
Processes and Threads
Dhamdhere: Operating Systems—
A Concept-Based Approach, 2 ed Slide No: 37
Copyright © 2008
Kernel-level and user-level threads
• Kernel-level threads
– Switching is performed by the kernel
* High thread switching overhead
* High parallelism; threads can operate in parallel
• User-level threads – Switching is performed by the thread library
* Low thread switching overhead
– A blocking call by a thread blocks the process
* Hence no other thread of the process can operate – Low parallelism
* At most one thread of a process can operate at any time
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 38/61
Chapter 3:
Processes and Threads
Dhamdhere: Operating Systems—
A Concept-Based Approach, 2 ed
Slide No: 38
Copyright © 2008
Scheduling of kernel-level threads
• At a ‘create thread’ call, the kernel creates a thread and a
thread control block (TCB) for it• Scheduler examines all TCBs and selects one of them• Dispatcher dispatches the thread corresponding to the selected TCB
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 39/61
Chapter 3:
Processes and Threads
Dhamdhere: Operating Systems—
A Concept-Based Approach, 2 ed
Slide No: 39
Copyright © 2008
Scheduling of user-level threads
• At a ‘create thread’ request, thread library creates a thread and a TCB• Thread library performs ‘scheduling’ of threads within a process; we call
it ‘mapping’ of a TCB into the PCB of a process• Kernel’s scheduler uses PCBs to schedule a process
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 40/61
Chapter 3:
Processes and Threads
Dhamdhere: Operating Systems—
A Concept-Based Approach, 2 ed
Slide No: 40
Copyright © 2008
Actions of the thread library (N, R, and B indicaterunning , ready and blocked states)
(a) Process P i is ready; threads h1 –h3 are ‘running’, ready, and blocked, resp
(b) Process P i is scheduled
(c) The thread library suspends thread h1 and schedules thread h2
(d) Thread h2 initiates I/O on device d , hence P i becomes blocked
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 41/61
Chapter 3:
Processes and Threads
Dhamdhere: Operating Systems—
A Concept-Based Approach, 2 ed
Slide No: 41
Copyright © 2008
Hybrid thread models
• Hybrid threads have elements of both kernel-level and
user-level threads
– Each user-level thread has a thread control block (TCB)
* Provides fast thread switching through thread library
– Each kernel-level thread has kernel thread control block (KTCB)
* Provides parallelism within a process
– Three models of associating user-level and kernel-level threads
* Many-to-one association
Resembles user-level threads* One-to-one association
Resembles kernel-level threads
* Many-to-many association
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 42/61
Chapter 3:
Processes and Threads
Dhamdhere: Operating Systems—
A Concept-Based Approach, 2 ed
Slide No: 42
Copyright © 2008
Associations in hybrid thread models
(a) Many-to-one association: scheduling is done by thread library
(b) One-to-one association: scheduling is done by kernel
(c) Many-to-many association: scheduling by thread library and kernel
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 43/61
Chapter 3:
Processes and Threads
Dhamdhere: Operating Systems—
A Concept-Based Approach, 2 ed
Slide No: 43
Copyright © 2008
Process Interactions
Processes may interact in four different ways
• Sharing of data
– Data consistency should be preserved
• Synchronization of actions
– Processes should perform their actions in a desired order • Passing of messages
– Messages should be stored and delivered appropriately
• Sending of Signals
– Processes should be able to send signals to other processes and
specify signal handling actions to be performed when signals are sent tothem
The OS performs message passing and provides facilities for the other
three modes of interaction
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 44/61
Chapter 3:
Processes and Threads
Dhamdhere: Operating Systems—
A Concept-Based Approach, 2 ed
Slide No: 44
Copyright © 2008
An example of data sharing: airline reservations
• Agents answer queries and perform bookings• They share the reservations data
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 45/61
Chapter 3:
Processes and Threads
Dhamdhere: Operating Systems—
A Concept-Based Approach, 2 ed
Slide No: 45
Copyright © 2008
Race conditions in data sharing
• Results of operations performed on shared data may be
wrong if race conditions exist
– Let f i (d s ) and f j (d s ) represent the value of d s after the operations
– If processes P i and P j perform operations Oi and O j
* If Oi is performed before O j , resulting value of d is f j (f i (d s ))
* If O j is performed before Oi , resulting value of d is f i (f j (d s ))
* A race condition exists if the result is not one of the two
Q: Why do race conditions arise?
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 46/61
Chapter 3:
Processes and Threads
Dhamdhere: Operating Systems—
A Concept-Based Approach, 2 ed
Slide No: 46
Copyright © 2008
Data sharing by processes of a reservation system
• Process P i performs actions S1, S2.1• Process P j performs actions S1, S2.1, S2.2
• The same seat is allocated to both the processes
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 47/61
Chapter 3:
Processes and Threads
Dhamdhere: Operating Systems—
A Concept-Based Approach, 2 ed
Slide No: 47
Copyright © 2008
Race condition in the airline reservation system
• Race conditions in the airline reservation system may
have two consequences:
– nextseatno may not be updated properly
– Same seat number may be allocated to two passengers
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 48/61
Chapter 3:
Processes and Threads
Dhamdhere: Operating Systems—
A Concept-Based Approach, 2 ed
Slide No: 48
Copyright © 2008
Race conditions in the airline reservations system
Q: Which of them hasa race condition?
Three possible
executions are shown
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 49/61
Chapter 3:
Processes and Threads
Dhamdhere: Operating Systems—
A Concept-Based Approach, 2 ed
Slide No: 49
Copyright © 2008
Control synchronization between processes
(a) Initiation of P j should be delayed until P i performs si
(b) After performing S j-1, P j should be delayed until P i performs si
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 50/61
Chapter 3:
Processes and Threads
Dhamdhere: Operating Systems—
A Concept-Based Approach, 2 ed
Slide No: 50
Copyright © 2008
Interprocess messages
• In the send command, process P i specifies the message to be sent to P j • In the receive command, process P j specifies the memory area where
it wishes to receive a message
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 51/61
Chapter 3:
Processes and Threads
Dhamdhere: Operating Systems—
A Concept-Based Approach, 2 ed
Slide No: 51
Copyright © 2008
Benefits of message passing
• Message passing has several benefits
– Data sharing is not necessary
– Processes may belong to different applications
– Messages cannot be tampered with by a process
– Kernel guarantees correctness
* A message is delivered to the correct processes
* A process is blocked when it wishes to receive a message and no
undelivered messages exist for it
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 52/61
Chapter 3:
Processes and Threads
Dhamdhere: Operating Systems—
A Concept-Based Approach, 2 ed
Slide No: 52
Copyright © 2008
Signals
• A process can send signals to other processes to convey
exceptional situations
– A process must anticipate signals from other processes and
must provide a signal handler for each signal
* It makes a system call to specify a signal handler – The kernel activates the signal handler when a signal is sent to
the process
– Schematic of signals on the next slide
* It uses signal vectors that are analogous to interrupt vectors
Addresses of signal handlers are entered in the signal vector
The correct signal handler is invoked when a signal is sent
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 53/61
Chapter 3:
Processes and Threads
Dhamdhere: Operating Systems—
A Concept-Based Approach, 2 ed
Slide No: 53
Copyright © 2008
Signal handling
(a) Actions when process P i makes an init_sig call to install a signal handler
(b) Actions when signal sig1 is sent while P i is executing instruction b1
Firm arrow : Pointers
in data structures
Dashed arrows: Executiontime actions
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 54/61
Chapter 3:
Processes and Threads
Dhamdhere: Operating Systems—
A Concept-Based Approach, 2 ed
Slide No: 54
Copyright © 2008
Processes in Unix
• A process operates in two modes—user mode and kernel
mode
– When a process makes a system call, it enters the kernel mode
and itself executes the kernel code for handling the system call
– It reenters the user mode after handling the system call – Accordingly, there are two running states: User running and
kernel running
* A process in the kernel running state is non-interruptible
* It gets blocked when it makes an I/O request; another process is
now scheduled and may make a system call
Kernel code is written in a reentrant manner, so a process can
enter kernel mode even if other processes are blocked in that
mode
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 55/61
Chapter 3:
Processes and Threads
Dhamdhere: Operating Systems—
A Concept-Based Approach, 2 ed
Slide No: 55
Copyright © 2008
Process state transitions in Unix
• I/O request: User running → Kernel running → Blocked • End of time slice: User running → Kernel running → Ready
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 56/61
Chapter 3:
Processes and Threads
Dhamdhere: Operating Systems—
A Concept-Based Approach, 2 ed
Slide No: 56
Copyright © 2008
Threads in Solaris
• Provides three entities for concurrency
– User threads
* Managed by a thread library
– Light weight processes (LWP)
* A unit of parallelism within a process. Thread library maps user threads into LWPs. Several LWPs may be created within a process
– Kernel threads
* A kernel thread is associated with each LWP. The kernel also
creates some kernel threads for its own use; e.g., a thread to handle
disk I/O
• Mapping between threads and LWPs influences
parallelism (see Hybrid models’ schematic)
Th d i S l i
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 57/61
Chapter 3:
Processes and Threads
Dhamdhere: Operating Systems—
A Concept-Based Approach, 2 ed
Slide No: 57
Copyright © 2008
Threads in Solaris
• Mapping between user threads and LWPs is performed by thread library• Each LWP has a KTCB; scheduling is performed by the kernel’s scheduler
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 58/61
Chapter 3:
Processes and Threads
Dhamdhere: Operating Systems—
A Concept-Based Approach, 2 ed
Slide No: 58
Copyright © 2008
Processes and threads in Linux
• Salient features
– Linux supports kernel-level threads
– Threads and processes are treated alike except at creation
* A thread shares the information about memory management,
current directory, open files and signal handlers of its parentprocess; a process does not share any information of its parent
– A thread or process contains information about
* Its parent
* Its deemed parent, to whom its termination should be reported
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 59/61
Chapter 3:
Processes and Threads
Dhamdhere: Operating Systems—
A Concept-Based Approach, 2 ed
Slide No: 59
Copyright © 2008
Processes and threads in Linux
• Process and thread states
– Task_running: scheduled or waiting to be scheduled
– Task_interruptible: sleeping on an event, but may receive a
signal
– Task_uninterruptible: sleeping and may not receive a signal – Task_stopped: operation has been stopped by a signal
– Task_zombie: operation completed, but its parent has not issued
a system call to check whether it has terminated
• Interruptibility simplifies implementation of signals
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 60/61
Chapter 3:
Processes and Threads
Dhamdhere: Operating Systems—
A Concept-Based Approach, 2 ed
Slide No: 60
Copyright © 2008
Processes and threads in Windows
• A process is a unit for resource allocation, and a thread is
a unit for concurrency. Hence each process must have at
least one thread in it
• Interesting thread states:
– Standby: Thread has been selected to run on a CPU
* Important in a multiprocessor computer system
– Transition: Kernel stack has been swapped out
8/3/2019 Dhamdhere OS2E Chapter 03 Power Point Slides 2
http://slidepdf.com/reader/full/dhamdhere-os2e-chapter-03-power-point-slides-2 61/61
Chapter 3: Dhamdhere: Operating Systems Slide No: 61
Thread state transitions in Windows 2000