CH 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 45

0

Operating Systems
Lecture 3:
Process management

Dr. Essam Halim Houssein


Lecturer, Faculty of Computers and Informatics,
Benha University
2

Introduction

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 accomplish its
task. These resources are allocated to the process either
when it is created or while it is executing.
A process is the unit of work in most systems. Systems
consist of a collection of processes: Operating-system
processes execute system code, and user processes execute
user code. All these processes may execute concurrently.
3

The operating system is responsible for the

following activities in connection with process and

thread management: the creation and deletion of

both user and system processes; the scheduling of

processes, communication, and deadlock

handling for processes.


4

Process Concept
Early computer systems allowed only one program to

be executed at a time. This program had complete control

of the system and had access to all the system's resources.

In contrast, current day computer systems allow multiple

programs to be loaded into memory and executed

concurrently.
5

Even on a single-user system such as Microsoft Windows, a


user may be able to run several programs at one time: a word
processor, a Web browser and an e-mail package. And even if
the user can execute only one program at a time, the
operating system may need to support its own internal
programmed activities, such as memory management. In
many respects, all these activities are similar, so we call all of
them processes.
6

The Process
a process is a program in execution. A process is more
than the 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 registers. A process generally also
includes the process stack, which contains temporary data,
and a data section, which contains global variables. A process
may also include a heap. The structure of a process in
memory is shown in Figure 3.1.
7

Process in Memory
8

We emphasize that a program by itself is not a process; a


program is a passive entity, such as a file containing a list of
instructions stored on disk (often called an executable file),
whereas a process is an active entity, with a program
counter specifying the next instruction to execute and a set of
associated resources.

A program becomes a process when an


executable file is loaded into memory.
9

Process State
As a process executes, it changes state. The state of a process
is defined in part by the current activity of that process. Each
process may be in one of the following states:
New. The process is being created.
Running. Instructions are being executed.
Waiting. The process is waiting for some event to occur.
Ready. The process is waiting to be assigned to a
processor.
Terminated. The process ha finished execution.
10

Diagram of Process State


11

Process Control Block

Each process is represented in the operating system by a

process control block (PCB) –also called a task control

block. A PCB is shown in next Figure. It contains many

pieces of informat1on associated with a specific process,

including these:
12

Process Control Block (PCB)


13

Information associated with each process (also called task control


block)
• Process state – running, waiting, etc
• Program counter – location of instruction to next execute
• CPU registers – contents of all process-centric registers
• CPU scheduling information- priorities, scheduling queue pointers
• Memory-management information – memory allocated to the
process
• Accounting information – CPU used, clock time elapsed since start,
time limits
• I/O status information – I/O devices allocated to process, list of
open files
14

CPU Switch From Process to Process


15

Process scheduling …

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.


16

Scheduling Queues

As processes enter the system, they are put into a job

queue, which consists of all processes in the system. The

processes that are residing in main memory and are ready

and waiting to execute are kept on a list called the ready

queue.
17

This queue is generally stored as a linked list. A ready-

queue header contains pointers to the first and final PCBs

in the list. Each PCB includes a pointer field that points to

the next PCB in the ready queue.


18

• Maximize CPU use, quickly switch processes onto CPU for


time sharing
• Process scheduler selects among available processes for
next execution on CPU
• Maintains scheduling queues of processes
▫ 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 queues – set of processes waiting for an I/O device
19

Ready Queue And Various I/O Device Queues


20

Suppose the process makes an I/O request to a shared

device, such as a disk.

Since there are many processes in the system, the disk may

be busy with the I/O request of some other process. The

process therefore may have to wait for the disk. The list of

processes waiting for a particular I/O device is called a

device queue. Each device has its own device queue .


21

A new process is initially put in the ready queue. It

waits there until it is selected for execution, or is

dispatched. Once the process is allocated the CPU and is

executing, one of several events could occur:


22

1. The process could issue an I/O request and then be

placed in an I/O queue.

2. The process could create a new subprocess and

wait for the subprocess's termination.

3. The process could be removed forcibly from the

CPU, as a result of an interrupt, and be put back in the

ready queue.
23

In the first two cases, the process eventually switches from

the waiting state to the ready state and is then put back in the

ready queue. A process continues this cycle until it

terminates, at which time it is removed from all queues and

has its PCB and resources deallocated.


24

Scheduler

A process migrates among the various scheduling queues

throughout its lifetime. The operating system must select,

for scheduling purposes, processes from these queues in

some fashion. The selection process is carried out by the

appropriate scheduler.
25

The long-term scheduler, or job scheduler, selects

processes from this pool and loads them into memory

for execution. The short-term scheduler, or CPU

scheduler, selects from among the processes that are

