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

Operating Systems

Lecture 3 - Process Management /1

Ralph Tambala

MUST . CSIT

Lecture 3 1 / 21
Outline

1 What is a Process?

2 Context Switch

3 Process States

4 Process Creation

5 Process Termination

6 Interrupt Handling

7 Additional Resources/Info

Lecture 3 2 / 21
What is a Process?

What is a Process?

A process is a program in execution.

A process contains code, data,


and stack. Usually, it has its
own address space
Program is a passive entity
stored on disk (executable file),
Process is an active entity; A
program becomes a process
when executable file is loaded
into memory.
Only one process can be running
in the CPU at any given time! Figure 1: Process in memory

Lecture 3 3 / 21
What is a Process?

Process Attributes

Program counter – A pointer to the address of the next instruction to


be executed for this process.
Stack – An area of computer memory that contains the temporary
data such as method/function parameters, return address and local
variables.
Data – This is a section that contains global variables
Heap – It is the memory dynamically allocated to a process during its
run time

Lecture 3 4 / 21
What is a Process?

Process Control Block

The process control block (PCB) is a data


structure that contains all data (process
attributes) for a process to be used for
controlling its execution.
PCB also contains the data associated with the
resources allocatted to the process.
The following are the attributes of a PCB:
◦ Process Id (PID) – It is a number that
identifies each process of the system uniquely.
Each process is assigned a PID during its
creation.
◦ Process counter (PC) – Stores the address of
the next instruction to be executed.
◦ Process state – Processes goes through Figure 2: PCB
different states during their lifetime. Process
state specifies the current state of the process.
Lecture 3 5 / 21
What is a Process?

Process Control Block (2)


◦ Priority – Specifies how urgent is to execute the process. Process with
the highest priority is allocated the CPU first among all the processes.
◦ General-purpose registers – These are used to hold the data of
process generated during its execution.
◦ Memory limits – It is the data needed by the memory manager like
base register, limit register, page table for the process etc.
◦ List of opened files – Maintains a list of files that are being opened by
a process to make sure that no other process can open the file at the
same time.
◦ List of I/O devices – Maintains a list of I/O devices being used by the
process.
The OS keeps a process table. It is an array of PCBs, it contains all PCBs
of the current processes in the system. The PCB is maintained for a
process throughout its lifetime and it is deleted once the process
terminates.
Lecture 3 6 / 21
What is a Process?

Processes and Scheduling


Long term scheduler is also known as a job scheduler. This scheduler
regulates the program and select process from the queue and loads
them into memory for execution.
Short term scheduling is also known as CPU/process scheduler. This
helps in selecting from a group of processes that are ready to execute
and allocate CPU to one of them.
Processes have isolation from each other, i.e. different address space,
security context (e.g. PID) and termination protection (may
terminate either voluntarily or involuntarily).
Processes are scheduled separately from each other.
One process blocking or being pre-empted allows another to run.
On some systems, a process can be composed of several threads
which share the process.
On a multiprocessor, processes can run simultaneously.
Lecture 3 7 / 21
What is a Process?

CPU Bursts (1)


To schedule CPU, process scheduler uses common trait among most
computer programs, they alternate between CPU cycles and I/O cycles.

Figure 3: Listing showing CPU cycles and I/O cycles

Lecture 3 8 / 21
What is a Process?

CPU Bursts (2)

Bursts of CPU usage alternate with periods of I/O wait


Some processes are CPU-bound – These processes do not make many
I/O requests
Other processes are I/O-bound – These are processes with a large
number of I/O operations, which slow the CPU.

Lecture 3 9 / 21
Context Switch

Context Switch (1)

Hardware reality – One CPU runs the fetch-execute algorithm


OS abstraction – Each process has one CPU, running the
fetch-execute algorithm for the process.
What does the OS have to do?

OS must do as in the diagram above to keep programs execute within


its context. This is called context switching.

Lecture 3 10 / 21
Context Switch

Context Switch (2)

During a context switch, the operating system performs the following


operations:
1 Save process context, program counter and CPU registers
2 Update the PCB with the new state and any accounting information
3 Move PCB to appropriate queue - ready, blocked
4 Select another process for execution
5 Update the PCB of the process selected
6 Update memory-management data structures
7 Restore context of selected process

Lecture 3 11 / 21
Context Switch

Context Switch (3)


In summary, the OS saves the state of the old process and loads the saved
state for the new process. Context switch time is overhead; the system
does no useful work while switching.

Figure 4: CPU switch from process to process


