2. Objectives
• At the end of this topic, students should be able to:
• Define a Process
• Describe a Process State
• Describe the Process Operations
• Explain a Process Control Block (PCB)
3. Process
• a process is an instance of a computer program that is being executed.
• It contains the program code and its current activity.
• The execution of a process must progress in a sequential fashion.
• A process is defined as an entity which represents the basic unit of work to
be implemented in the system.
• To put it in simple terms, we write our computer programs in a text file and
when we execute this program, it becomes a process which performs all
the tasks mentioned in the program.
• When a program is loaded into the memory and it becomes a process, it
can be divided into four sections ─ stack, heap, text and data.
6. Program
• A program is a piece of code which may be a single line or
millions of lines.
• A computer program is usually written by a computer
programmer in a programming language.
• For example, here is a simple program written in C
programming language:
7. Processes vs. Programs
• A process is different than a program
• Program: Static code and static data
• Process: Dynamic instance of code and data
• No one-to-one mapping between programs and processes
• Can have multiple processes of the same program: Example:
many users can run “ls” at the same time
• One program can invoke multiple processes: Example: “make”
runs many processes to accomplish its work
8. Process Life Cycle/ Process State
• When a process executes, it passes through different states.
• These stages may differ in different operating systems, and the names
of these states are also not standardized.
• In general, a process can have one of the following five states at a
time.
9. Processes can be any of the following states :
• Start/New - The process is at the initial stage of being created.
• Ready - The process is waiting to be assigned to a processor. Ready processes
are waiting to have the processor allocated to them by the operating system so
that they can run. Process may come into this state after Start state or while
running it by but interrupted by the scheduler to assign CPU to some other
process.
• Running - The CPU is working on this process's instructions. Once the process
has been assigned to a processor by the OS scheduler, the process state is set
to running and the processor executes its instructions.
• Waiting - Process moves into the waiting state if it needs to wait for a resource,
such as waiting for user input, or waiting for a file to become available.
• Termination/ Exit - Once the process finishes its execution or it is terminated
by the operating system, it is moved to the terminated state where it waits to
be removed from main memory. The process has completed.
11. Process State Transitions
• Following are six(6) possible transitions among above mentioned five (5)
states
• Transition 1 occurs when process discovers that it cannot continue. If
running process initiates an I/O operation before its allotted time expires,
the running process voluntarily relinquishes the CPU. This state transition is:
Block (process-name): Running → Blocked
• Transition 2 occurs when the scheduler decides that the running process has
run long enough and it is time to let another process have CPU time. This
state transition is:
Time-Run-Out (process-name): Running → Ready.
12. • Transition 3 occurs when all other processes have had their share and
it is time for the first process to run again This state transition is:
Dispatch (process-name): Ready → Running.
• Transition 4 occurs when the external event for which a process was
waiting (such as arrival of input) happens. This state transition is:
Wakeup (process-name): Blocked → Ready.
• Transition 5 occurs when the process is created. This state transition
is:
Admitted (process-name): New → Ready.
• Transition 6 occurs when the process has finished execution. This
state transition is:
Exit (process-name): Running → Terminated.
14. • An active process is normally in one of the five states in the diagram. The arrows
show how the process changes states.
• A process is running if the process is assigned to a CPU. A process is removed
from the running state by the scheduler if a process with a higher priority
becomes runnable. A process is also pre-empted if a process of equal priority is
runnable when the original process consumes its entire time slice.
• A process is runnable in memory if the process is in primary memory and ready
to run, but is not assigned to a CPU.
• A process is sleeping in memory if the process is in primary memory but is
waiting for a specific event before continuing execution. For example, a process
sleeps while waiting for an I/O operation to complete, for a locked resource to be
unlocked, or for a timer to expire. When the event occurs, a wakeup call is sent to
the process. If the reason for its sleep is gone, the process becomes runnable.
• When a process' address space has been written to secondary memory, and that
process is not waiting for a specific event, the process is runnable and swapped.
15. • If a process is waiting for a specific event and has had its whole address
space written to secondary memory, the process is sleeping and swapped.
• If a machine does not have enough primary memory to hold all its active
processes, that machine must page or swap some address space to
secondary memory.
• When the system is short of primary memory, the system writes individual
pages of some processes to secondary memory but leaves those processes
runnable. When a running process, accesses those pages, the process
sleeps while the pages are read back into primary memory.
• When the system encounters a more serious shortage of primary memory,
the system writes all the pages of some processes to secondary memory.
The system marks the pages that have been written to secondary memory
as swapped. Such processes can only be scheduled when the system
scheduler daemon selects these processes to be read back into memory.
16. Process Operations
Process Creation
• In general-purpose systems, some way is needed to create processes
as needed during operation.
• There are four principal events led to processes creation.
System initialization.
Execution of a process Creation System calls by a running process.
A user request to create a new process.
Initialization of a batch job.
17. • Background processes that stay in background sleeping but suddenly springing to life to
handle activity such as email, webpage, printing, and so on.
• Background processes are called daemons. This call creates an exact clone of the calling
process.
• A process may create a new process by some create process such as 'fork'.
• Creating process is called parent process and the created one is called the child
processes.
• Only one parent is needed to create a child process.
• Note that unlike plants and animals that use sexual representation, a process has only
one parent.
• This creation of process (processes) yields a hierarchical structure of processes.
• Notice that each child has only one parent but each parent may have many children.
• After the fork, the two processes, the parent and the child, have the same memory
image, the same environment strings and the same open files.
• After a process is created, both the parent and child have their own distinct address
space.
• If either process changes a word in its address space, the change is not visible to the
other process.
18. • Following are some reasons for creation of a process
User logs on.
User starts a program.
Operating systems creates process to provide service,
e.g., to manage printer.
Some program starts another process, e.g., Netscape
calls xv to display a picture.
19. Process Termination
• A process terminates when it finishes executing its last statement.
• Its resources are returned to the system, it is purged from any system lists or tables,
and its process control block (PCB) is erased i.e., the PCB's memory space is returned
to a free memory pool.
• The new process terminates the existing process, usually due to following reasons:
Normal Exist Most processes terminates because they have done their job. This call is
exist in UNIX.
Error Exist When process discovers a fatal error. For example, a user tries to compile a
program that does not exist.
Fatal Error An error caused by process due to a bug in program for example, executing
an illegal instruction, referring non-existing memory or dividing by zero.
Killed by another Process A process executes a system call telling the Operating
Systems to terminate some other process. In UNIX, this call is kill. In some systems
when a process kills all processes it created are killed as well (UNIX does not work this
way).
20. Process Control Block
• A Process Control Block (PCB) is a data structure maintained by the
Operating System for every process.
• The PCB is identified by an integer process ID (PID).
• A PCB keeps all the information needed to keep track of a process as
listed below in the table:
24. PCB ctd’
• The architecture of a PCB is completely dependent on Operating
System and may contain different information in different operating
systems.
• The PCB is maintained for a process throughout its lifetime, and is
deleted once the process terminates.
25. Context Switching
• Process Context = machine environment
during the time the process is actively
using the CPU.
• i.e. context includes program counter,
general purpose registers, processor status
register (with C,N,V and Z flags), . . .
• To switch between processes, the OS
must:
a) save the context of the currently
executing process (if any), and
b) restore the context of that being
resumed.
• Time taken depends on h/w support.
26. Idle system
• What do we do if there is no ready process?
halt processor (until interrupt arrives)
saves power (and heat!)
increases processor lifetime
might take too long to stop and start.
• busy wait in scheduler
quick response time
ugly, useless
• invent idle process, always available to run
gives uniform structure
could use it to run checks
uses some memory
can slow interrupt response
• In general there is a trade-off between responsiveness and usefulness.