Title: Chapter 4: Processes
1Chapter 4 Processes
- Process Concept
- Process Scheduling
- Operations on Processes
- Cooperating Processes
- Interprocess Communication
- Communication in Client-Server Systems
2Process Concept
- OS executes a variety of programs
- Batch system jobs
- Time-shared systems user programs or tasks
- interchangeable terms job and process
- Process a program in execution
- process execution must progress sequentially
- A process includes
- Text section (program code), data section (global
variable) - Current activities PC (Program Counter),
registers - Stack Temporary data (parameter, local
variable)
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 process. - terminated The process has finished execution.
4Diagram of Process State
5Process Control Block (PCB)
- Information associated with each process.
- Process state
- Program counter
- CPU registers
- CPU scheduling information
- Memory-management information
- Accounting information
- I/O status information
6Process Control Block (PCB)
7CPU Switch From Process to Process
8Process 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.
9Ready Queue And Various I/O Device Queues
10Representation of Process Scheduling
Short-term Scheduler
Long-term Scheduler
11Schedulers
- Long-term scheduler
- job scheduler
- selects which processes should be brought into
the ready queue. - Short-term scheduler
- CPU scheduler
- selects which process should be executed next and
allocates CPU.
12Addition of Medium Term Scheduling
Medium-term Scheduler
Medium-term Scheduler
Suspend
Short-term Scheduler
Long-term Scheduler
13Schedulers (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 (select a good mix of I/O- and
CPU- bound processes) - 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.
14Context 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 of a process represented in PCB
- Context-switch time is overhead the system does
no useful work while switching. - Time dependent on hardware support.
- Multi-sets of registers
15Context Switch (Cont.)
- Steps for Context Switch
- Save context of processor including program
counter and other registers - Update the PCB of the running process with its
new state and other associate information - Move PCB to appropriate queue - ready, waiting
- Select another process for execution (short-term
scheduler) - Update PCB of the selected process
- Restore CPU context from that of the selected
process
16When to Switch a Process ?
- A process switch may occur whenever the OS has
gained control of CPU - System Call
- explicit request by the program (ex file open).
The process will probably be blocked - Trap
- An error resulted from the last instruction. It
may cause the process to be moved to the Exit
state - Interrupt
- the cause is external to the execution of the
current instruction. Control is transferred to IH
17Process Creation
- When does a process get created?
- Submission of a batch job
- User logs on command interpreter
- Created by OS to provide a service to a user
(ex printing a file) - Spawned by an existing process
- a user program can create a number of processes
18Process Creation
- Parent process create children processes, which,
in turn create other processes, forming a tree of
processes. - Resource sharing
- Parent and children share all resources.
- Children share subset of parents resources.
- Why? No overloading system
- Parent and child share no resources.
- Execution
- Parent and children execute concurrently.
- Parent waits until children terminate.
19Process Creation (Cont.)
- Address space
- Child duplicate of parent.
- Child has a program loaded into it.
- UNIX examples
- fork system call creates new process
- Both continue, return values differ
- exec system call used after a fork to replace the
process memory space with a new program. - Fig. 4.8
20Fork and Execlp
fork
Parent
Child
Parent Process Image
execlp
Parent
Child
21Processes Tree on a UNIX System
22When does a process gets terminated?
- Batch job issues Halt instruction
- User logs off
- Process executes a service request to terminate
- Error and fault conditions
23Reasons for Process Termination
- Normal completion
- Time limit exceeded
- Memory unavailable
- Memory bounds violation
- Protection error
- example write to read-only file
- Arithmetic error
- Time overrun
- process waited longer than a specified maximum
for an event
24Reasons for Process Termination (Cont.)
- I/O failure
- Invalid instruction
- happens when try to execute data
- Privileged instruction
- Operating system intervention
- such as when deadlock occurs
- Parent request to terminate one offspring
- Parent terminates so child processes terminate
25Process Termination
- Process executes last statement and asks the
operating system to delete it (exit). - Output data from child to parent (via wait,
return pid). - 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. - Cascading termination.
26Cooperating 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
- Given multiple processing elements
- Modularity
- Convenience
27Producer-Consumer Problem
- Paradigm for cooperating processes, producer
process produces information that is consumed by
a consumer process. - (Print prg, prn), (compiler, assembler),
- unbounded-buffer places no practical limit on the
size of the buffer. - bounded-buffer assumes that there is a fixed
buffer size.
28Bounded-Buffer Shared-Memory Solution
- Shared data
- define BUFFER_SIZE 10
- Typedef struct
- . . .
- item
- item bufferBUFFER_SIZE
- int in 0
- int out 0
- Solution is correct, but can only use
BUFFER_SIZE-1 elements
29Bounded-Buffer Producer Process
- item nextProduced
- while (1)
- while (((in 1) BUFFER_SIZE) out)
- / do nothing /
- bufferin nextProduced
- in (in 1) BUFFER_SIZE
-
30Bounded-Buffer Consumer Process
- item nextConsumed
- while (1)
- while (in out)
- / do nothing /
- nextConsumed bufferout
- out (out 1) BUFFER_SIZE
-
-
31Interprocess Communication (IPC)
- Mechanism for processes to communicate and to
synchronize their actions. - Message system processes communicate with each
other without resorting to shared variables. - 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)
32Implementation Questions
- How are links established?
- Can a link be associated with more than two
processes? - How many links can there be between every pair of
communicating processes? - What is the capacity of a link?
- Is the size of a message that the link can
accommodate fixed or variable? - Is a link unidirectional or bi-directional?
33Logical Implementation
- Several methods for logically implementing a link
and the send/receive operations - Direct or indirect communication
- Symmetric or asymmetric communication
(addressing) - Automatic or explicit buffering
- Send by copy or send by reference
- Fixed-size or variable-sized messages
34Direct 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. - Asymmetric send (p, msg), receive(id, msg)
- Limited modularity (change name!)
35Indirect 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.
36Indirect 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
37Indirect 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.
38Indirect Communication (Cont.)
- A mailbox may be owned either by a process or by
OS - Owned by a process
- Owner (receiver) vs. user (sender)
- Owned by OS
- Support the following operations
- Create a new mailbox
- Send and receive messages through mailbox
- Destroy a mailbox
- Creator is the default owner
- Ownership and receiving privilege may be passed
to other processes through appropriate system
calls
39Synchronization
- 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. - Rendezvous blocking send/receive
40Buffering
- 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.
41Client-Server Communication
- Sockets
- Remote Procedure Calls
- Remote Method Invocation (Java)
42Sockets
- A socket is defined as an endpoint for
communication. - Concatenation of IP address and port
- The socket 161.25.19.81625 refers to port 1625
on host 161.25.19.8 - Port lt 1024std service
- Communication consists between a pair of
sockets. - Low-level form of communication
- Allow only an unstructured stream of bytes to be
exchanged - Responsibility of Client/Server to impose a
structure on the data
43(No Transcript)
44Socket Communication
pp. 118, 119, 120
45Port
After connection is established, noneed to
mention ports in messages
Socket
Datagram
need to mention ports in every message
46Socket Primitives
47Connectionless Socket Communication
48Connection-Oriented Client/Server Socket
Communication (I)
Reply
Request
Rendezvous
- Connection-oriented communication pattern using
sockets.
49Connection-Oriented Client/Server Socket
Communication (II)
The original port is used foraccepting
connection requestsfrom other clients
Use the new port to communicatewith each
connected client
50Establishing Datagrams in Java
- Set up a DatagramSocket
- Send socket new DatagramSocket()
- Receive socket new DatagramSocket(port)
- Set up a Packet
- Send s new DatagramPacket(.Packet Info..)
- Receive r new DatagramPacket(data, length)
- Send/Receive
- socket.receive(packet)
- socket.send(packet)
51Remote 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.
52Principle of RPC Between a Client and Server
Program
Information can be transported from caller to
callee in parameters and can come back in the
procedure result
Client suspends
Receive(blocked)
Receive(blocked)
53Parameter Passing and Data Conversion
- Parameter marshalling rules for RPC parameter
passing and data/message conversion - Passing reference parameters call-by-copy/restor
e - Ex. Array Call-by-value (entry)
call-by-reference (exit) - Data representation and type checking
- Different types of machines ? different data
representation - ASCII, EBCDIC
- 32-bit 2s complement or 16-bit
sign-and-magnitude for integer - External data representation (XDR)
machine-independent data representation - Transfer syntax ? rules regarding transfer of
messages - Message format, data representation in messages
54Parameter Passing
- Steps involved in doing remote computation
through RPC
2-8
Server may support manyprocedures
55Binding a Client to a Server
Matchmaker
56Execution of RPC
XDR
p. 123
57Exception Handling
- Exception in server procedures
- Overflow/underflow, protection violation
- Fundamental questions
- How does the server report status information to
the client? - How does a client send control information (e.g.
stop) to the server? - Mechanisms
- Put flag for exception handling in the send
primitives - Use a separate channel (socket connection or
port) for exchanging exception-handling messages
58Failure Handling
- Five classes of RPC failures
- The client is unable to locate the server
- The request message from the client to the server
is lost - The server crashes after receiving a request
- The reply message from the server to the client
is lost - The client crashes after sending a request
59Remote 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.
60Remote Method Invocation (Cont.)
- Difference between RPC and RMI
- RPC support procedural programming
- Only remote procedures or functions may be called
- RMI is object-based
- RMI supports invocation of methods on remote
objects - The parameters to remote procedures are ordinary
data structures in RPC - It is possible to pass objects as parameters to
remote methods in RMI
61Marshalling Parameters
620
Logically contiguous memory space (Virtual
memory)
12345
But physical memory occupied by a process may
not be contiguous (Chap 9)
63Steps for loading a process in memory
- Linker combines object modules into a single
executable binary file (load module) - The loader places the load module in physical
memory
64Loader
Process Control Block
Program Code
Program Code
Data
Data
Stack
Executable binary file (Load Module)
Process Image in Main Memory
65OS Requirements for Processes
- OS must interleave the execution of several
processes to maximize CPU usage while providing
reasonable response time - OS must allocate resources to processes (memory,
I/O device, etc.) while avoiding deadlock - OS must support inter-process communication,
synchronization, and user creation of processes
66Process State (Cont.)
- The New state
- OS has performed the necessary actions to create
the process - Has created a process identifier
- Has created tables needed to manage the process
- Memory table, file table, PCB
- But has not yet committed to execute the process
(not yet admitted) - Because resources are limited
- Process image may be put in secondary storage
(like swapping space)
67Process State (Cont.)
- The Terminated state
- Exit moves the process to this state
- It is no longer eligible for execution
- Tables and other info are temporarily preserved
for auxiliary program - Ex accounting program that cumulates resource
usage for billing the users - The process (and its tables) gets deleted when
the data is no more needed
68(No Transcript)
69(No Transcript)
70Process Scheduling Goal
- Multiprogramming -- have some process running at
all times - Maximize CPU utilization
- Time Sharing -- Let users interact with each
program while it is running - Minimize response time
- For a uni-processor system
- There will never be more than one running process
- The reset process will have to wait until the CPU
is free and can be rescheduled