Lecture 3 12 / 21
Process States

Process States (1)


As a process executes, it changes states
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 process
terminated: The process has finished execution

Figure 5: Process states

Lecture 3 13 / 21
Process States

Process States (2)

The following are transitions between


states:
1 Process enters ready queue
2 Scheduler picks this process
3 Scheduler picks a different
process
4 Process waits for event (such as
I/O)
5 I/O or event occurs
6 Process exits
7 Process ended by another
process Figure 6: Process transitions

Lecture 3 14 / 21
Process Creation

Process Creation (1)


Processes can be created in two ways
◦ System initialization: one or more processes created when the OS
starts up
◦ Execution of a process creation system call: something explicitly asks
for a new process
System calls can come from
◦ User request to create a new process (system call executed from user
shell)
◦ Already running processes which could be user programs or system
daemons
Parent process creates children processes, which, in turn create other
processes, forming a tree of processes
Parent and child processes have resource sharing options:
◦ Parent and children share all resources
◦ Children share subset of parent’s resources
◦ Parent and child share no resources

Lecture 3 15 / 21
Process Creation

Process Creation (2)


UNIX example
fork() system call creates a new process that is a clone of its parent.
exec() system call used after a fork to replace the process’ memory
space with a new program
wait() – wait for termination of a child. Either blocks, gets child
return-code, or exit code (if no children).
exit(); – Exit, destroying the process.

Figure 7: Process creation

Lecture 3 16 / 21
Process Termination

Process Termination (1)


Processes can be terminated voluntary or involuntary.
Process can terminate voluntary when it is a normal exit (process
finished executing) and also as an error exit.
Process can terminate involuntary due to a fatal error and sometimes
a process may be killed by another process.
Some instances that may lead to process termination include:
◦ Time limit exceeded
◦ Memory unavailable
◦ Protection error (e.g. request to write a read-only file)
◦ Arithmetic error (e.g. a zero division)
◦ I/O failure
◦ Operating system intervention
Process executes last statement and then asks the operating system
to delete it using the exit() system call.
◦ Returns status data from child to parent (via wait())
◦ Process’ resources are deallocated by operating system
Lecture 3 17 / 21
Process Termination

Process Termination (2)


Some OS do not allow a child to exist if its parent has terminated. If
a process terminates, then all its children (grandchildren, great
grandchildren, etc) must also be terminated. This is called cascading
termination.
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 a zombie
◦ If parent terminated without invoking wait, process is an orphan
Parent may terminate the execution of children processes using the
abort() system call. The following are some reasons why a parent
may kill its child process:
◦ Child has exceeded allocated resources
◦ Task assigned to child is no longer required
◦ The parent is exiting (cascading termination)
Lecture 3 18 / 21
Interrupt Handling

What is an Interrupt
Interrupts are signals sent to the CPU by external devices or from a
program within the computer that requires the operating system to stop
and figure out what to do next.
Interrupts are important because they give the user better control over the
computer. Without interrupts, a user may have to wait for a given
application to have a higher priority over the CPU to be ran.
Various types of interrupts include:
I/O interrupt – result when a READ or WRITE command is issued
Internal interrupt (synchronous interrupt) – result from arithmetic
operation or job instruction
Illegal arithmetic operation interrupt – dividing by zero; bad
floating-point operation generating an overflow or underflow
Illegal job instruction interrupt – protected or non-existent storage
access attempt; attempts to make system changes

Lecture 3 19 / 21
Interrupt Handling

Interrupt Handling

An interrupt handler, also known as an interrupt service routine


(ISR), is a control program that handles interruption event sequence.
Interrupt handler sequence is as follows:
1 Interrupt type described and stored (passed on as an error message to
the user)
2 Interrupted process state saved (including value of the program counter
and contents of all registers)
3 Interrupt is processed (job processing is terminated if non-recoverable
interrupt occurs, resources allocated to the job are released and an
error message is sent to the user; for an I/O interrupt (recoverable),
the job is moved to the I/O queue; for a time quantum interrupt
(recoverable), the job is moved to the READY queue).
4 Processor resumes normal operation

Lecture 3 20 / 21
Additional Resources/Info

Additional Info/Tasks
1 In Figure 6, which transitions are facilitated by short term scheduler
and which ones are facilitated by long term scheduler?
2 Can a process:
◦ block itself?
◦ run itself?
◦ Check out this hands-on Process Creation and Termination tutorial in
C.

Lecture 3 21 / 21

You might also like