Unit-2 Operating System
Unit-2 Operating System
Dr.Srinath.S
UNIT : 2 Syllabus
Dr.Srinath.S
Chapter – 3
Process Concept
Dr.Srinath.S
Process – General view
• A process can be thought of as a program in execution.
• A process will need certain resources such as CPU time, memory, files and
I/O devices.
Dr.Srinath.S
Process Concept
• An operating system executes the program in different ways
• Batch system
• Time-shared systems
Dr.Srinath.S
The Process
Process includes:
• The process is more than the program code, which is sometimes known as the text section.
• It also includes the current activity, as represented by the value of the program counter.
• It includes the process Stack which contains temporary data
• Function parameters, return addresses, local variables
• Data section containing global variables
• Heap containing memory dynamically allocated during run time
Dynamic memory
Global variables
Program code
Dr.Srinath.S
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
Dr.Srinath.S
Diagram of Process State
Dr.Srinath.S
Process control Block (PCB)
Dr.Srinath.S
Process Control Block (PCB)
Information associated with each process
• Process state – may be new, ready, running, waiting, halted and so on.
• I/O status information – list of I/O devices required for the process
Dr.Srinath.S
Process Control Block (PCB)
Dr.Srinath.S
CPU Switch From Process to Process
Dr.Srinath.S
Process Scheduling
• Maximize CPU use, quickly switch processes onto CPU for time sharing
• Process scheduler selects among available processes for next execution on CPU
• Job queue – set of all processes in the system. As processes enter the system, they are put
into a job queue.
• Ready queue – set of all processes residing in main memory, ready and waiting to execute
(head of the ready queue contains the first and last process PCB address. And
each PCB has the address of the next PCB)
Dr.Srinath.S
Representation of Process Scheduling
Dr.Srinath.S
Schedulers
• Long-term scheduler (or job scheduler) – selects which processes
should be brought into the ready queue
Dr.Srinath.S
Schedulers (Cont.)
• Short-term scheduler is invoked very frequently (milliseconds)
(must be fast)
Dr.Srinath.S
Medium term scheduler
Dr.Srinath.S
Addition of Medium Term Scheduling
Dr.Srinath.S
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.
Dr.Srinath.S
Process Creation
• Parent process create children processes, which, in turn create
other processes, forming a tree of processes
• Resource sharing
• Parent and children share all resources
• Children share subset of parent’s resources
• Execution
• Parent and children execute concurrently
• Parent waits until children terminate
Dr.Srinath.S
A Tree of Processes on Solaris
Dr.Srinath.S
Process Creation (Cont.)
• There is also two possibility exists in terms of address space of new process
• Address space
• Child duplicate of parent
• Child has a program loaded into it
• UNIX examples
• fork system call creates new process
• exec system call used after a fork to replace the process’ memory space with a new
program
Dr.Srinath.S
Process Termination
• Process executes last statement and asks the operating system to
delete it (exit)
• Output data from child to parent (via wait)
• Process’ resources are de-allocated by operating system
Dr.Srinath.S
Inter-process Communication
• Processes within a system may be independent or cooperating
• Computation speedup – if we want a particular task to run faster, we must break it into subtasks, each of
which will be executing in parallel with others. This is possible only when multiple computing models like
CPU exists.
• Convenience – user may be working on many tasks at the same time like editing, printing and compiling
• Cooperating processes need inter-process communication (IPC), which will allow them to
exchange data and information.
Dr.Srinath.S
Inter-process Communication – Message Passing
• Processes that want to communicate must have a way to refer to each other.
They can use either direct or indirect communication.
Under direct communication: each process must name the recipient or sender:
Direct communication:
• IPC facility provides two operations:
• send(message) : send (P, message) : send the message to process P
• receive(message) : receive (Q, message) : receive a message from process Q
Dr.Srinath.S
Indirect Communication
• Operations
• create a new mailbox
• send and receive messages through mailbox
• destroy a mailbox after the communication
Dr.Srinath.S
Indirect Communication
• Mailbox sharing
• P1, P2, and P3 share mailbox A
• P1, sends; P2 and P3 receive
• Who gets the message?
• Solutions
• Allow a link to be associated with at most two processes
• Allow only one process at a time to execute a receive operation
• Allow the system to select arbitrarily the receiver. Sender is notified who
the receiver was.
Dr.Srinath.S
Synchronization
• Message passing may be either blocking or non-blocking
Dr.Srinath.S
Threads
Thread
• Advantage of thread
• we can do multiple task from a single process.
• Reduce context switching
• Improve CPU Utilization
Dr.Srinath.S
Difference between process vs. thread
The advantage of thread is, within a process, when one thread is blocked
for I/O another thread is selected for execution.
Dr.Srinath.S
Two types of threads
• User level thread : Managed by the user, Kernel will not know
about the existence of thread and will not manage the threads.
Dr.Srinath.S
Benefits
• Responsiveness
• Resource Sharing
• Economy
• Utilization of MP Architectures
Dr.Srinath.S
User Threads
• Examples in different OS
- POSIX Pthreads
- Mach C-threads
- Solaris threads
Dr.Srinath.S
Kernel Threads
• Examples
- Windows 95/98/NT/2000
- Solaris
- Tru64 UNIX
- BeOS
- Linux
Dr.Srinath.S
Multithreading Models
• Many-to-One
• One-to-One
• Many-to-Many
Dr.Srinath.S
Many-to-One
Dr.Srinath.S
Many-to-One Model
Dr.Srinath.S
One-to-One
• Examples
- Windows 95/98/NT/2000
- OS/2
Dr.Srinath.S
One-to-one Model
Dr.Srinath.S
Many-to-Many Model
Dr.Srinath.S
Many-to-Many Model
Dr.Srinath.S
Threading Issues
Dr.Srinath.S
Java Threads
Dr.Srinath.S
Java Thread States
Dr.Srinath.S
CPU Scheduling
Dr.Srinath.S
CPU Scheduling : Basic Concepts
Dr.Srinath.S
Alternating Sequence of CPU And I/O Bursts
Dr.Srinath.S
CPU Scheduler
Dr.Srinath.S
Dispatcher : Associated with Context Switch
• Dispatch latency – time it takes for the dispatcher to stop one process
and start another running.
Dr.Srinath.S
Scheduling Criteria
• CPU utilization – keep the CPU as busy as possible
• Throughput – # of processes that complete their execution per
time unit
• Turnaround time – amount of time to execute a particular
process
• Waiting time – amount of time a process has been waiting in
the ready queue
• Response time – amount of time it takes from when a request
was submitted until the first response is produced, not output
(for time-sharing environment)
Dr.Srinath.S
Optimization Criteria
• Max CPU utilization
• Max throughput
• Min turnaround time
• Min waiting time
• Min response time
Dr.Srinath.S
First-Come, First-Served (FCFS) Scheduling
0 24 27 30
Dr.Srinath.S
FCFS Scheduling (Cont.)
0 3 6 30
Dr.Srinath.S
Shortest-Job-First (Remaining) (SJR) Scheduling
• Associate with each process the length of its next CPU burst. Use these
lengths to schedule the process with the shortest time.
• Two schemes:
• Non preemptive – once CPU given to the process it cannot be preempted until
completes its CPU burst.
• preemptive – if a new process arrives with CPU burst length less than remaining
time of current executing process, preempt. This scheme is know as the
Shortest-Remaining-Time-First (SRTF).
• SJF is optimal – gives minimum average waiting time for a given set of
processes.
Dr.Srinath.S
Example of Non-Preemptive SJF
P1 P3 P2 P4
0 3 7 8 12 16
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
Dr.Srinath.S
Round Robin (RR)
• Each process gets a small unit of CPU time (time quantum), usually 10-
100 milliseconds. After this time has elapsed, the process is
preempted and added to the end of the ready queue.
• If there are n processes in the ready queue and the time quantum is q,
then each process gets 1/n of the CPU time in chunks of at most q time
units at once. No process waits more than (n-1)q time units.
• Performance
• q large FIFO
• q small q must be large with respect to context switch, otherwise overhead is
too high.
Dr.Srinath.S
Example of RR with Time Quantum = 20
Dr.Srinath.S
Turnaround Time Varies With The Time Quantum
Dr.Srinath.S
Multilevel Queue
Dr.Srinath.S
Multilevel Queue Scheduling
Dr.Srinath.S
Multilevel Feedback Queue
Dr.Srinath.S
Example of Multilevel Feedback Queue
• Three queues:
• Q0 – time quantum 8 milliseconds
• Q1 – time quantum 16 milliseconds
• Q2 – FCFS
• Scheduling
• A new job enters queue Q0 which is served FCFS. When it gains CPU, job receives
8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q1.
• At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still
does not complete, it is preempted and moved to queue Q2.
Dr.Srinath.S
Multilevel Feedback Queues
Dr.Srinath.S
Multiple-Processor Scheduling
Dr.Srinath.S