1/21/2020
Operating System
B.Tech
Processes
Delhi Technological University
Instructor: Divya Sethia
[email protected]
• Earlier computer: single program loaded with complete
control of system resources
• Current trend: loading multiple programs in memory
and executing concurrently – requires firmer control and
management
• Program in execution is process
• System is collection of processes – user process
executing user code and OS process executing system
code.
• All processes execute concurrently with CPU
multiplexed among them.
• CPU switches between processes - make computer
more productive
1
1/21/2020
Processes
• Process Concept
• Process Scheduling
• Operations on Processes
• Cooperating Processes
• Interprocess Communication
• Communication in Client-Server
Systems
1. Process Concept
• An operating system executes a variety of programs:
– Batch system – jobs
– Time-shared systems – user programs or tasks
• The terms job and process are used interchangeably
• Process – a program in execution; process execution must
progress in sequential fashion
• A process includes:
– Program code (text section)
– Current activity represented by program counter and
processor registers
– Stack ( temp data : function parameters, return
addresses and local variables)
– data section (global variables)
– Heap (memory dynamically allocated during process run
time)
2
1/21/2020
Process in Memory
Program vs Process
Program Process
Passive entity ( file containing list of Active entity : program counter with
instructions stored on media) – next instr to execute and set of
executable file resources
Becomes a process when loaded into
mem
Invocation: double clicking or entering
name on the cli (prog.ex or a.out)
Different users invoking same program : different process
existing for same program eg: different copies of browsers .
- Text sections are equivalent.
- Data, stack and heap vary
3
1/21/2020
Process State
• As a process executes, it changes state
– new: The process is being created
– running: Instructions are being executed
– waiting: The process is waiting for some event to occur
(I/O, reception of signal)
– ready: The process is waiting to be assigned to a
processor
– terminated: The process has finished execution
• State names may vary on diff OS and may be finer
• Only single process may be in running state on a
processor, several may be in ready or waiting state
Diagram of Process State
4
1/21/2020
Process Control Block (PCB) (Task
Control Block)
Information associated with each
process
• Process state
• Program counter
• CPU registers
• CPU scheduling information ( process
priority, pointer to scheduling queues)
• Memory-management information (base
and limit registers, page tables)
• Accounting information (amount of CPU
and real time used, time limits, account
numbers, process numbers)
• I/O status information ( list of I/O
devices allocated, open files etc)
CPU Switch From Process to Process
5
1/21/2020
2. Process Scheduling Queues
Multiprogramming: Process running all the
time to maximize CPU utilization
- Timesharing CPU among processes frequently
such that each user can interact with program
- Process scheduler selects available process
from set of available process
- Only one process running on processor at a
time
- Other processes if ready need to wait until
CPU is free and can be rescheduled.
2. Process Scheduling Queues
• Job queue – set of all processes in system
generated as process enters system
• Ready queue – set of all processes residing
in main memory, ready and waiting to
execute (stored as a linked list , ready queue
header contains pointer to first and final
PCBs in list)
6
1/21/2020
2. Process Scheduling Queues
• Device queues – set of processes waiting
for an I/O device : disk may be required by
multiple resources. Disk busy with I/O for one
process – other process need to wait for the
device) Each device has a device queue
which is list of processes waiting for I/O for
the device.
• Processes migrate among the various
queues
Ready Queue And Various I/O Device Queues
7
1/21/2020
Representation of Process Scheduling
Schedulers
•Scheduler selects processes from the queues to be scheduled
Batch system:
-More processes submitted than can be executed
-Processes spooled on a mass storage device for later execution
Long term scheduler: ( job scheduler) – selects process from
spool, which processes should be brought into ready queue.
- selects processes from spool and loads them into memory for
execution
Short term scheduler : selects processes in memory that are ready
and allocates CPU to one of them
Difference:?
Freq of short term scheduler is higher than long term scheduler.
Selects a new process for CPU frequently at least once in every 100
msec. Must be very fast.
Eg: 10 milsec to decide which process to execute for 100 milisec.
Then 10/(100+10) = 9 % used for only scheduling
8
1/21/2020
Scheduler
•Long term scheduler executes at much less frequency
•Separates creation of processes and control degree of
multiprogramming
• Average rate of process creation must be same as average
rate of departure
•May need to be invoked only when process leaves system
• Requires to take care
o I/O bound process: spends more time in I/O than
computations
o CPU bound process: spends more time in computations
than I/O
•Long term scheduler must select right process mix (if all
processes are I/O then ready queue will be empty most of the
time, if all are CPU bound then I/O queue will be empty with
most of the devices being under unused – system is unused)
•Hence good combinations of I/O and CPU bound processes is
required
•Systems lUNIX and Windows: only small term schedulers.
Addition of Medium Term Scheduling
Some OS may introduce additional intermediate level scheduler
•Medium-term scheduling – required to remove processes from
memory to reduce degree of multiprogramming
•Later process can be moved back to memory and execution be
continued
•Process is swapped in and out by medium scheduler
•Maybe required to improve the process mix or for freeing up
memory
9
1/21/2020
Schedulers (Cont.)
• Short-term scheduler is invoked very
frequently (milliseconds) (must be fast)
• Long-term scheduler is invoked very
infrequently (seconds, minutes) (may be
slow)
• The long-term scheduler controls the degree of
multiprogramming
• Processes can be described as either:
– I/O-bound process – spends more time doing I/O
than computations, many short CPU bursts
– CPU-bound process – spends more time doing
computations; few very long CPU bursts
Context Switch
CPU switches to another process
• system must save state of old process
• load saved state for new process (context: PCB of
process (CPU registers and process state, memory
management information)
Context-switch time is overhead since the system does
no useful work while switching
Speed dependent:
• no of registers to be copied, memory speed etc)
• hardware support – multiple register set one for each
process then simply change the pointer to the register set
for context switch.
• Address space of current process must be preserved as
the space for the next task is prepared for use.
10
1/21/2020
Operations on Processes
3.1 Process Creation
• Process can create several new processes via system call during
course of execution
• Creating process: Parent process New process: children process
• Parent process create children processes, which, in turn create
other processes, forming a tree of processes
• Process has a unique identifier ps -el
• Resource sharing
– Parent and children share all resources
– Children share subset of parent’s resources
– Parent and child share no resources
• Execution
– Parent and children execute concurrently
– Parent waits until children terminate
Process Creation (Cont.)
• Address space
– Child duplicate of parent
– 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
11
1/21/2020
Process Creation
• New process created by fork consists of
copy of address space of original process.
• Both processes continue execution after
fork
• Fork returns different value to the
processes (0 in the new child process)
• Exec used to replace process’s memory
space with a new program
Process Creation
12
1/21/2020
3.2 Process Termination
• Process executes last statement and asks the operating
system to delete it (exit)
– Output data from child to parent (via wait)
– Process’ resources are deal located by operating
system (virtual memory, open files, I/O buffers)
• Parent may terminate execution of children processes
(abort)
– Child has exceeded allocated resources
– Task assigned to child is no longer required
– If parent is exiting
Some operating system do not allow child to continue if
its parent terminates
– All children terminated - cascading termination
• Process termination: exit()
wait() parent waits for child to exit
If parent exits before all children are assigned new parent init
– required so that there is a parent which can collect child
status and execution statistics
13
1/21/2020
4. Cooperating Processes
• Independent process cannot affect or be
affected by the execution of another process
• Cooperating process can affect or be affected
by the execution of another process
• Advantages of process cooperation
– Information sharing
– Computation speed-up
– Modularity
– Convenience
IPC
• Two models:
• i) Shared mem
- region of mem shared between processes is
established and processes read and write data
to shared region
• Ii) Message passing
14
1/21/2020
Difference
Message Passing Shared Mem
Useful for small data
Easier to implement
Slow: require system calls involving kernel Allows max speed – since routine mem
intervention access
Shared Memory
• Shared mem resides in the address space of
the process creating shared mem segment
• Other processes must attach to it
• Typically OS prevents one process to access
other process address space not for shared
15
1/21/2020
Producer-Consumer Problem
• Paradigm for cooperating processes, producer process produces
information that is consumed by a consumer process
Eg: web server producing pages and browser consuming the web
pages
Shared buffer filled by the producer and emptied by the consumer in
a synchronized manner
Buffers:
– unbounded-buffer no limit on the size of the buffer (consumer
must wait for new items, producer can always produce new
items)
– bounded-buffer assumes that there is a fixed buffer
size(consumer must wait if buffer is empty, producer must wait
if buffer is full)
Bounded-Buffer – Shared-Memory Solution
• Shared data (circular array)
#define BUFFER_SIZE 10
Typedef struct {
...
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
In – next free position
Out – first full position
Empty buffer => when in = out
Full buffer => ((in+1)% BUFFER_SIZE) = out
• Solution is correct, but can only use BUFFER_SIZE-1
elements
16
1/21/2020
Producer
Item nextProduced
while (true) {
/* Produce an item */
while (((in = (in + 1) % BUFFER SIZE count) == out)
; /* do nothing -- no free buffers */
buffer[in] = nextProduced;
in = (in + 1) % BUFFER SIZE;
{
Consumer
while (true) {
while (in == out)
; // do nothing -- nothing to consume
// remove an item from the buffer
nextConsumed = buffer[out];
out = (out + 1) % BUFFER SIZE;
return nextConsumed;
}
17
1/21/2020
Interprocess Communication (IPC)
• Mechanism for processes to communicate and to synchronize their
actions
• Message system – processes communicate with each other without
resorting to shared variables
• Message communication between processes residing on diff
machines
• IPC facility provides two operations:
– send(message) – message size fixed or variable
– receive(message)
• If P and Q wish to communicate, they need to:
– establish a communication link between them
– exchange messages via send/receive
• Implementation of communication link
– physical (e.g., shared memory, hardware bus)
– logical (e.g., logical properties)
Direct Communication
• Processes must name each other explicitly:
– send (P, message) – send a message to process P
– receive(Q, message) – receive a message from
process Q
• Properties of communication link
– Links are established automatically
– A link is associated with exactly one pair of
communicating processes
– Between each pair there exists exactly one link
– The link may be unidirectional, but is usually bi-
directional
18
1/21/2020
Indirect Communication
• Messages are directed and received from mailboxes (also
referred to as ports)
– Each mailbox has a unique id
– Processes can communicate only if they share a mailbox
• Properties of communication link
– Link established only if processes share a common mailbox
– A link may be associated with many processes
– Each pair of processes may share several communication links
– Link may be unidirectional or bi-directional
Indirect Communication
• Operations
– create a new mailbox
– send and receive messages through mailbox
– destroy a mailbox
• Primitives are defined as:
send(A, message) – send a message to mailbox A
receive(A, message) – receive a message from mailbox A
19
1/21/2020
Indirect Communication
• Mailbox sharing
– P1, P2, and P3 share mailbox A
– P1, sends; P2 and P3 receive
– Who gets the message?
• Solutions
– Allow a link to be associated with at most two processes
– Allow only one process at a time to execute a receive
operation
– Allow the system to select arbitrarily the receiver. Sender is
notified who the receiver was.
Synchronization
• Message passing may be either blocking or non-
blocking
• Blocking is considered synchronous
– Blocking send has the sender block until the message is
received
– Blocking receive has the receiver block until a message is
available
• Non-blocking is considered asynchronous
– Non-blocking send has the sender send the message and
continue
– Non-blocking receive has the receiver receive a valid
message or null
20
1/21/2020
Buffering
• Messages exchanged reside in temp queues Queue of messages
attached to the link; implemented in one of three ways
1. Zero capacity – 0 messages
Sender must wait (hence blocked) for receiver (rendezvous)
2. Bounded capacity – finite length of n messages Sender must wait
if link full
3. Unbounded capacity – infinite length Sender never waits
Client-Server Communication
• Sockets
• Remote Procedure Calls
• Remote Method Invocation (Java)
21
1/21/2020
Sockets
• A socket is defined as an endpoint for
communication
• Concatenation of IP address and port
• The socket 161.25.19.8:1625 refers to port 1625 on
host 161.25.19.8
• Communication consists between a pair of sockets
Socket Communication
22
1/21/2020
Remote Procedure Calls
• Remote procedure call (RPC) abstracts procedure
calls between processes on networked systems.
• Stubs – client-side proxy for the actual procedure
on the server.
• The client-side stub locates the server and
marshalls the parameters.
• The server-side stub receives this message,
unpacks the marshalled parameters, and peforms
the procedure on the server.
Execution of RPC
23
1/21/2020
Remote Method Invocation
• Remote Method Invocation (RMI) is a Java
mechanism similar to RPCs.
• RMI allows a Java program on one machine to
invoke a method on a remote object.
THANKS
24