Operating Systems-
Systems-1
1- نظم تشغيل
3- عال251
Operating System Concepts 1.1 Silberschatz, Galvin and Gagne ©2013
Chapter 3:
Process Management
Operating System Concepts Silberschatz, Galvin and Gagne ©2013
Process Concept
Process – a program in execution; process execution
must progress in sequential fashion
Program is passive entity stored on disk (executable
file), process is active
Program becomes process when executable file
loaded into memory
Execution of program started via:
GUI mouse clicks,
command line entry of its name,
etc
One program can be several processes
Consider multiple users executing the same program
Operating System Concepts 1.3 Silberschatz, Galvin and Gagne ©2013
Process Structure
A process is more than the program code, which is
sometimes known as the text section.
It also includes the current activity:
The value of the program counter
The contents of the processor's registers.
It also includes the process stack, which contains
temporary data (such as function parameters, return
addresses, and local variables)
It also includes the data section, which contains global
variables.
It may also include a heap, which is memory that is
dynamically allocated during process run time.
Operating System Concepts 1.4 Silberschatz, Galvin and Gagne ©2013
Process State
As a process executes, it changes state
new: The process is being created
running: Instructions are being executed
waiting: The process is waiting for some event to occur
ready: The process is waiting to be assigned to a processor
terminated: The process has finished execution
Diagram of Process State
Operating System Concepts 1.5 Silberschatz, Galvin and Gagne ©2013
Process Control Block (PCB)
Information associated with each process
(also called task control block)
Process state – running, waiting, etc
Program counter – location of instruction to
next execute
CPU registers – contents of all process-centric
registers
CPU scheduling information- priorities,
scheduling queue pointers
Memory-management information – memory
allocated to the process
Accounting information – CPU used, clock time
elapsed since start, time limits
I/O status information – I/O devices allocated
to process, list of open files
Operating System Concepts 1.6 Silberschatz, Galvin and Gagne ©2013
Threads
So far, process has a single thread of
execution
Consider having multiple program counters
per process
Multiple locations can execute at once
Multiple threads of control -> threads
Need storage for thread details, multiple
program counters in PCB
Operating System Concepts 1.7 Silberschatz, Galvin and Gagne ©2013
Overview
Most modern applications are multithreaded
Threads run within application
Multiple tasks with the application can be implemented
by separate threads
Update display
Fetch data
Spell checking
Answer a network request
Process creation is heavy-weight while thread creation
is light-weight
Can simplify code, increase efficiency
Kernels are generally multithreaded
Operating System Concepts 1.8 Silberschatz, Galvin and Gagne ©2013
Multithreading Models
One to One model
Many to One model
Many to Many model
Operating System Concepts 1.9 Silberschatz, Galvin and Gagne ©2013
One-to-One Model
Each user-level thread maps to a single kernel thread
Creating a user-level thread creates a kernel thread
More concurrency than many-to-one
Number of threads per process sometimes restricted due to overhead
Examples
Windows
Linux
Operating System Concepts 1.10 Silberschatz, Galvin and Gagne ©2013
Many-to-One Model
Many user-level threads mapped to single kernel thread
One thread blocking causes all to block
Multiple threads may not run in parallel on multicore system
because only one may be in kernel at a time
Few systems currently use this model
Examples:
Solaris Green Threads
GNU Portable Threads
Operating System Concepts 1.11 Silberschatz, Galvin and Gagne ©2013
Many-to-Many Model
Allows many user level threads to be mapped to many kernel
threads
Allows the operating system to create a sufficient number of kernel
threads
Solaris prior to version 9
Windows with the ThreadFiber package
Operating System Concepts 1.12 Silberschatz, Galvin and Gagne ©2013
Threading Issues
Semantics of fork() and exec() system
calls
Signal handling
Synchronous and asynchronous
Thread cancellation of target thread
Asynchronous or deferred
Thread-local storage
Scheduler Activations
Operating System Concepts 1.13 Silberschatz, Galvin and Gagne ©2013
Semantics of fork() and exec()
Does fork()duplicate only the
calling thread or all threads?
Some UNIX systems have two
versions of fork
exec() usually works as normal –
replace the running process including
all threads
Operating System Concepts 1.14 Silberschatz, Galvin and Gagne ©2013
Process Scheduling
Maximize CPU use
Quickly switch processes onto CPU for time sharing
Process “gives” up then CPU under two conditions:
I/O request
After N units of time have elapsed (need a timer)
Once a process gives up the CPU it is added to the
“ready queue”
Process scheduler selects among available
processes in the ready queue for next execution on
CPU
Operating System Concepts 1.15 Silberschatz, Galvin and Gagne ©2013
Scheduling Queues
OS Maintains scheduling queues of
processes
Job queue – set of all processes in the
system
Ready queue – set of all processes residing
in main memory, ready and waiting to
execute
Device queues – set of processes waiting for
an I/O device
Processes migrate among the various
queues
Operating System Concepts 1.16 Silberschatz, Galvin and Gagne ©2013
Representation of Process Scheduling
Queuing diagram represents queues, resources, flows
Operating System Concepts 1.17 Silberschatz, Galvin and Gagne ©2013
Schedulers
Short-term scheduler (or CPU scheduler) – selects which process
should be executed next and allocates a CPU
Sometimes the only scheduler in a system
Short-term scheduler is invoked frequently (milliseconds) (must
be fast)
Long-term scheduler (or job scheduler) – selects which processes
should be brought into the ready queue
Long-term scheduler is invoked infrequently (seconds, minutes)
(may be slow)
The long-term scheduler controls the degree of multiprogramming
Processes can be described as either:
I/O-bound process – spends more time doing I/O than
computations, many short CPU bursts
CPU-bound process – spends more time doing computations; few
very long CPU bursts
Long-term scheduler strives for good process mix
Operating System Concepts 1.18 Silberschatz, Galvin and Gagne ©2013
Context Switch
When CPU switches to another process, the
system must save the state of the old
process and load the saved state for the new
process via a context switch
Context of a process represented in the PCB
Context-switch time is pure overhead; the
system does no useful work while switching
The more complex the OS and the PCB
the longer the context switch
Time dependent on hardware support
Some hardware provides multiple sets of
registers per CPU multiple contexts
loaded at once
Operating System Concepts 1.19 Silberschatz, Galvin and Gagne ©2013
Operations on Processes
System must provide
mechanisms for:
process creation,
process termination,
and so on as detailed next
Operating System Concepts 1.20 Silberschatz, Galvin and Gagne ©2013
Process Creation
A process may create other processes.
Parent process create children processes, which, in turn
create other processes, forming a tree of processes
Generally, a process is identified and managed via a
process identifier (pid)
A Tree of Processes in UNIX
Operating System Concepts 1.21 Silberschatz, Galvin and Gagne ©2013
Process Creation (Cont.)
Resource sharing among parents and children
options
Parent and children share all resources
Children share subset of parent’s resources
Parent and child share no resources
Execution options
Parent and children execute concurrently
Parent waits until children terminate
Operating System Concepts 1.22 Silberschatz, Galvin and Gagne ©2013
Process Creation (Cont.)
Address space
A child is a duplicate of the parent address space.
A child loads a program into the address space.
UNIX examples
fork() system call creates new process
exec() system call used after a fork()replaces
the process’ memory space with a new program
Operating System Concepts 1.23 Silberschatz, Galvin and Gagne ©2013
Process Termination
A process terminates when it finishes executing its final statement
and it asks the operating system to delete it by using the exit()
system call.
At that point, the process may return a status value (typically an
integer) to its parent process (via the wait() system call.
All the resources of the process are deallocated by the operating
system.
A parent may terminate the execution of children processes using
the abort() system call. Some reasons for doing so:
Child has exceeded allocated resources
Task assigned to child is no longer required
The parent is exiting and the operating systems does not allow a
child to continue if its parent terminates
Operating System Concepts 1.24 Silberschatz, Galvin and Gagne ©2013
Process Termination (Cont.)
Some operating systems do not allow a child process to exists if its
parent has terminated. If a process terminates, then all its children
must also be terminated.
cascading termination. All children, grandchildren, etc. are
terminated.
The termination is initiated by the operating system.
The parent process may wait for termination of a child process by
using the wait()system call. The call returns status information
and the pid of the terminated process
pid = wait(&status);
If no parent waiting (did not invoke wait()) process is zombie
If parent terminated without invoking wait, process is orphan
Operating System Concepts 1.25 Silberschatz, Galvin and Gagne ©2013