ready to execute and allocates the CPU to one of them.


28

Addition of Medium Term Scheduling


 Medium-term scheduler can be added if degree of multiple programming
needs to decrease
 Remove process from memory, store on disk, bring back in from disk to
continue execution: swapping
29

Later, the process can be reintroduced into memory, and its

execution can be continued where it left off. This scheme is

called swapping. The process is swapped out, and is later

swapped in..
30

Context Switch
interrupts cause the operating system to change a CPU from

its current task and to run a kernel routine. Such operations

happen frequently on general-purpose systems. When an

interrupt occurs, the system needs to save the current

Context of the process running on the CPU so that it can

restore that context when its processing is done, essentially

suspending the process and then resuming it.


31

The context is represented in the PCB of the process; it

includes the value of the CPU registers, the process state, and

memory-management information. Generically, we perform a

State Save of the current state of the CPU, be it in kernel or

user mode, and then a State Restore to resume operations.


32

Switching the CPU to another process requires performing a

state save of the current process and a state restore of a

different process. This task is known as a Context

Switch.
33

Operations on processes

The processes in most systems can execute concurrently, and

they may be created and deleted dynamically. Thus, these

systems must provide a mechanism for process creation and

termination.
34

Process Creation

A process may create several new processes, via a create-

process system call, during the course of execution. The

creating process is called a parent process, and the

new processes are called the children of that

process. Each of these new processes may in turn create

other processes, forming a tree of processes.


35

Most operating systems (including UNIX and the Windows

family of operating systems) identify processes according to a

unique process identifier (or pid), which is typically an

integer number. Next Figure illustrates a typical process

tree for the Linux operating system, showing the name of

each process and its pid.


36

A Tree of Processes in Linux

init
pid = 1

login kthreadd sshd


pid = 8415 pid = 2 pid = 3028

bash khelper pdflush sshd


pid = 8416 pid = 6 pid = 200 pid = 3610

emacs tcsch
ps
pid = 9204 pid = 4005
pid = 9298
37

When a process creates a new process, two possibilities exist in

terms of execution:

1. The parent continues to execute concurrently with its children.

2. The parent waits until some or all of its children have terminated.

There are also two possibilities in terms of the address space of

the new process:

1. The child process is a duplicate of the parent process (it has the same

program and data as the parent).

2. The child process has a new program loaded into it.

Task (3): Java Program Forking Separate Process?


38

Process Termination
A process terminates when it finishes executing its final

statement and asks the operating system to delete it by using

the exit () system call At that point, the process may

return a status value (typically an integer) to its parent

process (via the wait() system call). All the resources of the

process-including physical and virtual memory, open files,

and I/O buffers-are deallocated by the operating system.


39

A parent may terminate the execution of one of its

children for a variety of reasons, such as these:


1. The child has exceeded its usage of some of the
resources that it has been allocated.
2. The task assigned to the child is no longer
required.
3. The parent is exiting, and the operating system does not
allow a child to continue if its parent terminates.
40

Interprocess Communication IPC


Processes executing concurrently in the operating system
may be either independent processes or cooperating
processes. A process is independent if it cannot affect or
be affected by the other processes executing in the system.
Any process that does not share data with any other process
is independent. A process is cooperating if it can affect or
be affected by the other processes executing in the system.
Clearly, any process that shares data with other processes is
a cooperating process.
41

Cooperating processes require an interprocess

communication (IPC) mechanism that will allow them to

exchange data and information. There are two fundamental

models of interprocess communication: (1) shared

memory and (2) message passing.


42

In the shared-memory model, a region of memory that is


shared by cooperating processes is established. Processes can
then exchange information by reading and writing data to the
shared region.
In the message passing model, communication takes
place by means of messages exchanged between the
cooperating processes.
43

Communications Models
(a) Message passing. (b) shared memory.
44

Shared-Memory Systems
Normally, the operating system tries to prevent one process
from accessing another process's memory. Shared memory
requires that two or more processes agree to remove this
restriction. They can then exchange information by
reading and writing data in the shared areas. The form of the
data and the location are determined by these processes and
are not under the operating system's control. The processes
are also responsible for ensuring that they are not writing
to the same location simultaneously.
45

Message-Passing Systems
Message passing provides a mechanism to allow processes to
communicate and to synchronize their actions without
sharing the same address space and is particularly
useful in a distributed environment, where the
communicating processes may reside on different computers
connected by a network. For example, a chat program
used on the World Wide Web could be designed so that chat
participants communicate with one another by exchanging
messages.
46

Communications in Client-Server Systems

• Sockets
• Remote Procedure Calls
• Pipes
• Remote Method Invocation
(Java)

Task (13): Communications in Client-Server Systems?


Any Questions?

You might also like