Os Chapter 2
Os Chapter 2
Prepared by
Pranali Dhamne
Process
3 Process ID : Unique identification for each of the process in the operating system.
5 Program Counter
Program Counter is a pointer to the address of the next instruction to be executed for this process.
6 CPU registers
Various CPU registers where process need to be stored for execution for running state.
Process priority and other scheduling information which is required to schedule the process.
PCB [Process Control Block] continue..
● Every process has its own process control block (PCB), i.e each process will have a
unique PCB.
This includes the information of page table, memory limits, Segment table depending
on memory used by the operating system.
9 Accounting information
This includes the amount of CPU used for process execution, time limits, execution ID
etc.
1 IO status information
0
This includes a list of I/O devices allocated to the process.
Process Control Block
• Process identification
– Identifiers
• Numeric identifiers that may be stored with the process
control block include
• Identifier of this process
• Identifier of the process that created this process (parent
process)
• User identifier
Process Control Block
• Processor State Information
– User-Visible Registers
• A user-visible register is one that may be referenced by
means of the machine language that the processor
executes.
• Typically, there are from 8 to 32 of these registers,
although some RISC implementations have over 100.
Process Control Block
• Processor State Information
– Control and Status Registers
These are a variety of processor registers that are employed to
control the operation of the processor. These include
• •Program counter: Contains the address of the next instruction to
be fetched
• •Condition codes: Result of the most recent arithmetic or logical
operation (e.g., sign, zero, carry, equal, overflow)
•Status information: Includes interrupt enabled/disabled flags,
execution mode
Process Control Block
• Processor State Information
– Stack Pointers
• Each process has one or more last-in-first-out
(LIFO) system stacks associated with it. A stack
is used to store parameters and calling
addresses for procedure and system calls. The
stack pointer points to the top of the stack.
Process Control Block
• Process Control Information
– Scheduling and State Information
This is information that is needed by the operating system to perform its
scheduling function. Typical items of information:
•Process state: defines the readiness of the process to be scheduled for
execution (e.g., running, ready, waiting, halted).
• •Priority: One or more fields may be used to describe the scheduling priority of the
process. In some systems, several values are required (e.g., default, current,
highest-allowable)
• •Scheduling-related information: This will depend on the scheduling algorithm
used. Examples are the amount of time that the process has been waiting and the
amount of time that the process executed the last time it was running.
Process Control Block
• Process Control Information
– Data Structuring
• A process may be linked to other process in a queue, ring, or
some other structure.
• For example, all processes in a waiting state for a particular
priority level may be linked in a queue.
• A process may exhibit a parent-child (creator-created)
relationship with another process.
• The process control block may contain pointers to other
processes to support these structures.
Process Control Block
• Process Control Information
– Interprocess Communication
• Various flags, signals, and messages may be associated with
communication between two independent processes. Some or all
of this information may be maintained in the process control block.
– Process Privileges
• Processes are granted privileges in terms of the memory that may
be accessed and the types of instructions that may be executed. In
addition, privileges may apply to the use of system utilities and
services.
Process Control Block
• Process Control Information
– Memory Management
• This section may include pointers to segment
and/or page tables that describe the virtual
memory assigned to this process.
– Resource Ownership and Utilization
• Resources controlled by the process may be
indicated, such as opened files. A history of
utilization of the processor or other resources
may also be included; this information may be
needed by the scheduler.
Difference between Process Vs Program
Process Program
The process is basically an instance of the computer A Program is basically a collection of instructions that
program that is being executed. mainly performs a specific task when executed by the
computer.
A Process requires resources such as memory, CPU, A Program is stored by hard-disk and does not require
Input-Output devices. any resources.
A process has a dynamic instance of code and data A Program has static code and static data.
Basically, a process is the running instance of the On the other hand, the program is the executable
code. code.
Two-State Process Model
• Process may be in one of two states
– Running
– Not-running
Not-Running Process in a Queue
Process Scheduling Components
• Quit an application
● Arrival Time: Time at which the process arrives in the ready queue.
● Completion Time: Time at which process completes its execution.
● Burst Time: Time required by a process for CPU execution.
● Turn Around Time: Time Difference between completion time and arrival time.
● Waiting Time(W.T): Time Difference between turn around time and burst time.
1. FCFS
2. SJF /SJN
3. SRTN
4. Priority
5. Round Robin
6. Multi level queue
First-Come, First-Served (FCFS) Scheduling
0 24 27 30
P0 0-0=0 P0 0 5
P1 5-1=4 P1 1 3
P2 8-2=6 P2 2 8
P3 16 - 3 = 13 P3 3 6
● Convoy effect as all the other processes wait for the one big
process to get off the CPU. This effect results in lower CPU and
device utilization than might be possible if the shorter processes
were allowed to go first.
Advantages of FCFS:
● Easy to implement
● First come, first serve method
Disadvantages of FCFS:
Advantages
○ SJF is used because it has the least average waiting time than the other CPU Scheduling Algorithms
○ SJF can be termed or can be called as long term CPU scheduling algorithm.
Disadvantages
○ Starvation is one of the negative traits Shortest Job First CPU Scheduling Algorithm exhibits.
○ Often, it becomes difficult to forecast how long the next CPU request will take
● Shortest Job Next/First (SJN/SJF)
● If Arrival time not given, consider all processes arrived at same time i.e. 0 ms
Example 2
Calculate ATT and AWT using SJF scheduling.
1 1 7
2 3 3
3 6 2
4 7 10
5 9 8
Turn Around Time Waiting Time
7 0
AWT = 10 7
ATT = 4 2
24 14
12 4
Example 3
SRTN (Shortest Remaining Time Next)
Process Arrival Burst
ID Time Time
P1 0 8
P2 1 4
P3 2 2
P4 3 1
P5 4 3
P6 5 2
Proc Arriv Burs Process ID Turn Around Waiting Response
ess al t Time Time Time
ID Time Tim
e P1 20 12 0
P1 0 8 P2 9 5 1
P3 2 0 2
P2 1 4
P4 2 1 4
P3 2 2
P5 9 6 10
P4 3 1
P6 2 0 5
P5 4 3
Gantt Chart
P6 5 2
GATE 2011
Q. Consider the following table of arrival time and burst time for three processes P0, P1
and P2.
C 6.33
D 7.33
Solution
Process ID Arrival Time Burst Time Completion Time Turn Around Time Waiting Time
1 0 9 13 13 4
2 1 4 5 4 0
3 2 9 22 20 11
P1 1 4 0
P2 2 3 0
P3 1 7 6
P4 3 4 11
P5 2 2 12
Round Robin
Thread
● Thread is the segment of a process which means a process can
have multiple threads and these multiple threads are contained
within a process.
● A thread is the subset of a process and is also known as the
lightweight process.
● A process can have more than one thread, and these threads
are managed independently by the scheduler.
● All the threads within one process are interrelated to each
other.
● Threads have some common information, such as data
segment, code segment, files, etc., that is shared to their
peer threads. But contains its own registers, stack, and counter.
● A thread has three states: Running, Ready, and Blocked.
S.NO Process Thread
3. It takes more time for creation. It takes less time for creation.
Multiprogramming holds the concepts We don’t need multi programs in action for multiple threads because
6.
of multi-process. a single process consists of multiple threads.
Process switching uses an interface in an Thread switching does not require calling an operating system and causes
9.
operating system. an interrupt to the kernel.
If one process is blocked then it will not If a user-level thread is blocked, then all other user-level threads are
10.
affect the execution of other processes blocked.
The process has its own Process Control Thread has Parents’ PCB, its own Thread Control Block, and Stack and
11.
Block, Stack, and Address Space. common Address space.
Since all threads of the same process share address space and other
Changes to the parent process do not
12. resources so any changes to the main thread may affect the behavior of
affect child processes.
the other threads of the process.
13. A system call is involved in it. No system call is involved, it is created using APIs.
User-level thread
4: Threads 65
THREAD Many-to-One
•
S
Many user-level threads
mapped to single kernel
thread.
• Examples
- Windows 95/98/NT/2000
- Linux
4: Threads 67
Threading Issues
THREAD
Semantics of fork() and exec() system calls
S fork() duplicate only the calling thread or all threads?
• Does
Thread cancellation
• Terminating a thread before it has finished
• Two general approaches:
• Asynchronous cancellation terminates the target thread
immediately
• Deferred cancellation allows the target thread to
periodically check if it should be cancelled
4: Threads 68
Threading Issues
THREAD
Signal handling
S
• Signals are used in UNIX systems to notify a process that a particular event has occurred
• A signal handler is used to process signals
1. Signal is generated by particular event
2. Signal is delivered to a process
3. Signal is handled
• Options:
• Deliver the signal to the thread to which the signal applies
• Deliver the signal to every thread in the process
• Deliver the signal to certain threads in the process
• Assign a specific thread to receive all signals for the process
Thread pools
• Create a number of threads in a pool where they await work
• Advantages:
• Usually slightly faster to service a request with an existing thread than create a new thread
• Allows the number of threads in the application(s) to be bound to the size of the pool
4: Threads 69
Threading Issues
THREAD
S
Thread specific data
• Allows each thread to have its own copy of data
• Useful when you do not have control over the thread creation process
(i.e., when using a thread pool)
Scheduler activations
• Many:Many models require communication to maintain the appropriate
number of kernel threads allocated to the application
• Scheduler activations provide upcalls - a communication mechanism
from the kernel to the thread library
• This communication allows an application to maintain the correct
number kernel threads
4: Threads 70
THREAD Various Implementations
PThreads
• ASPOSIX standard (IEEE 1003.1c) API for thread creation and synchronization
• API specifies behavior of the thread library, implementation is up to
development of the library
• Common in UNIX operating systems (Solaris, Linux, Mac OS X)
Windows Threads
• Implements the one-to-one mapping
• Each thread contains
• A thread id
• Register set
• Separate user and kernel stacks
• Private data storage area
• The register set, stacks, and private storage area are known as the context of
the threads
4: Threads 71
THREAD Various Implementations
Linux Threads
S refers to them as tasks rather than threads
• Linux
• Thread creation is done through clone() system call
• clone() allows a child task to share the address space of the parent task (process)
Java Threads
• Java threads may be created by:
• Extending Thread class
• Implementing the Runnable interface
• Java threads are managed by the JVM.
4: Threads 72