Title: Chapter 4: Processes 9/16/03
1Chapter 4 Processes9/16/03
- Process Concept
- Process Scheduling
- Operations on Processes
- Cooperating Processes
- Interprocess Communication
- Communication in Client-Server Systems
- NOTE Instructor annotations in BLUE
2Process Concept
- An operating system executes a variety of
programs - Batch system jobs
- Time-shared systems user programs or tasks
- Textbook uses the terms job and process almost
interchangeably. - Process a program in some state of execution
process execution must progress in sequential
fashion. - A process includes
- program counter
- stack
- data section
- Text section (code)
3Process 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. - ready The process is waiting to be assigned to
a processor not waiting for anything or
resource. - terminated The process has finished execution.
4Diagram of Process State
5Queuing representation of Process States
A queue for each event is faster to search gt
From Stallings, Operating Systems, 4th ed. fig
3.8b, p. 121
6The Suspended State
- Suspended State When a process which has been
completely removed from memory and now is
residing on the disk as a process image which can
be reloaded into memory swapping. - Non-virtual memory systems This state is always
needed in a system which does not employ virtual
memory uses process swapping. - Process is either fully in memory or fully
swapped out to disk. - Needed if most processes in memory are blocked
(say for I/O) and. - By swapping out to the suspended state, memory is
freed for a new or runnable processes. - CPU utilization maximized.
- Virtual memory systems process is only partially
in memory. - May still be a need for swapping and the
suspended state. - If too many processes are resident in memory
(even partially), performance is impacted
thrashing low CPU utilization - Thus swapping out a process to the disk(Suspended
state) will improve performance.
7Suspended States
Most general state diagram
From Stallings, Operating Systems, 4th ed. fig
3.8b, p. 121
8Transitions involving suspended states
- Blocked -gt Blocked/Suspend all processes
waiting, no ready processes, blocked process is
swapped out to make room for unblocked processes - Blocked/Suspend -gtReady/Suspend event waited
for occurs, but still remains in suspended state
maybe memory space unavailable. - Ready/Suspend -gt Ready all processes waiting,
swap in a ready/ suspended processes. - Ready-gt Ready/Suspend Rare transition, if
running process is extremely large to free up
memory, or due to priorities. - New-gt Ready/Suspend Maintain a pool of
non-blocked processes on disk overhead of
building process done in advance - Blocked/Suspend -gtBlocked System anticipates
that waited event will occur soon move blocked
process into memory. - Running -gt Ready/Suspend Preempt a running
process due to a high priority blocked process
becoming unblocked
9Process Control Block (PCB)
- Information associated with each process -
uniquely - identifies and characterizes a particular
process. - Some information in the PCB
- Process state running, waiting, ready, etc.
- Program counter
- CPU register values
- CPU scheduling information - used by short term
scheduler - see later - Memory-management information base, limit
registers, page table info, etc. - Accounting information
- I/O status file information devices allocated
to proc., open files, etc.
10Process Control Block (PCB)
11CPU Switch From Process to Process
(context switch)
Process state saved in PCB
12Process Scheduling Queues
- 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. - Process migration between the various queues.
Elements entered in these queues are PCBs
linked list
13Ready Queue And Various I/O Device Queues
14Representation of Process Scheduling
15Schedulers
- Long-term scheduler (or job scheduler) selects
which processes should be brought into the ready
queue.Schedule new jobs to become a process
(from disk) - Short-term scheduler (or CPU scheduler) selects
which process should be executed next and
allocates CPU. - Medium term scheduler - remove processes,
especially suspended, from memory and move to
disk. Later bring process back into memory - Swapping - controls degree of Multiprogramming,
and memory utilization
16Addition of Medium Term Scheduling
17Schedulers (Cont.)
- Short-term scheduler is invoked very frequently
(milliseconds) ? (must be fast) a potential
performance bottleneck). - Long-term scheduler is invoked very infrequently
(seconds, minutes) ? (may be slow). - The long-term medium term schedulers controls
the degree of multiprogramming how many
programs to have in memory at one time. - Has implications on controlling thrashing in a
Virtual memory system - 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.
18Context Switch
- When CPU switches to another process, the system
must save the state of the old process and load
the saved state for the new process. - Context-switch time is overhead the system does
no useful work while switching an area needing
optimization. - Time dependent on hardware support.
19Process Creation
- Parent process create children processes, which,
in turn create other processes, forming a tree of
processes.Example UNIX fork/spawning mechanism. - Resource sharing
- Parent and children share all resources.
- Children share subset of parents resources.
- Parent and child share no resources.
- Execution
- Parent and children execute concurrently.
- Parent waits until children terminate - wait is a
system call.
20Process 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. - fork and execve are both system calls
- Except for 3 special processes that are part of
the kernel scheduler, init, and pagedaemon (PIDs
0, 1, 2), all processes are create from a fork
call. - All other processes have fork ancestries which
start with the init system process - A typical user program run from the command line
would be forked from the shell process. - If the parent quits before waiting for a child to
quit, the orphan child is adopted by init and
becomes its child. - If child quits before the parent waits for it
to quit, the child then becomes a zombie the
zombie is required for the parent to successfully
do a wait for the child (and get certain status).
Zombie goes away when wait occurs. - Since init never ends, it avoids clogging up the
system with zombies by automatically calling wait
when a child quits.
21Processes Tree on a UNIX System
swapper is the scheduler
the family tree
22Process Termination
- Process executes last statement and asks the
operating system to decide it (exit). - Output data from child to parent (via wait).
- Process resources are deallocated by operating
system. - Parent may terminate execution of children
processes (abort). - Child has exceeded allocated resources.
- Task assigned to child is no longer required.
- Parent is exiting.
- Operating system does not allow child to continue
if its parent terminates in UNIX it child is
adopted by init. - Cascading termination.
- In UNIX terminated process may become a Zombie
before going away - these may haunt the system
for a while! (see earlier slide).
23Cooperating 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
synchronization required - Advantages of process cooperation
- Information sharing
- Computation speed-up - only when distributed
across multiple processors - Modularity - same rationale as used for
strucuring a program along function/subroutine or
OO class lines. - Convenience
24Producer-Consumer Problem
- Example of Cooperating Processes
- Paradigm for cooperating processes, producer
process produces information that is consumed by
a consumer process. - unbounded-buffer places no practical limit on the
size of the buffer. - bounded-buffer assumes that there is a fixed
buffer size. - In both cases the buffer is shared memory
shared by both processes
25Bounded-Buffer Shared-Memory Solution
- Shared data
- define BUFFER_SIZE 10
- typedef struct
- . . .
- item
- item bufferBUFFER_SIZE
- int in 0
- int out 0
- int counter 0
26Bounded-Buffer Producer Process
- Producer process
- item nextProduced
- while (1)
- while (counter BUFFER_SIZE)
- / do nothing /
- bufferin nextProduced
- in (in 1) BUFFER_SIZE
- counter
-
What if Consumer does not complete the
incrementing of shared variable counter? It
could take 3 instruction to increment and a task
switch could occur all three instructions are
complete?
27Bounded-Buffer Consumer Process
- Consumer process
- item nextConsumed
- while (1)
- while (counter 0)
- / do nothing /
- nextConsumed bufferout
- out (out 1) BUFFER_SIZE
- counter--
-
-
What if Producer does not complete the
incrementing of shared variable counter? It
could take 3 instruction to increment and a task
switch could occur all three instructions are
complete?
28Interprocess Communication (IPC)
- Mechanism for processes to communicate and to
synchronize their actions - handshaking, null
message scheme (see later). - Message system processes communicate with each
other without resorting to shared variables or
common (shared) memory/buffer. - IPC facility provides two operations(p. 111)
- send(message) message size fixed or variable
- receive(message)
- If P and Q wish to communicate, they need to
- establish a communication link between
themmsgqid msgget() in UNIX project - exchange messages via send/receive
- Implementation of communication link
- physical (e.g., shared memory, hardware bus,
network) Use of shared memory simulates message
passing - retains interface- covered in chapter
15 - logical (e.g., logical properties or
interface/API as seen by application) - only
logical implementation covered here.
29Implementation Questions
- How are links established? - explicitly vs.
implicitly. - Can a link be associated with more than two
processes? - ex multiple processes communicating
via a network. - How many links can there be between every pair of
communicating processes? Ex two processes may
communicate via two message queues. - What is the capacity of a link? (0, finite,
infinite) - Is the size of a message that the link can
accommodate fixed or variable? - Is a link unidirectional or bi-directional? - ex
UNIX pipes can be either - Blocking send/receive vs nonblocking send/receive
- has synchronization implications.
30Direct Communication
- Processes must name each other explicitly
- send (P, message) send a message to process P
- receive(Q, message) receive a message from
process QName the other process in the call.
P, Q are pids. In send, message can be a
pointer or the message itself, in receive,
message usually a pointer to where to put the
received message. - Properties of communication link
- Links are established automatically - only need
to know the others identity. - A link is associated with exactly one pair of
communicating processes P1 ltL1gt P2 ltL2gtP3 - Between each pair there exists exactly one link.
- The link may be unidirectional, but is usually
bi-directional. also asymmetric, receive from
anyone, id of sender passed to receiver
receive(id, message
31Indirect Communication
- Messages are directed and received from mailboxes
(also referred to as ports). - Each mailbox has a unique message queue 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 - P1 and P2 can communicate
with each other via multiple message queues. - Link may be unidirectional or bi-directional.
32Indirect Communication - continued
- Operations
- create a new mailbox- msgget in UNIX
- send and receive messages through mailbox
- destroy a mailbox- msgctl() return to UNIX
- Primitives are defined as
- send(A, message) send a message to mailbox A
- receive(A, message) receive a message from
mailbox AIn UNIX, mailbox A is the message Queue
id returned by the msgget call
33Indirect Communication - continued
- Mailbox sharing
- P1, P2, and P3 share mailbox A.
- P1, sends P2 and P3 receive.
- Who gets the message? - may be a race condition
- Solutions this is your problem!
- 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. - Dont do anything - maybe it does not matter.
The hungry dog approach. - in UNIX can send a message class id receiver
only receives messages in a certain class
34Synchronization - sect 4.5.3
- Message passing may be either blocking or
non-blocking. - Blocking is considered synchronous
- Non-blocking is considered asynchronous
- send and receive primitives may be either
blocking or non-blocking.
35Buffering
- Queue of messages attached to the link
implemented in one of three ways. - 1. Zero capacity 0 messagesSender must wait
for receiver (rendezvous). - 2. Bounded capacity finite length of n
messagesSender must wait if link full. - 3. Unbounded capacity infinite length Sender
never waits.
See notes by instructor Message Passing
Concepts and Synchronization for additional and
supplementary material on message passing. Read
pp. 1-4 for now.
36Client-Server Communication
- EX A user (client) sends request for some
information from a server which may also require
some computation by the serverSome approaches - Sockets
- Remote Procedure Calls
- Remote Method Invocation (Java)
37Sockets
- A socket is defined as an endpoint for
communication. - Identified as a concatenation of IP address and
port - identifies a host and a port on that host.
- The socket 161.25.19.81625 refers to port 1625
on host 161.25.19.8 - Communication consists between a pair of sockets.
38Socket Communication
Client assigned port by the local host computer (
? 1024)
Server port is Well Known (lt 1024) Client port
is assigned a port the host it is running on
39Remote Procedure Calls
- Remote procedure call (RPC) abstracts procedure
calls between processes on networked systems. - Message-based communication used to provide this
facility. - In contrast to general IPC message passing, RPC
messages are highly structured and formalized. - They are received by an RPC daemon, listening to
a port and contains the identifier of the
function to be called, and the parms to be
passed. - Stubs client-side proxy for the actual
procedure on the server. - Hides communication details and makes it look
like an ordinary local procedural call. - The client-side stub locates the server and
marshalls the parameters (creates a network
packet for parms). - The server-side stub receives this message,
unpacks the marshalled parameters, and performs
the procedure on the server - returned values
passed back to client using same technique.
40Execution of RPC
Matchmaker gets server port to allow client
port to bind procedure name to a remote server
port. Client receives the server port number for
the given preocedure.
41Remote 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.
42Marshalling Parameters
Skeleton receives parcel, un-marshals it, invokes
the desired method, marshals results, and returns
parcel to client.
Stub is proxy for remote object. Creates a parcel
consisting of method name marshaled parms.