Documente Academic
Documente Profesional
Documente Cultură
. 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.
What is a process?
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
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
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
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
Process interaction
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
OS view of a process
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
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
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)
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 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
Event handling
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
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
Process States
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
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
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
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
Event handling
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
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
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
Threads
Benefits of threads
Efficient communication
* Threads of a process can communicate through shared data space
Kinds of threads
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 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
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
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
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)
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
(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
Agents answer queries and perform bookings They share the reservations data
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
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 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
Three possible executions are shown Q: Which of them has a race condition?
(a) Initiation of Pj should be delayed until Pi performs si (b) After performing Sj-1, Pj should be delayed until Pi performs si
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
Signals
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
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
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
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
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
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 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