Sunteți pe pagina 1din 61

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

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

Chapter 3: Processes and Threads

Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed

Slide No: 26 Copyright 2008

Event handling actions of an OS

Q: Are any arrows missing?

Chapter 3: Processes and Threads

Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed

Slide No: 27 Copyright 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

Chapter 3: Processes and Threads

Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed

Slide No: 28 Copyright 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 process when it initiates an I/O operation for it

Chapter 3: Processes and Threads

Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed

Slide No: 29 Copyright 2008

PCB-ECB interrelationship

Process Pi starts an I/O operation The OS forms an ECB for the end of I/O operation event and mentions Pi as the process that will be affected by it
Chapter 3: Processes and Threads Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed Slide No: 30 Copyright 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?)

Chapter 3: Processes and Threads

Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed

Slide No: 31 Copyright 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

Chapter 3: Processes and Threads

Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed

Slide No: 32 Copyright 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

Chapter 3: Processes and Threads

Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed

Slide No: 33 Copyright 2008

Threads in Process Pi : (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
Chapter 3: Processes and Threads Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed Slide No: 34 Copyright 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

Chapter 3: Processes and Threads

Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed

Slide No: 35 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, whose routines are linked to become a part of the process code. The kernel 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
Chapter 3: Processes and Threads Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed Slide No: 36 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
Chapter 3: Processes and Threads Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed Slide No: 37 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
Chapter 3: Processes and Threads Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed Slide No: 38 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 Kernels scheduler uses PCBs to schedule a process
Chapter 3: Processes and Threads Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed Slide No: 39 Copyright 2008

Actions of the thread library (N, R, and B indicate running, ready and blocked states)

(a) (b) (c) (d)

Process Pi is ready; threads h1h3 are running, ready, and blocked, resp Process Pi is scheduled The thread library suspends thread h1 and schedules thread h2 Thread h2 initiates I/O on device d, hence Pi becomes blocked
Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed Slide No: 40 Copyright 2008

Chapter 3: Processes and Threads

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
Chapter 3: Processes and Threads Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed Slide No: 41 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
Chapter 3: Processes and Threads Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed Slide No: 42 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 to them

The OS performs message passing and provides facilities for the other three modes of interaction
Chapter 3: Processes and Threads Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed Slide No: 43 Copyright 2008

An example of data sharing: airline reservations

Agents answer queries and perform bookings They share the reservations data

Chapter 3: Processes and Threads

Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed

Slide No: 44 Copyright 2008

Race conditions in data sharing

Results of operations performed on shared data may be wrong if race conditions exist
Let fi(ds) and fj(ds) represent the value of ds after the operations If processes Pi and Pj perform operations Oi and Oj
* If Oi is performed before Oj, resulting value of d is fj(fi(ds)) * If Oj is performed before Oi, resulting value of d is fi(fj(ds)) * A race condition exists if the result is not one of the two

Q: Why do race conditions arise?

Chapter 3: Processes and Threads

Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed

Slide No: 45 Copyright 2008

Data sharing by processes of a reservation system

Process Pi performs actions S1, S2.1 Process Pj performs actions S1, S2.1, S2.2 The same seat is allocated to both the processes
Chapter 3: Processes and Threads Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed Slide No: 46 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

Chapter 3: Processes and Threads

Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed

Slide No: 47 Copyright 2008

Race conditions in the airline reservations system

Three possible executions are shown Q: Which of them has a race condition?

Chapter 3: Processes and Threads

Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed

Slide No: 48 Copyright 2008

Control synchronization between processes

(a) Initiation of Pj should be delayed until Pi performs si (b) After performing Sj-1, Pj should be delayed until Pi performs si

Chapter 3: Processes and Threads

Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed

Slide No: 49 Copyright 2008

Interprocess messages

In the send command, process Pi specifies the message to be sent to Pj In the receive command, process Pj specifies the memory area where it wishes to receive a message

Chapter 3: Processes and Threads

Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed

Slide No: 50 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

Chapter 3: Processes and Threads

Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed

Slide No: 51 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

Chapter 3: Processes and Threads

Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed

Slide No: 52 Copyright 2008

Signal handling

Firm arrow : Pointers in data structures Dashed arrows: Execution time actions

(a) Actions when process Pi makes an init_sig call to install a signal handler (b) Actions when signal sig1 is sent while Pi is executing instruction b1
Chapter 3: Processes and Threads Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed Slide No: 53 Copyright 2008

Processes in Unix

A process operates in two modesuser 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

Chapter 3: Processes and Threads

Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed

Slide No: 54 Copyright 2008

Process state transitions in Unix

I/O request: User running Kernel running Blocked End of time slice: User running Kernel running Ready
Chapter 3: Processes and Threads Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed Slide No: 55 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)
Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed Slide No: 56 Copyright 2008

Chapter 3: Processes and Threads

Threads in Solaris

Mapping between user threads and LWPs is performed by thread library Each LWP has a KTCB; scheduling is performed by the kernels scheduler
Chapter 3: Processes and Threads Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed Slide No: 57 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 parent process; 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

Chapter 3: Processes and Threads

Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed

Slide No: 58 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

Chapter 3: Processes and Threads

Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed

Slide No: 59 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

Chapter 3: Processes and Threads

Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed

Slide No: 60 Copyright 2008

Thread state transitions in Windows 2000

Chapter 3: Processes and Threads

Dhamdhere: Operating Systems A Concept-Based Approach, 2 ed

Slide No: 61 Copyright 2008

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