Operating System B.Tech Processes Delhi Technological University Instructor: Divya Sethia Divyashikha@dtu - Ac.in
Operating System B.Tech Processes Delhi Technological University Instructor: Divya Sethia Divyashikha@dtu - Ac.in
Operating System
B.Tech
Processes
Delhi Technological University
Instructor: Divya Sethia
[email protected]
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
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
4
1/21/2020
5
1/21/2020
6
1/21/2020
7
1/21/2020
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
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.
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
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
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)
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
In – next free position
Out – first full position
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
17
1/21/2020
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
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
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
Execution of RPC
23
1/21/2020
THANKS
24