2. Chapter 3 Outline
1. Process Concept
2. Thread Concept
3. Inter process Communication
2
3. 1. Process Concept
• A process can be thought of as a program in execution. A process will
need certain resources – such as CPU time, memory, files, and I/O
devices – to accompany its task. These resources are allocated to the
process either when it is created or while it is executing.
• A process is more than program code, which is sometimes known as
the text section. It also includes the current activity, as represented by
the value of the program counter and the contents of the processor’s
register.
• In addition, a process generally includes the process stack, which
contains temporary data (such as method parameters, the process
stack, which contains temporary data), and a data section, which
contains global variables.
3
4. Cont..
• Processes may be of two types:
– IO bound processes: spend more time doing IO than computations, have
many short CPU bursts. Word processors and text editors are good
examples of such processes.
– CPU bound processes: spend more time doing computations, few very
long CPU bursts.
Process States
• As a process executes, it changes states. The state of a process is defined in
part by the current activity of that process. Each process may be in either of
the following states, as shown in Figure:
– New: The process is being created.
– 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.
– Ready: The process is waiting to be assigned to a processor.
– Terminated: The process has finished execution.
4
6. Process Control Block
Each process is represented in the operating system by a process
control block (PCB) – also called a task control block, as shown
in Figure below. A PCB contains many pieces of information
associated with a specific process, including these:
• Process state: The state may be new, ready, running, waiting,
halted and so on.
• Process Number: It shows a unique number or unique ID of a
particular process.
• Program counter: The counter indicates the address of the next
instruction to be executed for this process.
• CPU registers: It tells us the registers being used by a particular
process. E.g. index registers, stack pointers and general purpose
registers and etc.
6
7. Process Control Block
• 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.
Figure
Process control block (PCB) 7
Process State
Process Number
Program Counter
Registers
Memory Limits
List of Open Files
…
8. Process Scheduling
• The objective of multiprogramming is to have some process running
all the time so as to maximize CPU utilization.
• The objective of time-sharing is to switch the CPU among
processors so frequently that users can interact with each program
while it is running.
• To meet these objectives, the process scheduler selects an available
process(possibly from a set of several available processes) for
program execution on the CPU.
– For a single-processor system, there will never be more than one running
process.
– If there are more processes, the rest will have to wait until the CPU is free
and can be rescheduled.
• Switching the CPU from one process to another requires saving of the
context of the current process and loading the state of the new process,
as shown in Figure below. This is called context switching.
8
10. 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.
10
11. • In systems designed for running only a single application, it may be
possible to have all the processes that will ever be needed be present
when the system comes up.
• In general-purpose systems some way is needed to create processes
as needed during operation.
• There are four principal events that cause processes to be created:
1. System initialization
2. Execution of a process creation system call by a running
process
3. A user request to create a new process
4. Initiation of a batch job.
Process creation
11
12. 1. System initialization:
When an operating system is booted, typically several
processes are created.
These processes can be:
Foreground processes : processes that interact with
(human) users and perform work for them.
Background processes: processes which are not
associated with particular users, but instead have some
specific function.
2. Execution of a process creation system call by a running process
Running process will issue system calls to create one or more
new processes to help it do its job.
Creating new processes is particularly useful when the work
to be done can easily be formulated in terms of several
related, but otherwise independent interacting processes.
Cont..
12
13. 3. A user request to create a new process.
In interactive systems, users can start a program by typing a
command or (double) clicking an icon.
Taking either of these actions starts a new process and runs
the selected program in it.
4. Initiation of a batch job.
users can submit batch jobs to the system (possibly
remotely).
When the operating system decides that it has the resources
to run another job, it creates a new process and runs the next
job from the input queue in it.
Cont..
13
14. After a process has been created, it starts running and does
whatever its job is.
However, nothing lasts forever, not even processes.
Sooner or later the new process will terminate, usually due to
one of the following conditions:
1. Normal exit (voluntary)
2. Error exit (voluntary)
3. Fatal error (involuntary)
4. Killed by another process (involuntary)
Process termination
14
15. 1. Normal exit (voluntary)
Most processes terminate because they have done their work.
E.g. When a compiler has compiled the program, it executes a system
call to tell the operating system that it is finished. This call is exit in
UNIX and ExitProcess in Windows
Screen-oriented programs also support voluntary termination.
E.g. Word processors, Internet browsers and similar programs always
have an icon or menu item that the user can click to tell the process to
remove any temporary files it has open and then terminate.
2. Error exit (voluntary)
The second reason for termination is an error caused by the process, often
due to a program bug.
Examples include executing an illegal instruction, referencing
nonexistent memory, or dividing by zero.
Cont..
15
16. 3. Fatal error (involuntary)
A process is terminate if it is discovers a fatal error.
For example, if a user types the command cc foo.c to
compile the program foo.c and no such file exists, the
compiler simply exits.
4. Killed by another process (involuntary)
The fourth reason a process might terminate is that the
process executes a system call telling the operating system to
kill some other process.
In UNIX this call is kill. The corresponding Win32 function
is Terminate Process.
Cont..
16
17. • A thread is a basic unit CPU utilization.
• It comprises: A thread ID, a program counter, a register set, and stack.
• It shares with other threads belonging to the same process its code
section, data section and other operating-system resources, such as
open files and signals.
• A traditional / heavyweight process has a single thread of control.
• If a process has a multiple threads of control it can perform more
than one task at a time.
17
2. Thread concept
19. • The benefits of multithreaded programming can be broken down in to
four major categories:
1. Responsiveness: multithreading is an interactive application may
allow a program to continue running even if part of it is blocked or is
performing a lengthy operation.
2. Recourse sharing: by default, threads share the memory and the
resources of the process to which they belong.
3. Economy: allocating memory and resources for process creation is
costly. Because threads share the resources of the process to which
they belong, it is more economical to create and context-switch
threads.
4. Utilization of multiprocessor architectures: The benefits of
multithreading can be greatly increased in a multiprocessor
architecture, where threads may be running in a parallel on different
processor. A single- threaded process can only run on one CPU, no
matter how many are available. Multithreading on multi –CPU
machine increases concurrency.
Cont..
19
20. • Processes frequently need to communicate with other processes. For
example, in a shell pipeline, the output of the first process must be passed to
the second process, and so on down the line.
• Thus there is a need for communication between processes, preferably in a
well-structured way not using interrupts.
• In the following sections we will look at some of the issues related to this
Inter process Communication or IPC. Very briefly, there are three issues
here.
– The first was alluded to above: how one process can pass information to another.
– The second has to do with making sure two or more processes do not get into
each other’s way when engaging in critical activities (suppose two processes each
try to grab the last 1 MB of memory).
– The third concerns proper sequencing when dependencies are present: if process
A produces data and process B prints them, B has to wait until A has produced
some data before starting to print. We will examine all three of these issues
starting in the next section.
3. Inter Process communication
20
21. Race condition: The situation where several processes access and
manipulate shared data concurrently and the final value of the shared
data depends upon which process finishes last.
The key to preventing trouble here and in many other situations involving
shared memory, shared files, and shared everything else is to find some
way to prohibit more than one process from reading and writing the shared
data at the same time.
To prevent race conditions, concurrent processes must coordinate or be
synchronized.
3.1 Race condition
21
23. • Race Condition Solution – Locking
Cont..
23
Read X
X=X+10
Read X
X=X+10
X
10
20
20
30
24. 3.2 Critical-Section
A critical section is a piece of code in which a process or thread accesses
a common shared resource.
– The important features of the system is that – ensure that when
one process is executing in its CS, no other process is allowed to
execute in its CS. i.e. no two processes are executed in their critical
sections at the same time.
When a process executes code that manipulates shared data (or resource),
we say that the process is in it’s Critical Section (for that shared data).
The execution of critical sections must be mutually exclusive: at any
time, only one process is allowed to execute in its critical section (even
with multiple processors).
24
26. Critical section to prevent a race condition
Multiprogramming allows logical parallelism, uses devices efficiently but we
lose correctness when there is a race condition.
So we forbid logical parallelism inside critical section so we lose some
parallelism but we regain correctness.
Critical section (Con’t…)
26
27. Solution to Critical-Section Problem
A solution to a critical –section problem must satisfy the following four
requirements.
1. No two processes may be simultaneously inside their critical regions.
2. No assumptions may be made about speed or the number of CPUs.
3. No process running outside its critical region may block other process
4. No process should have to wait forever to enter its critical region.
27
28. 3.3 Mutual Exclusion with busy waiting
1. Disabling interrupts:- here each process will disable all interrupts just after
entering its critical region and re-enable them just before leaving it.
With interrupts disabled, no clock interrupts can occur.
The CPU is only switched from process to process as a result of clock or other
interrupts, after all, and with interrupts turned off the CPU will not be switched
to another process.
Thus, once a process has disabled interrupts, it can examine and update the
shared memory without fear that any other process will intervene.
Process Pi:
repeat
disable interrupts
critical section
enable interrupts
remainder section
forever
28
29. Drawbacks of Disabling Interrupts:
1. If the user process did not turned off the interrupts, this could be
the end of the system.
2. If the system is a multiprocessor, with two or more CPUs,
disabling interrupts affects only the CPU that executed the disable
instruction.
The other ones will continue running and can access the shared
memory. That is, critical section is now atomic but not mutually
exclusive (interrupts are not disabled on other processors).
In general, disabling interrupts is often a useful technique within the
operating system itself but is not appropriate as a general mutual exclusion
mechanism for user processes
Mutual Exclusion with busy waiting(cont…)
29
30. 2. Lock Variables:- is a software solution which uses a single, shared
(lock)variable, initially 0.
When a process wants to enter its critical region, it first tests the lock.
If the lock is 0, the process sets it to 1 and enters the critical region.
If the lock is already 1, the process just waits until it becomes 0. Thus,
a 0 means that no process is in its critical region, and a 1 means that
some process is in its critical region.
Suppose that one process reads the lock and sees that it is 0. Before it
can set the lock to 1, another process is scheduled, runs, and sets the
lock to 1. When the first process runs again, it will also set the lock to
1, and two processes will be in their critical regions at the same time.
Mutual Exclusion with busy waiting(cont…)
30
Editor's Notes
#23:The two process have to run as a batch. i.e. One After Another.
In Process one X is Locked in a critical section i.e No other process can modify or read X