Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Operating Systems

Course Code: CS211


Instructor: Dr. Sarah Iqbal

Lecture # 5
Agenda for Today
 Process concept
 Process scheduling concepts
 Process creation and termination
 Process management in UNIX/Linux— system calls:
fork, exec, wait, exit
What is a process?
 Process – a program in execution; process execution must progress in
sequential fashion.
 A process consists of:
 Code (text) section: the executable code
 Data section: global variables
 Stack section: temporary data storage when invoking functions
 Heap section: memory that is dynamically allocated during program
run time
 Environment
 CPU state (program counter, etc.)
 Process control block (PCB)
 The status of the current activity of a process is represented by the value
of the program counter and the contents of the processor’s registers.
CPU and I/O Bound Processes
 Processes can be:
 I/O-bound process – spends more time doing I/O than
computations, many short CPU bursts.
I/O Burst CPU Burst I/O Burst CPU Burst

 CPU-bound process – spends more time doing


computations; few very long CPU bursts.
CPU Burst I/O CPU Burst I/O
Process States

 As a process executes, it changes state


 new: The process is being created.
 ready: The process is waiting to be assigned to a processor.
 running: Instructions are being executed.
 waiting: The process is waiting for some event to occur (such
as an I/O completion or reception of a signal).
 terminated: The process has finished execution.
Process States
Process Control Block (PCB)
Process information and attributes
 Process state: The state may be new, ready, running, waiting,
halted and so on.
 Program counter: The counter indicates the address of the next
instruction to be executed for this process.
 CPU registers: The registers vary in number and type, depending
on the computer architecture. They include accumulators, index
registers, stack pointers and general-purpose registers, plus any
condition code information. Along with the program counter, this
state information must be saved when an interrupt occurs, to
allow the process to be continued correctly afterwards.
 CPU Scheduling information: This information includes a process
priority, pointers to scheduling queues, and any other scheduling
parameters.
Process Control Block (PCB)

 Memory-management information: This information may include


information such as the value of the base and limit registers, the
page tables, or the segment tables, depending on the memory
system used by the operating system.
 Accounting information: This information includes the amount of
CPU and real time used, time limits, account numbers, job or
process numbers, and so on.
 I/O status information: The information includes the list of I/O
devices allocated to the process, a list of open files, and so on.
Process Control Block (PCB)
Threads
 A process is a program that performs a single thread of execution.
 For example, when a process is running a word-processor program, a single
thread of instructions is being executed. This single thread of control allows
the process to perform only one task at a time. Thus, the user cannot
simultaneously type in characters and run the spell checker.
 Most modern operating systems have extended the process concept to allow
a process to have multiple threads of execution and thus to perform more
than one task at a time. This feature is especially beneficial on multicore
systems, where multiple threads can run in parallel.
 A multithreaded word processor could, for example, assign one thread to
manage user input while another thread runs the spell checker.
 On systems that support threads, the PCB is expanded to include information
for each thread.
Process Scheduling
 The objective of multiprogramming is to have some process running
at all times so as to maximize CPU utilization.
 The objective of time sharing is to switch a CPU core among
processes so frequently that users can interact with each program
while it is running.
 To meet these objectives, the process scheduler selects an available
process for program execution on a core.
 Each CPU core can run one process at a time.
Process Scheduling
Process Scheduling Queues
 Job Queue: As processes enter the system, they are put into a job
queue. This queue consists of all processes in the system.
 Ready Queue: The processes that are residing in main memory and
are ready and waiting to execute are kept on a list called the ready
queue.
 Device queues – set of processes waiting for I/O devices.

 Process migration between the various queues.


Queues in a Computer System
CPU Scheduling
 A process migrates among the ready queue and various wait queues throughout
its lifetime.
 The role of the CPU scheduler is to select from among the processes that are in
the ready queue and allocate a CPU core to one of them.
 The CPU scheduler must select a new process for the CPU frequently.
 An I/O-bound process may execute for only a few milliseconds before waiting for
an I/O request.
 Although a CPU-bound process will require a CPU core for longer durations, the
scheduler is unlikely to grant the core to a process for an extended period. Instead,
it is likely designed to forcibly remove the CPU from a process and schedule
another process to run. Therefore, the CPU scheduler executes at least once every
100 milliseconds, although typically much more frequently.
Context Switch
 When CPU switches to another process, the
system must save the state (context) of the
‘current’ (old) process and load the saved state for
the new process.
 Context-switch time is overhead; the system does
no useful work while switching.
 Time dependent on hardware support; typically in
microseconds
CPU Switch From Process to Process
Schedulers
 The Long-term scheduler
 The Short-term scheduler

 Medium-term scheduler
Schedulers
A process migrates between the various scheduling queues throughout
its lifetime. The operating system must select, for scheduling purposes,
processes from these queues in some fashion. The appropriate
scheduler carries out this selection process.
 The Long-term scheduler (or job scheduler) selects which processes
should be brought into the ready queue, from the job pool that is the
list of all jobs in the system.
 The Short-term scheduler (or CPU scheduler) selects which process
should be executed next and allocates CPU.
Long Term Scheduler
Long-term scheduler (or job scheduler) – selects processes from
the job pool to be brought into the ready queue.
• Long-term scheduler is invoked very infrequently
• The long-term scheduler controls the degree of multiprogramming.
• More processes, smaller percentage of time each process is
executed
Short Term Scheduler
Short-term scheduler (or CPU scheduler) – selects which process should be
executed next and allocates it the CPU through the dispatcher.
• Short-term scheduler is invoked very frequently
• Invoked when following events occur
o CPU slice of the current process finishes
o Current process needs to wait for an event
o Clock interrupt
o I/O interrupt
o System call
o Signal
Medium Term Scheduler
 Also known as swapper
 Selects an in-memory process and swaps it out to
the disk temporarily
 Swapping decision is based on several factors
• Arrival of a higher priority process but no memory
available
• Poor mix of jobs
• Memory request of a process cannot be met
Addition of Medium Term Scheduling
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 a subset of parent’s resources.
 Parent and child share no resources.
 When a process creates a new process, two possibilities for execution
exist:
1.The parent continues to execute concurrently with its children.
2.The parent waits until some or all of its children have terminated.
Processes Tree on a UNIX System
Process Creation …
 There are also two address-space possibilities for the new process:
1. The child process is a duplicate of the parent process (it has
the same program and data as the parent).
2. The child process has a new program loaded into it.
 UNIX examples
 fork() system call creates a new process
 exec() system call used after a fork to replace the process’
memory image with a new executable.
Process Management in UNIX/Linux

 Important process-related UNIX/Linux system calls


 A new process is created by the fork() system call
 exec() system call to replace the process’s memory
space with a new program.
 The parent waits for the child process to complete
with the wait() system call.
 exit() system call.
Process Termination
A parent may terminate the execution of one of its children for a
variety of reasons, such as these:
 The child has exceeded its usage of some of the resources
that it has been allocated. (To determine whether this has
occurred, the parent must have a mechanism to inspect the
state of its children.)
 The task assigned to the child is no longer required.

 The parent is exiting, and the operating system does not allow
a child to continue if its parent terminates.
Reference & Reading Material
• Operating Systems Concepts,
Chapter 3 “Processes”

29

You might also like