Title: Chapter 4: Processes
1Chapter 4 Processes
- Process Concept
- Process Scheduling
- Operations on Processes
- Cooperating Processes
- Interprocess Communication
- Communication in Client-Server Systems
Synthesized from Stallingss and text book slides
2Major Process Management Functions
- Interleave the execution of several processes to
maximize processor utilization while providing
reasonable response time - Allocate resources to processes
- Support interprocess (IP) communication and user
creation of processes
3Process 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. - Task is also used in the literature
- Process a program in execution process
execution must progress in sequential fashion. - A process includes
- program counter
- stack
- data section
- Can be traced
- list the sequence of instructions that execute
4Switch processes
Assume no virtual memory
From Stallings book
5(No Transcript)
6Process C
Process A
Dispatcher
dispatcher
Process A
Process B
Dispatcher
dispatcher
Process C
Process C
7Process State
- As a process executes, it changes state
- new The process is being created.
- Submission of a batch job
- User logs on
- Created to provide a service such as printing
- Process creates another process
- A new process has not yet been loaded into main
memory - running Instructions are being executed.
- waiting (blocked) 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.
- Batch job issues Halt instruction
- User logs off
- Quit an application
- Error and fault conditions
8Reasons for Process Termination
- Normal completion
- Time limit exceeded
- Memory unavailable
- Bounds violation
- Protection error
- example write to read-only file
- Arithmetic error
- Time overrun
- process waited longer than a specified maximum
for an event
9Reasons for Process Termination
- I/O failure
- Invalid instruction
- happens when try to execute data
- Privileged instruction
- Data misuse
- Operating system intervention
- such as when deadlock occurs
- Parent terminates so child processes terminate
- Parent request
10Diagram of Process State
http//www.infocom.cqu.edu.au/Units/win2000/85349/
Resources/Animations/
11(No Transcript)
12Using Two Queues
13Print I/O event
Disk I/O event
14Suspended Processes
- Assume no virtual memory
- Entire process must be loaded into memory
- All processes in all queues must be resident in
main memory - Processor is faster than I/O so all processes
could be waiting for I/O - Processor is idle
- Enlarge main memory ??
- Implies enlarged processes ?
- Swap these processes to disk to free up more
memory - SWAPPING itself an I/O operation (disk is faster
compared to tape, printer) - Blocked state becomes suspend state when swapped
to disk - Suspend queue the processes that temporarily
kicked out of memory - Two new states
- Blocked, suspend the process in the secondary
memory and awaiting an event - Ready, suspend the process in the secondary
memory and available for execution
15One Suspend State
Ready queue is empty There are blocked
processes.
After swapping-out, the OS can admit a new
process or bring into the memory a suspended
process (preferred)
16Difficulties of Single Suspend State
- It is not good to bring a suspended-blocked
process into memory - Not ready for execution
- If event occurs (the suspended one is waiting
for) - The process is potentially available for
execution - IF THERE IS VIRTUAL MEMORY
- NO EXPILICIT SWAPPING !
- Still, for performance purpose, the OS can swap
in-out processes
17Two Suspend States
Possible but not necessary transitions
18Some of the Transitions
- Running?Ready/Suspend
- Preempting the process because a higher-priority
process on the Blocked/Suspend queue has just
become unblocked, needs more memory - Blocked/Suspend?Ready/Suspend
- Event occurs
- Ready/Suspend?Ready
- No ready process in m. memory or ready/suspend
process has higher priority than the ready
processes - Blocked/Suspend?Blocked
- Event will occur soon. The blocked/suspend
process has higher priority than ready/suspend
ones
19Reasons for Process Suspension
20Operating System Control Structures
- Information about the current status of each
process and resource - Tables are constructed for each entity the
operating system manages - Memory Tables
- I/O Tables
- File Tables
- Process Table
21(No Transcript)
22Memory Tables
- Allocation of main memory to processes
- Allocation of secondary memory to processes
- Protection attributes for access to shared memory
regions - Information needed to manage virtual memory
23I/O Tables
- I/O device is available or assigned
- Status of I/O operation
- Location in main memory being used as the source
or destination of the I/O transfer
24File Tables
- Existence of files
- Location on secondary memory
- Current Status
- Attributes
- Sometimes this information is maintained by a
file-management system
25Process Table
- Where process is located
- Attributes necessary for its management
- Process ID
- Process state
- Location in memory
26Process Location
- Process includes set of programs to be executed
- Data locations for local and global variables
- Any defined constants
- Stack
- Process control block
- Collection of attributes
- Process image
- Collection of program, data, stack, and attributes
27Process Control Block (PCB)
28Process Control Block
- Process identification
- Identifiers
- Numeric identifiers that may be stored with the
process control block include - Identifier of this process
- Identifier of the process that created this
process (parent process) - User identifier
29Process Control Block
- Processor State Information
- User-Visible Registers
- A user-visible register is one that may be
referenced by means of the machine language that
the processor executes. Typically, there are from
8 to 32 of these registers, although some RISC
implementations have over 100.
30Process Control Block
- Processor State Information
- Control and Status Registers
- These are a variety of processor registers that
are employed to control the operation of the
processor. These include - Program counter Contains the address of the
next instruction to be fetched - Condition codes Result of the most recent
arithmetic or logical operation (e.g., sign,
zero, carry, equal, overflow) - Status information Includes interrupt
enabled/disabled flags, execution mode
31Process Control Block
- Processor State Information
- Stack Pointers
- Each process has one or more last-in-first-out
(LIFO) system stacks associated with it. A stack
is used to store parameters and calling addresses
for procedure and system calls. The stack pointer
points to the top of the stack.
32Process Control Block
- Process Control Information
- Scheduling and State Information
- This is information that is needed by the
operating system to perform its scheduling
function. Typical items of information - Process state defines the readiness of the
process to be scheduled for execution (e.g.,
running, ready, waiting, halted). - Priority One or more fields may be used to
describe the scheduling priority of the process.
In some systems, several values are required
(e.g., default, current, highest-allowable) - Scheduling-related information This will depend
on the scheduling algorithm used. Examples are
the amount of time that the process has been
waiting and the amount of time that the process
executed the last time it was running. - Event Identity of event the process is awaiting
before it can be resumed
33Process Control Block
- Process Control Information
- Data Structuring
- A process may be linked to other process in a
queue, ring, or some other structure. For
example, all processes in a waiting state for a
particular priority level may be linked in a
queue. A process may exhibit a parent-child
(creator-created) relationship with another
process. The process control block may contain
pointers to other processes to support these
structures.
34Process Control Block
- Process Control Information
- Interprocess Communication
- Various flags, signals, and messages may be
associated with communication between two
independent processes. Some or all of this
information may be maintained in the process
control block. - Process Privileges
- Processes are granted privileges in terms of the
memory that may be accessed and the types of
instructions that may be executed. In addition,
privileges may apply to the use of system
utilities and services.
35Process Control Block
- Process Control Information
- Memory Management
- This section may include pointers to segment
and/or page tables that describe the virtual
memory assigned to this process. - Resource Ownership and Utilization
- Resources controlled by the process may be
indicated, such as opened files. A history of
utilization of the processor or other resources
may also be included this information may be
needed by the scheduler.
36(No Transcript)
37Processor State Information
- Contents of processor registers
- User-visible registers
- Control and status registers
- Stack pointers
- Program status word (PSW)
- contains status information
- Example the EFLAGS register on Pentium machines
38Pentium II EFLAGS Register
39CPU Switch From Process to Process
40Process 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.
41Ready Queue And Various I/O Device Queues
42Representation of Process Scheduling
resources
Queues
Parent wait for its termination
43Schedulers
- Long-term scheduler (or job scheduler) selects
which processes should be brought into the ready
queue. - Short-term scheduler (or CPU scheduler) selects
which process should be executed next and
allocates CPU.
44Addition of Medium Term Scheduling
45Schedulers (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.
46Context 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. - Time dependent on hardware support.
47Process 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.
- Parent and child share no resources.
- Execution
- Parent and children execute concurrently.
- Parent waits until children terminate.
48Process 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.
49Spawn A Process in UNIX
How many processes with identical codes this
program creates!
1, 2, 3, 4, 5 ?
/ p. 505, A Book on C, by Al Kelley and Ira Pohl
/ include ltstdio.hgt main() int fork(void),
cp1,cp2 cp1 fork() /new process/ cp2
fork() /new process/ sleep(2) / suspend
execution for an interval / printf("In main
value d\n",value)
In UNIX type command from shell man fork
50Output
cp1 21163 --- cp2 21164 cp1 0 --- cp2
21165 cp1 0 --- cp2 0 cp1 21163 --- cp2 0
51More FORK(), EXEC and WAIT?
include ltstdio.hgt main(int argc, char
argv) int pid pid fork() if (pid lt
0) / error / fprintf(stderr, Fork
Failed) exit(-1) Else if (pid
0) / child process /
execlp(/bin/ls, ls,NULL)
printf(child complete \n) / NOT REACHABLE
/ else / parent process /
wait(NULL) // wait for childs termination
printf(Child complete) exit(0)
52Processes Tree on a UNIX System
53Process 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. - Cascading termination.
54Cooperating 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
- Shared file
- Computation speed-up
- Break a task into multiple subtasks
- Subtasks will execute in parallel
- Multiple processing elements (CPUs, I/O channels)
- Modularity
- Modular system design
- Convenience
- Simultaneous editing, printing, compiling
55Producer-Consumer Problem(Example cooperation)
- 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. - The Producer can always produce new items
- The Consumer has to wait for new items
- bounded-buffer assumes that there is a fixed
buffer size. - The Producer must wait if the buffer is full
- The Consumer must wait if the buffer is empty
- The creation of BUFFER
- By OS through IPC facility
- By application programmer with the use of shared
memory
56Bounded-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
57Bounded-Buffer (Circular Queue!)
- Producer Process
- item nextProduced
- while (1)
- while (((in 1) BUF_SIZE) out)
- / do nothing /
- bufferin nextProduced
- in (in 1) BUFFER_SIZE
-
- Consumer Process
- item nextConsumed
- while (1)
- while (in out)
- / do nothing /
- nextConsumed bufferout
- out (out 1) BUFFER_SIZE
-
-
http//www.infocom.cqu.edu.au/Units/win2000/85349/
Resources/Animations/Bounded_Buffer/bb.swf
58Interprocess 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 implementation (e.g., shared memory,
hardware bus, network) - logical implementation (e.g., logical properties)
59Logical Implementation 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?
- Zero, bounded, unbounded
- Is the size of a message that the link can
accommodate fixed or variable? - Is a link unidirectional or bi-directional?
60Direct Communication(processes refer to each
other by name)
- 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. - Symmetry in addressing
- Both sender and receiver names the process
- Antisymmetric adressing
- send (P, message) send a message to process P
- receive(id, message) receive a message from
process id - Changing the names of the processes ???
61Indirect 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.
62Indirect Communication
- Owner is the process
- Mailbox is part of the address space of the
process - Process terminates ? mailbox disappears
- Operations (if OS is the owner!)
- Process would be able to
- create a new mailbox (eg A)
- Owner can be a process or the OS
- Modify the ownership/receive privileges (via
system calls) - 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
63Indirect 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.
64Synchronization(Design options for Send and
Receive)
- Message passing may be either blocking or
non-blocking. - Blocking is considered synchronous
- The Sender is blocked until until the message is
received by the receiving process or by the
mailbox. - The receiver blocks until a message is available.
- Non-blocking is considered asynchronous
- The Sender sends the message and resumes
operation. - The receiver retrieves either a valid message or
a NULL. - Both SEND and RECEIVE are blocking
- We have rendezvous between sender and receiver
65Buffering
- 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 (blocks).
66Client-Server Communication
- Sockets
- Remote Procedure Calls
- Remote Method Invocation (Java)
Service request
SERVER
SERVER/ CLIENT
Service request
CLIENT
Return the results
Return the results
67Sockets
- 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 - Communication consists between a pair of sockets.
- Famous servers and their ports
- Telnet server listens to port 23
- ftp ? port 21
- http (web) ? port 80
- All ports lt 1024 ? well known ? implement
standard services
68Socket Communication
- Client process initiates a request for a
connection - The host computer assigns a PORT Arbitrary number
gt 1024 eg 1625
69JAVA sockets
- Socket Types in JAVA
- Connection-oriented (TCP) Socket class
- Connectionless (UDP) DatagramSocket
- MulticastSocket subclass of DatagramSocket
- Server sets
- sock new ServerSocket(5155) // server listens
the port 5155 - client sock.accept() // listen for connections
- Client sets
- sock new Socket(127.0.0.1,5155)
- In sock.getInputStream()
70Connection-oriented
http//www.linktionary.com/c/connections.html
71Connectionless
72Remote Procedure Calls
- Remote procedure call (RPC) abstracts procedure
calls between processes on networked systems. - Built on top of basic IPC facilty
- Message based communication
- Messages exchanged for RPC is well structured
- 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. - External data representation (XDR)
- Types of host machines are different (eg.
alignment)
73Execution of RPC
74Remote 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.
75Parameter Passing in RMI
- Local (nonremote) objects are passed by copy
using object serialization technique - Remote objects are passed by reference
- Server knows address of the object, therefore can
invoke methods on this object remotely. - Remote objects are to be passed as parameters to
remote objects - They must implement the interface
java.io.Serializable - Object serialization the state of an object to
be written to a byte stream.
76Marshalling Parameters