2.1 Processes

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Chapter 2

Processes and Threads

2.1 Processes
2.2 Threads
2.3 Scheduling
2.4 Interprocess
communication
2.1 Processes

2
What is a process?
A process is basically a program in
execution. 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. The following image
shows a simplified layout of a process
inside main memory.

Stack: The process Stack contains the temporary data such as


method/function parameters, return address and local variables.
Heap: This is dynamically allocated memory to a process during its run time.
Text: This includes the current activity represented by the value of Program
Counter and the contents of the processor's registers.
Data: This section contains the global and static variables.

3
The Process Model - 1

• Multiprogramming of four programs


• Conceptual model of 4 independent, sequential processes
• Only one program active at any instant

4
The Process Model - 2
• An operating system executes a variety of programs:
– Batch system – jobs
– Time-shared systems – user programs or tasks
• A process resources includes:
– Address space (text segment, data segment)
– CPU (virtual)
• program counter
In the simplest terms, a batch job is a
• registers
scheduled program that is assigned to run on
• stack a computer without further user interaction.
– Other resource Batch jobs are often queued up during
working hours, then executed during the
(open files, child processes…) evening or weekend when the computer is
idle.

5
Process Creation (1)
Principal events that cause process creation
1. System initialization (When an operating system is booted,
typically numerous processes are created. Some of these
are foreground processes).
2. Execution of a process-creation system call by running
process (Often a running process will issue system calls to
create one or more new processes to help it do its job).
3. User request to create a new process
4. Initiation of a batch job (users can submit batch jobs to the
system).

6
Process Creation (2)
• Address space
– Child duplicate of parent (When a process creates new process, a
duplicate of that process is created. This new process is called the child
and the process that created it is called the parent. The child process then
replaces the copy for the code the parent process created with the code the
child process is supposed to execute)
– 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

7
Process Creation (3) : Example

8
Process Termination

Conditions which terminate processes


1.Normal exit (voluntary)
2.Fatal error (voluntary)
3.Error exit (involuntary)
4.Killed by another process (involuntary)

9
Process Hierarchies

• Parent creates a child process, child processes


can create its own process
• Forms a hierarchy
– UNIX calls this a "process group"
• Windows has no concept of process hierarchy
– all processes are created equal

10
Process States (1)

• Possible process states


– running
– blocked
– ready
• Transitions between states shown

11
Process States (2)

• Lowest layer of process-structured OS


– handles interrupts, scheduling
• Above that layer are sequential processes
12
Processes
context switch

13
Implementation of Processes (1)
To implement the process model, the operating system maintains a table (an
array of structures), called the process table , with one entry per process.
(Some authors call these entries process control blocks .) This entry contains
important information about the process’ state, including its program counter,
stack pointer, memory allocation, ...
Process Life Cycle
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.

14
Implementation of Processes (2)
Process Control Block (PCB) - 1

A Process Control Block is a data


structure maintained by the Operating
System for every process. A PCB keeps
all the information needed to keep track
of a process. The architecture of a PCB
is completely dependent on Operating
System. Here is a simplified diagram of
a PCB.
PCB is mostly stored as per-process
kernel stack which is in the kernel space
and kernel has access to this which is
kept protected from any users.

15
Process Control Block (PCB) - 2
Process State: The current state of the process i.e., whether it is ready, running,
1
waiting, or whatever.
2 Process privileges: This is required to allow/disallow access to system resources.

3 Process ID: Unique identification for each of the process in the operating system.
4 Pointer: A pointer to parent process.
Program Counter: Program Counter is a pointer to the address of the next
5
instruction to be executed for this process.
CPU registers: Various CPU registers where process need to be stored for
6
execution for running state.
CPU Scheduling Information: Process priority and other scheduling information
7
which is required to schedule the process.
Memory management information: This includes the information of page table,
8
memory limits, Segment table depending on memory used by the operating system.
Accounting information: This includes the amount of CPU used for process
9
execution, time limits, execution ID etc.

10 IO status information: This includes a list of I/O devices allocated to the process.
Implementation of Processes (2)

The above figure shows some of the key fields in a typical system. The fields in
the first column relate to process management. The other two relate to memory
management and file management, respectively. It
Modeling Multiprogramming (1)
When multiprogramming is used, the CPU utilization can be improved.
Crudely put, if the average process computes only 20% of the time it is sitting in
memory, then with five processes in memory at once the CPU should be busy all
the time. This model is unrealistically optimistic, however, since it tacitly
assumes that all five processes will never be waiting for I/O at the same time.

Skeleton of what the lowest level of the operating system


does when an interrupt occurs.
Modeling Multiprogramming (2)
A better model is to look at CPU usage from a probabilistic viewpoint. Suppose that a process spends
a fraction p of its time waiting for I/O to complete. With n processes in memory at once, the probability
that all n processes are waiting for I/O (in which case the CPU will be idle) is p n. The CPU utilization is
then given by the formula CPU utilization = 1 − p n

The figure on the right


shows the CPU
utilization as a function
of n, which is called the
degree of
multiprogramming.

Skeleton of what the lowest level of the operating system


does when an interrupt occurs.

You might also like