Sie sind auf Seite 1von 26

Chapter II

process and process management


2.1 Process concepts
A question that arises in discussing operating systems involves what to call all
the CPU activities. A batch system executes jobs, whereas a time-shared
system has user’s programs, or tasks. Even on a single-user system such as
Microsoft Windows, a user may be able to run several programs at one
time. In many respects, all these activities are similar, so we call all of them
processes.
Process is a dynamic entity i.e. a program in execution.
Process exist in a limited span of time.
A process has object program, data, resources and the status of the
process on execution.
A process is not a closed system; there is a communication between
processes through a shared environment.
Programs vs process
Program Process
₋ Static entity ₋ Dynamic entity
₋ Exist in a single space and ₋ Exist in a limited span of
continue to exist. time.
₋ Programs contain instructions₋ Executes instruction
Types of process
There are two types of process
1. Sequential process:
the execution sequence in sequential fashion, i.e. at any point in time
at most one instruction is executed.
2. Concurrent process
a) True concurrency: two or more processes executing at the same
time; it implies a need for more than one processor.
b) Apparent concurrency: switching from one process to the other.
In both cases if a snapshot of the system is taken, several processes
will be found in partial execution.
Process Creation
There are four principal events which cause
process to be created
1. System initialization: when OS boots
– Some are foreground process that interact with
users and work for them.
– Others are background processes which have
some specific functions; e.g. process that check
emails.
– Background processes are called daemons.
2. Execution of a process creation system call by
a running process (fork in Unix and
creatProcess in windows)
3. A user request to create a new process.
4. Initiation of a batch job (in large mainframes)
Process Termination
Events which cause process termination
1. Normal exit (voluntary): a process has
finished execution.
2. Error exit (voluntary): a fatal error has
occurred and a process cannot continue; e.g.
cc abc.c but the file doesn’t exist.
3. Fatal error (involuntary): mostly due to
program bug; e.g. executing an illegal
instruction, referencing non existing memory,
division by zero, …
4. Killed by another process (involuntary): the
killer must have an authorization (kill in Unix
and terminateProcess in Windows)
The operating system is responsible for the
following activities in relation to process
management.
– Process creation and termination
– Process suspension and resumption
– Provision of mechanisms for
• Process synchronization
• Process communication
2.1.1. Process state and process state transition
As the process executes, it changes state.
A process may be in one of the following
states.

Figure: process in different state


– New State: The process being created.
– Running State: A process is said to be running if it
has the CPU, that is, process actually using the
CPU at that particular instant.
– Blocked (or waiting) State: A process is said to be
blocked if it is waiting for some event to happen
such as an I/O completion before it can proceed.
– A process is unable to run until some external
event happens.
– Ready State: A process is said to be ready if it use
a CPU if one were available. A ready state process
is runable but temporarily stopped running to let
another process run.
– Terminated state: The process has finished
execution.
The three main state described as follow

Operating system maintains lists of ready and waiting processes.

Ready List Waiting Lists


P1 P10
Ordered Unordered
P11 P9

P20 P4
.
.
.
.
.
.
Process state Transitions
There are four state transitions two of them
caused by the process scheduler.
Process scheduler is a part of OS that decides
which process should get the CPU next.
1. Ready Running
– The assignment of the CPU to the first process on
the ready list is called dispatching and it is
performed by a system entity called dispatcher.
Dispatch (process_name):Ready Running
2. Running Ready
– A process may be stopped by a clock interrupt if
it doesn’t relinquish (release) the CPU before the
allocated time expires.
Timeout (process_name):Running Ready
3. Running waiting
– During I/O a process voluntarily relinquishes the
CPU.
Wait(process_name): Running waiting
4. Waiting Ready
– When I/O finishes
Wakeup(process_name): waiting Ready
Implementation of Processes
The OS maintains a table (an array of
structures), called the Process Table or
Process Control Block.
It enables an interrupted process to resume
normally.
It is an area of memory containing all the
relevant information associated with each
process.
These may include
– Process state
– Program counter
– Stack pointer
– Value of CPU registers when the process is
suspended.
– CPU scheduling information such as priority
– The area of memory used by the process(memory
management information)
– Accounting information: amount of CPU time
used, time limits, process no, etc.
– I/O status information: list of I/O devices such as
tape derives allocated for the process, a list of
open files, etc.
– Pointer to the next process’s PCB
Pointer Process
State
Process number

Program counter

Register

Memory limits

List of open files

.
.
.
• The information is system dependent; e.g.,
there is an entry “parent process” in UNIX but
none in windows.
• All PCBs are linked to form a list, which is
accessed by a pointer in a central table.
• When a process changes state from ready to
running, its PCB is used by the OS to restart.
• The use of PCB will be shown later in Process
Scheduling.
2.2 Threads
A process has an address space (containing
program text and data) and a single thread of
control, as well as other resources such as
open files, child processes, accounting
information, etc.
The thread has a program counter, registers,
and a stack
Thread allows multiple executions to take
place in the same process environment, called
multithreading.
– (a) Three processes each with one thread. (b) One
process with three threads.
Each thread has a program counter, registers
stack, and state; but all threads of a process share
address space, global variables and other
resources such as open files, etc.
• The first column lists some items shared by all threads in a
process.
• The second one lists some items private to each thread.
• Threads are sometimes called light weight processes.
• Threads take turns in running
Thread Usage-Why do we need threads?

• E.g., a word processor has different parts;


parts for
– Interacting with the user
– Formatting the page as soon as the changes are
made
– Timed savings (for auto recovery)
– Spelling and grammar checking
1. Simplifying the programming model since
many activities are going on at once.
2. They are easier to create and destroy than
processes since they don't have any
resources attached to them
3. Performance improves by overlapping
activities if there is too much I/O
4. Real parallelism is possible if there are
multiple CPUs
Note: implementation details are beyond the
scope of the course (distributed systems).
2.3 Interprocess communication
• Processes frequently need to communicate
with other processes.
• Processes may share a memory area or a file
for communication
• There are three issues related to IPC
1. How can one process pass information to
another.
2. How can we make two or more processes don’t
interfere with each other when engaged in
critical activities; e.g., getting the last 1MB of
memory
3. Sequencing of events when dependency exist;
e.g., one process produces data and another
process consumes it.
– These issues are also applied to threads; the first
is easy for thread since they share a common
address space.
Race conditions
– Arises as a result of sharing some resources
– E.g. printer spooler
– When a process wants to print a file, it enters a
file name on a special spooler directory
– Another process, the printer daemon,
periodically checks to see if there are any files to
be printed, and if there are it prints them and
removes from the directory.
– Recall the daemon is a process running in the
back ground and started automatically when the
system is booted.
– Assume that the spooler directory has a large
number of slots, numbered 0,1,2,3,…,n, each
capable of holding a file name.
– There are two shared variables (shared by all
processes)
– out-points to the next file to be printed
– in-points to the next free slot in the directory
• Then the following may happen
– Process A reads in and stores the value 7 in a local
variable.
– A clock interrupt occurs and the CPU is switched
to B.
– B also reads in and stores the value 7 in a local
variable.
– It stores the name of its file in slot 7 and updates
in to be 8.
– A runs again; it runs its file name in slot 7, erasing
the file name that B wrote.
– It updates in to be 8.
• The printer daemon will not notice this; but B
will never receive any output
• Situation like this, where two or more process
are reading and writing some shared data and
the final result depends on who runs precisely
when are called race conditions.

Das könnte Ihnen auch gefallen