Lecture3 Processes Threads
Lecture3 Processes Threads
06/10/2024
Processes
06/10/2024
Program, job, and process
Program, job, and process
Modern operating systems use three terms that refer to a set of instructions: program, job, and
process.
Program
A program is a nonactive set of instructions stored on disk (or tape). It may or may not become a job.
Job
A program becomes a job from the moment it is selected for execution until it has finished running
and becomes a program again. During this time a job may or may not be executed. It may be located
on disk waiting to be loaded to memory, or it may be loaded into memory and waiting for execution
by the CPU. It may be on disk or in memory waiting for an input/output event, or it may be in memory
while being executed by the CPU.
The program is a job in all of these situations. When a job has finished executing (either normally or
abnormally), it becomes a program and once again resides on the disk. The operating system no
longer governs the program.
06/10/2024
Process
What is a process?
• A program in execution.
• A process is the unit of work in most systems.
• It is a program that has started but has not finished.
• In other words, a process is a job that is being run in memory. It has been selected among other
waiting jobs and loaded into memory. A process may be executing or it may be waiting for
CPU time.
• As long as the job is in memory, it is a process.
• A program becomes a process when an executable file is loaded into memory.
• A process will need certain resources such as CPU time, memory, files, and I/O
devices to accomplish its task.
• These resources are allocated to the process either when it is created or while it is
executing.
06/10/2024
Process cont.
Loading Executable files is done via GUI mouse clicks and command line
entry of its name, etc.
One program can have several processes
Consider several users running different copies of the mail program, or the same user invoking
many copies of the web browser program.
A process can initiate a sub process, which is a called a child process (and the initiating
process is sometimes referred to as its parent ).
A child process is a replica of the parent process and shares some of its resources, but
cannot exist if the parent is terminated.
06/10/2024
Process states
As a process executes, it changes state. It may be in one of the following
states:
New : The process is just being put together/created.
Running: Instructions are being executed. This running process holds the
CPU.
Waiting: The process is waiting for some event to occur (such as an I/O
completion or reception of a signal)
06/10/2024
Process states cont.
06/10/2024
Process Control Block
Each process is represented in the operating system by a process
control block
(PCB) also called Task Controlling Block (TCB)
It is a data structure within the operating system containing the
information needed to manage a particular process
It can be said that the set of the PCBs defines the current state of
the operating system
The role of the PCBs is central in process management.
06/10/2024
The PCB components
06/10/2024
The PCB components
Program counter. The counter indicates the address of the next instruction
to be executed for this process.
CPU registers..
Along with the program counter, this state information must be saved when
an interrupt occurs, to allow the process to be continued correctly afterward.
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
06/10/2024
The PCB components cont.
Memory-management information. This information may include such items
as the value of the base and limit registers and the page tables, or segment
tables, depending on the memory system used by the operating system.
I/O status information. This information includes the list of I/O devices
allocated to the process, a list of open files, and so on
Accounting information – CPU used, clock time elapsed since start, time
limits
06/10/2024
CPU switching between processes
06/10/2024
Process Scheduling
The operating system is responsible for managing the scheduling
activities:
A uniprocessor system can have only one running process at a time.
The main memory cannot always accommodate all processes at run-
time.
The operating system will need to decide on which process to execute
next (CPU scheduling), and which processes will be brought to the
main memory (job scheduling)
06/10/2024
Process Scheduling Queues
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 queue – set of processes waiting for an I/O device
06/10/2024
CPU Schedulers
Long-term scheduler (or job scheduler) – selects which processes
should be brought into the ready queue
Short-term scheduler (or CPU scheduler) – selects which process
should be executed next and allocates CPU
Medium-term scheduler – in charge of swapping process in and out of
memory& reduce level of Multiprogramming (some OS introduce an
additional intermediate level of scheduling)
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
06/10/2024
CPU and I/O Bursts
CPU–I/O Burst Cycle
Process execution consists of a
06/10/2024
CPU-bound and I/O-bound Processes
06/10/2024
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.
A context switch is the computing process of storing and restoring the state
of a CPU (the context) such that multiple processes can share a single CPU
resource.
The context switch is an essential feature of a multitasking operating
system.
Context switches are usually computationally intensive and much of the
design of operating systems is to optimize the use of context switches.
06/10/2024
Process Creation
Principal events that cause process creation
System initialization
Execution of a process creation system call by a running process
User request to create a new process
Process creation refers to the creation of a new process by the operating
system. When a process is created, the operating system allocates the
necessary resources and sets up the environment for the process to
execute.
06/10/2024
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.
UNIX examples
fork system call creates new process
06/10/2024
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 deallocated by operating system
Parent may terminate execution of children processes
(abort)
Child has exceeded allocated resources
Task assigned to child is no longer required
If parent is exiting
Some operating system do not allow child to continue if its parent
terminates
All children terminated - cascading termination
06/10/2024
Cooperating Processes
Independent process cannot affect or be affected by the execution
of another process
Cooperating process can affect or be affected by the execution of
another process
06/10/2024
Advantages of process cooperation
06/10/2024
processes termination
When are processes terminated?
Normal exit (voluntary)
Error exit (voluntary)
Fatal error (involuntary), due to bugs
Killed by another process (involuntary)
06/10/2024
UNIX Process Control
UNIX provides a number of system calls for process control
including:
fork - used to create a new process
exec - to change the program a process is executing
exit - used by a process to terminate itself normally
abort - used by a process to terminate itself abnormally
kill - used by one process to kill or signal another
wait - to wait for termination of a child process
sleep - suspend execution for a specified time interval
getpid - get process id
getppid - get parent process id