Title: Chapter 4 Processes
1Chapter 4 Processes
2Outline
- Process Concept
- Process Scheduling
- Operation on Processes
- Cooperating Processes
- Interprocess Communication
- Communication in Client-Server Systems
3Process Concept
4Overview
- OS executes a variety of programs
- Batch system jobs
- Time-shared systems user programs or tasks
- The terms job and process almost interchangeably
- Process a program in execution
- A process includes (process image)
- Text section (program code), data section (global
variable) - Current activities PC (Program Counter),
registers - Stack Temporary data (parameter, local
variable) - Process vs. Program active vs. passive entities
50
Logically contiguous memory space (Virtual
memory)
12345
But physical memory occupied by a process may
not be contiguous (Chap 9)
6Steps 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
7Loader
Process Control Block
Program Code
Program Code
Data
Data
Stack
Executable binary file (Load Module)
Process Image in Main Memory
8OS 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
9Process State
- As a process executes, it changes state
- New The process is being created (put in job
queue) - Ready The process is waiting to be assigned to
a processor (put in ready queue) - Running Instructions are being executed
- Waiting The process is waiting for some event
to occur, such as an I/O completion or reception
of a signal (put in waiting queues) - Terminated The process has finished execution
10Diagram of Process State
- Only one process can be running on any processor
at any instant - Many processes may be ready and waiting
11Process 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)
12Process 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
13Operating System Control Structures
- An OS maintains the following tables for managing
processes and resources - Memory tables
- I/O tables (DST)
- File tables
- Process tables (this chapter)
14If Primary Process Table is allocated in advance
(like booting), there is a upper-bound for the
of processes that can exist in a system
15PCB may not be adjacent to code, data, and stack
16Location of the Process Image
- Each process image is in virtual memory
- May not occupy a contiguous range of physical
addresses (relies on memory management scheme) - But contiguous in the processs own virtual
(logical) memory - Both a private and shared memory address space is
used - The location of each process image is pointed to
by an entry in the Primary Process Table - For the OS to manage the process, at least part
of its image must be bring into main memory
17Process Control Block (PCB)
- Information associated with each process.
- Process identifier, user identifier, parent
process identifier - Process state new, ready, running, waiting
- Program counter address of the next instruction
- CPU registers accumulators, index registers
- CPU scheduling information priority
- Memory-management information base/limit
register, page tables - Accounting information CPU time used, time
limits - I/O status information list of I/O device
allocated, list of open files - Pointer to other PCBs
18Process Control Block (PCB)
19(No Transcript)
20(No Transcript)
21Thread
- The process model implies that a process is a
program that performs a single thread of
execution - In a single-thread word-processor program, the
user cannot simultaneously type in characters and
run the spell checker within the same process - If a process has multiple threads of execution
- It can perform more than one task at a time
- Chapter 5
22Process Scheduling
23Goal
- 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
24Process Scheduling Queues
- Job queue (New) set of all newly created
processes in the system - Ready queue (Ready) set of all processes
residing in main memory, ready and waiting to
execute - Device queues (Waiting) set of processes
waiting for an I/O device - Process migration between the various queues
- Process Scheduling queues are implement by linked
lists - Link by pointers in PCBs
25Ready Queue And Various I/O Device Queues
26Representation of Process Scheduling
Short-term Scheduler
Job queue
Long-term Scheduler
27Schedulers
- Long-term scheduler (or job scheduler) selects
which processes should be brought into the ready
queue (New to Ready) - Short-term scheduler (or CPU scheduler) selects
which process should be executed next and
allocates CPU (Ready to Running) - Medium-term scheduler removes processes from
memory at some later time, the process can be
reintroduced into memory and its execution can be
continued where it left off
28Schedulers (Cont.)
- Short-term scheduler is invoked very frequently
(milliseconds) ? must be fast - Long-term scheduler is invoked very infrequently
(seconds, minutes) ?may be slow - Long-term scheduler controls multiprogramming
degree - 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.
29CPU Switch From Process to Process (Context
Switch)
state current value of hardware PC,registers,
base/limitregisters (one CPU only has one or
fewsuch things)
Context Switch
30Context 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 - The context of a process is represented in the
PCB of a process - Context-switch time is overhead the system does
no useful work while switching - Time dependent on hardware support, memory
management method - Eg. Multiple sets of registers
31Context 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
32When 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
33Examples of Interrupts
- Clock process has expired his time slice and is
transferred to the ready state - I/O
- First move the processes that where waiting for
this event to the ready state - Then resume the running process or choose a
process of higher priority - Memory fault (Chapter 10)
- Memory address is in virtual memory so it must
bring corresponding block into main memory - Thus move this process to a blocked state
(waiting for the I/O to complete)
34Addition of Medium Term Scheduling
Medium-term Scheduler
Suspend
Medium-term Scheduler
Short-term Scheduler
Job queue
Long-term Scheduler
35The need for Swapping
- So far, all the processes had to be (at least
partly) in main memory - Even with virtual memory, keeping too many
processes in main memory will deteriorate the
systems performance - The worst case all the processes in main memory
are blocked and CPU is idle - The OS may need to suspend some processes, i.e.
to swap them out to disk. - Chapter 9
36Operations on Processes
37Process 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
38Process Creation (Cont.)
- Steps for process creation
- Assign a unique process identifier
- Allocate space for the process image
- Initialize process control block
- many default values (ex state is New, no I/O
devices or files...) - Set up appropriate linkages
- Ex add new process to linked list used for the
scheduling queue
39Process Creation -- Parent/Child Process
- Parent process creates 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
(partition) - Parent and child share no resources
- Execution
- Parent and children execute concurrently
- Parent waits until children terminate
40A Tree of Processes On A Typical UNIX System
41Process Creation -- Parent/Child Process (Cont.)
- Address space
- Child duplicate of parent
- Child has a program loaded into it
- UNIX examples
- fork system call creates new process
- execlp system call used after a fork to replace
the process memory space with a new program - Other OS
- DEC VMS create a new process, loads a specified
program into that process, and starts it running - Windows NT both models
42Fork and Execlp
fork
Parent
Child
Parent Process Image
execlp
Parent
Child
43C Program Forking A Separate Process
44When does a process gets terminated?
- Batch job issues Halt instruction
- User logs off
- Process executes a service request to terminate
- Error and fault conditions
45Reasons 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
46Reasons 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
47Process Termination
- Process executes last statement and asks OS to
decide it (exit) - Output data from child to parent (via wait)
- Process resources are de-allocated 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
- OS does not allow child to continue if its parent
terminates - Cascading termination
48Cooperation Processes
49Cooperating 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
- Cooperation processes require communication and
synchronization
50Producer-Consumer Problem
- 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. - The producer and consumer must be synchronized
- Consumer does not try to consume an item that has
not yet been produced - Buffer implementation
- Explicitly coded by AP with the use of shared
memory - Interprocess communication facility (IPC)
51Shared Bounded-Buffer Example
- Shared memory
- Shared buffer -- circular array
- in next free position
- out first full position
- BUFFER_SIZE max of items (can only use
BUFFER_SIZE 1 items) - Require that producers and consumers share a
common buffer pool, and that the code for
implementing the buffer be written explicitly by
the application programmer
52Shared Bounded-Buffer Example (Cont.)
Producer
Consumer
53Interprocess Communication
54Interprocess Communication (IPC)
- Mechanism for processes to communicate and to
synchronize their actions (provided by OS) - 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,
network) - Logical (e.g., logical properties)
55Implementation 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?
56Logical 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
57Direct Communication
- Processes must name each other explicitly
symmetry - send (P, message) send a message to process P
- receive (Q, message) receive a message from
process Q - Asymmetry in addressing
- send (P, message) send a message to process P
- receive (id, message) id is set to the name of
the process with which communication has taken
place - 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 - Disadvantage when the name of a process changes
58Indirect Communication
- Messages are sent to and received from mailboxes
(also referred to as ports) - Each mailbox has a unique id
- Processes can communicate only if they share a
mailbox - send(A, message), receive(A, message)
- 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
59Indirect Communication (Cont.)
- Mailbox sharing
- P1, P2, and P3 share mailbox A.
- P1, sends P2 and P3 receive.
- Who gets the message?
- Solutions depend on the following issues
- 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
60Indirect 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
61Synchronization
- Blocking or non-blocking synchronous or
asynchronous - Blocking send sender blocked until the message
is received by the receiver or by the mailbox - Non-blocking send sender sends the message and
resumes operation - Blocking receive receiver blocks until a message
is available - Non-blocking receive receiver retrieves either a
valid message or a null
Rendezvous both the send and receive are blocking
62Buffering
- Queue of messages attached to the link
implemented in one of three ways. - Zero capacity 0 messages
- Sender must wait for receiver
- Bounded capacity finite length of n messages
- Sender must wait if link full
- Unbounded capacity infinite length
- Sender never waits
63Client-Server Communication
- Especially for client-server distributed systems
- Sockets
- Remote Procedure Calls (RPC)
- Remote Method Invocation (RMI)
64Socket
- Socket communication endpoint of a
communication link - Model network I/O based on conventional file I/O
- Concatenation of IP address and port
- The socket 161.25.19.81625 refers to port 1625
on host 161.25.19.8 - 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 - Berkeley socket
- WinSock standard group for Windows applications
65(No Transcript)
66Port
After connection is established, noneed to
mention ports in messages
Socket
Datagram
need to mention ports in every message
67Socket Primitives
68Connectionless Socket Communication
69Connection-Oriented Client/Server Socket
Communication (I)
Reply
Request
Rendezvous
- Connection-oriented communication pattern using
sockets.
70Connection-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
71Establishing a Simple Server (Stream Socket) in
Java
- Step1 Create a ServerSocket Object
- ServerSocket s new ServerSocket(port,
queueLength) - Step2 Create a Socket and Wait for a Connection
- Socket connect s.accept()
- Step3 Associate Input and Output Stream with the
Socket - connect.getInputStream
- connect.getOutputStream
- Step4 Process Connection
- Step5 Close Connection
Figure 4.10 Time-of-day server
72Establishing a Simple Client (Stream Socket) in
Java
- Step1 Create a Socket to make connection
- Socket connect new Socket(ServerName, port)
- Step2 Associate Input and Output Stream with the
Socket - connect.getInputStream
- connect.getOutputStream
- Step3 Process Connection
- Step4 Close Connection
Figure 4.11 Time-of-day client
73Establishing 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)
74RPC Operations
- Remote procedure call (RPC) abstracts procedure
calls between processes on networked systems. - Principle of RPC between client and server (next
slide) - Flow of RPC
- Implementation Issues
- Parameter and result passing, and data conversion
- Binding
- Compilation (self study)
- Exception and failures handling (self study)
- Security (self study)
- Further Study of RPC Chapter 4 of Advanced OS
Course
75Principle 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)
76Flow of RPCs
77Steps of a RPC
- Client procedure calls client stub in normal way
- Client stub builds message, calls local OS
- Client's OS sends message to remote OS
- Remote OS gives message to server stub
- Server stub unpacks parameters, calls server
- Server does work, returns result to the stub
- Server stub packs it in message, calls local OS
- Server's OS sends message to client's OS
- Client's OS gives message to client stub
- Stub unpacks result, returns to client
78Client and Server Stubs
- Client stub contains the actual procedures that
the client program will call - Collect and pack parameters into outgoing message
and then call the runtime system to send it - Unpack the reply and return values to the client
- Server stub contains the procedures called by
the runtime system on the server machine when an
incoming messages arrives, and then call the
actual server procedures that do the work
79Parameter Passing and Data Conversion
- Parameter marshaling 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) - How about complex data structure like tree or
graph? - 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
80Parameter Passing
- Steps involved in doing remote computation
through RPC
2-8
Server may support manyprocedures
81Binding a Client to a Server
Matchmaker
82Execution of RPC
83Exception 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
84Failure 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
85Remote 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.
86Remote 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
87Marshalling Parameters in RMI