Date post: | 07-Jul-2018 |
Category: |
Documents |
Upload: | swapnil-bishnu |
View: | 215 times |
Download: | 0 times |
of 17
8/19/2019 C3-Process [Compatibility Mode]
1/49
Processes
8/19/2019 C3-Process [Compatibility Mode]
2/49
Processes and threads
Processes
Threads
Scheduling
Interprocess communication
Classical IPC problems
Deadlock
8/19/2019 C3-Process [Compatibility Mode]
3/49
Process 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 almostinterchangeably.
Process – a program in execution; process executionmust progress in sequential fashion.
A process includes:
program counter stack
data section
8/19/2019 C3-Process [Compatibility Mode]
4/49
What is a process?
Code, data, and stack
Usually (but not always) has its own address space
Program state
CPU registers
Program counter (current location in the code) Stack pointer
Only one process can be running in the CPU at any
given time!
8/19/2019 C3-Process [Compatibility Mode]
5/49
The process model
Multiprogramming of four programs
Conceptual model 4 independent processes
Processes run sequentially
Only one program active at any
instant! That instant can be very short…
A
C
D
Single PC
(CPU’s point of view)
AB
C D
Multiple PCs
(process point of view)
B
B
A
BC
D
Time
8/19/2019 C3-Process [Compatibility Mode]
6/49
When is a process created?
Processes can be created in two ways
System initialization: one or more processes created when
the OS starts up
Execution of a process creation system call: something
explicitly asks for a new process System calls can come from
User request to create a new process (system call executed
from user shell)
Already running processes
User programs System daemons
8/19/2019 C3-Process [Compatibility Mode]
7/49
Process 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 parent’s resources.
Parent and child share no resources.
Execution
Parent and children execute concurrently.
Parent waits until children terminate.
8/19/2019 C3-Process [Compatibility Mode]
8/49
Process Creation (Cont.)
Address space
Child duplicate of parent (UNIX)
Child has copy of parent’s address space with program id of 0
(parent is a non-zero value)
Enables easy communication between the two The child process’ memory space is replaced with a new program which is
then executed. Parent can wait for child to complete or create more
processes
Child has a program loaded into it directly(DEC VMS)
Windows NT supports both models
8/19/2019 C3-Process [Compatibility Mode]
9/49
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
8/19/2019 C3-Process [Compatibility Mode]
10/49
Processes Tree on a UNIX System
8/19/2019 C3-Process [Compatibility Mode]
11/49
When do processes end?
Conditions that terminate processes can be
Voluntary
Involuntary
Voluntary
Normal exit Error exit
Involuntary
Fatal error (only sort of involuntary)
Killed by another process
8/19/2019 C3-Process [Compatibility Mode]
12/49
Process Termination
Process executes last statement and asks the operating
system to delete it (exit).
Return 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.
8/19/2019 C3-Process [Compatibility Mode]
13/49
Process Termination
In UNIX, a process can be terminated via the exit system
call.
Parent can wait for termination of child by the wait
system call wait returns the process identifier of a terminated child so that
the parent can tell which child has terminated
If a parent terminates, all children are assigned the init process
as their new parent
8/19/2019 C3-Process [Compatibility Mode]
14/49
Process hierarchies
Parent creates a child process
Child processes can create their own children
Forms a hierarchy
UNIX calls this a “process group”
If a process exits, its children are “inherited” by theexiting process’s parent
Windows has no concept of process hierarchy
All processes are created equal
8/19/2019 C3-Process [Compatibility Mode]
15/49
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.
ready: The process is waiting to be assigned to a
process.
terminated: The process has finished execution.
8/19/2019 C3-Process [Compatibility Mode]
16/49
Blocked
(waiting)
Created
Exit
Ready
Running
Process states
Process in one of 5 states Created
Ready
Running
Blocked
Exit
Transitions between states1 - Process enters ready queue
2 - Scheduler picks this process
3 - Scheduler picks a different process
4 - Process waits for event (such asI/O)
5 - Event occurs
6 - Process exits
7 - Process ended by another process
1
5
4
3
2
7
76
8/19/2019 C3-Process [Compatibility Mode]
17/49
Process 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
8/19/2019 C3-Process [Compatibility Mode]
18/49
Process Control Block (PCB)
8/19/2019 C3-Process [Compatibility Mode]
19/49
Context 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 nouseful work while switching.
Time dependent on hardware support.
Varies from 1 to 1000 microseconds
8/19/2019 C3-Process [Compatibility Mode]
20/49
CPU Switch From Process to Process
8/19/2019 C3-Process [Compatibility Mode]
21/49
Processes in the OS
Two “layers” for processes
Lowest layer of process-structured OS handles interrupts,
scheduling
Above that layer are sequential processes Processes tracked in the process table
Each process has a process table entry
Scheduler
0 1 N -2 N -1…
Processes
8/19/2019 C3-Process [Compatibility Mode]
22/49
What’s in a process table entry?
File managementRoot directory
Working (current) directory
File descriptors
User ID
Group ID
Memory managementPointers to text, data, stack
or
Pointer to page table
Process managementRegisters
Program counter
CPU status word
Stack pointer
Process state
Priority / scheduling parametersProcess ID
Parent process ID
Signals
Process start time
Total CPU usage
May be
stored
on stack
8/19/2019 C3-Process [Compatibility Mode]
23/49
What happens on a trap/interrupt?
1. Hardware saves program counter (on stack or in aspecial register)
2. Hardware loads new PC, identifies interrupt3. Assembly language routine saves registers
4. Assembly language routine sets up stack 5. Assembly language calls C to run service routine6. Service routine calls scheduler 7. Scheduler selects a process to run next (might be
the one interrupted…)
8. Assembly language routine loads PC & registersfor the selected process
8/19/2019 C3-Process [Compatibility Mode]
24/49
Cooperating Processes
Independent process cannot affect or be affected by theexecution 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 via parallel sub-tasks
Modularity by dividing system functions into separate processes
Convenience - even an individual may want to edit, print and
compile in parallel
8/19/2019 C3-Process [Compatibility Mode]
25/49
Producer-Consumer Problem
Paradigm for cooperating processes, producer
process produces information that is consumed by a
consumer process.
A buffer enables the producer and consumer to run
concurrently e.g., print program produces characters that are consumed
by the print driver
unbounded-buffer places no practical limit on the size of
the buffer.
bounded-buffer assumes that there is a fixed buffer size – producer must wait if the buffer is full
8/19/2019 C3-Process [Compatibility Mode]
26/49
Bounded-Buffer –
Shared-Memory Solution
Shared data#define BUFFER_SIZE 10typedef struct {. . .
} item;item buffer[BUFFER_SIZE];
int in = 0; int out = 0; Shared buffer is implemented as a circular array
in points to the next free position in the buffer out points to the first full position in the buffer buffer empty when in == out
Buffer full when((in+1)%BUFFER_SIZE)== out Solution is correct, but can only use BUFFER_SIZE-1 elements
8/19/2019 C3-Process [Compatibility Mode]
27/49
Bounded-Buffer Processes
PRODUCER PROCESS item nextProduced; while (1){ while (((in+1)%BUFFER_SIZE) == out)
; // buffer full, do nothing buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;}
CONSUMER PROCESS item nextConsumed; while (1) { while (in == out)
; // buffer empty, do nothingnextConsumed = buffer[out];out = (out + 1) % BUFFER_SIZE;
}
8/19/2019 C3-Process [Compatibility Mode]
28/49
Bounded-Buffer Processes
A
Producer If in+1 will point to same spot out points to, then no more room in the
buffer to insert items and while loops continues
If the buffer is not full then Producer inserts item in buffer at position in andthen increases value of in by 1 mod BUFFER_SIZE
Consumer
If nothing to consume, while loop continues If there is something to consume, then it is processed and out pointer is
increased by 1 mod BUFFER_SIZE
in=0 out=0
in=1
in=2
in=3
in=4
0 1 2 3 4
8/19/2019 C3-Process [Compatibility Mode]
29/49
Bounded-Buffer Processes
A
in 1
out 0
in 0
out 0
A B
in 2
out 0
A B C
in 3
out 0
A B C Din 4
out 0
What would happen if anE was added to index 4?
8/19/2019 C3-Process [Compatibility Mode]
30/49
Interprocess Communication (IPC)
Mechanism for processes to communicate and tosynchronize their actions.
Message system – processes communicate with each
other without resorting to shared variables.
Useful in a distributed environment where the communication processes may reside on different computers connected with a
network (chat program)
IPC facility provides two operations:
send (message)
receive(message)
8/19/2019 C3-Process [Compatibility Mode]
31/49
Interprocess Communication (IPC)
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 properties shared memory
hardware bus
logical properties Direct or indirect communication
Symmetric or asymmetric communication
Automatic or explicit buffering Send by copy or send by reference
Fixed-size or variable-size messages
8/19/2019 C3-Process [Compatibility Mode]
32/49
Implementation Questions
How are links established? Can a link be associated with more than two
processes?
How many links can there be between every pairof 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?
8/19/2019 C3-Process [Compatibility Mode]
33/49
Direct Communication
Processes must name each other explicitly (adisadvantage): 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.
Weakness: changing the name of a process may requireexamining all other process definitions to change the old name tothe new name
8/19/2019 C3-Process [Compatibility Mode]
34/49
Indirect 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.
8/19/2019 C3-Process [Compatibility Mode]
35/49
Indirect Communication
A mailbox owned by a process has a unique owner. When process terminates, mailbox disappears
Mailbox owned by the OS can do the following:
create a new mailbox
send and receive messages through mailbox
destroy a mailbox
Ownership may be passed to other processes via system calls
resulting in multiple receivers for each mailbox
Primitives are defined as:
send( A, message) – send a message to mailbox Areceive( A, message) – receive a message from mailbox A
8/19/2019 C3-Process [Compatibility Mode]
36/49
Indirect Communication
Mailbox sharing P 1 , P 2 , and P 3 share mailbox A.
P 1, sends; P 2 and P 3 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.
8/19/2019 C3-Process [Compatibility Mode]
37/49
Synchronization
Message passing may be either blocking or non-blocking. Blocking is considered synchronous
Send – sending process is blocked until the message is received
by the receiving process or mailbox
Receive – receiver blocks until a message is available
Non-blocking is considered asynchronous Send – sending process sends the message and resumes
operation
Receive – receiver retrieves either a valid message or a null
8/19/2019 C3-Process [Compatibility Mode]
38/49
Buffering
Whether the communication link is direct or indirect,messages exchanged by communicating processes resides
in a temporary queue
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 messages
Sender must wait if link full.
3. Unbounded capacity – infinite length,sender never waits.
8/19/2019 C3-Process [Compatibility Mode]
39/49
Client-Server Communication
Sockets Remote Procedure Calls
Remote Method Invocation (Java)
8/19/2019 C3-Process [Compatibility Mode]
40/49
Sockets
A pair of processes communicating over a networkemploys a pair of sockets.
A socket is defined as an endpoint for communication A socket is identified by an IP address concatenated with a port
number
When a client process initiates a request for a connection,
it is assigned a port by the host computer (arbitrary
number greater than 1024) Unique port number per connecting process allows multiples processes
to establish connections with the same server The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8
8/19/2019 C3-Process [Compatibility Mode]
41/49
Socket Communication
8/19/2019 C3-Process [Compatibility Mode]
42/49
Remote Procedure Calls
Remote procedure Call (RPC) abstracts procedure calls
between processes on networked systems.
Enables a client to invoke a procedure on a remote host as it
would invoke a procedure locally
Stubs – client-side proxy for the actual procedure on theserver.
When the client invokes a remote procedure, the RPC system
calls the appropriate stub, passing it the parameters provided
by the remote procedure
8/19/2019 C3-Process [Compatibility Mode]
43/49
Remote Procedure Calls
The client-side stub locates the server and marshals the parameters (packages the parameters into a form which
may be transmitted over a network)
The server-side stub receives this message, unpacks the
marshaled parameters, and performs the procedure on the
server.
Matchmaker – an OS daemon that enables dynamic
binding of the RPC name to the actual port address of the
RPC on the server
8/19/2019 C3-Process [Compatibility Mode]
44/49
Marshalling Parameters
8/19/2019 C3-Process [Compatibility Mode]
45/49
Execution of RPC
Client : User Calls kernel to send RPC message to procedure X
Client : Kernel sends message to matchmaker to find
the port number
Message: From Client to server, port: matchmaker,Re: Address for RPC X
Server : Matchmaker receives message, looks up
answer
8/19/2019 C3-Process [Compatibility Mode]
46/49
Execution of RPC
Server : Matchmaker replies to client with port P Message: From Server To Client, Port Kernel, Re
RPC X, Port P
Client : Kernel places port P in user RPC message
Client : Kernel sends RPC Message: From Client, To Server, Port P,
Server : Daemon listening to port P receives message
8/19/2019 C3-Process [Compatibility Mode]
47/49
Execution of RPC
Server : Daemon processes request and processessend output
Message : From RPC, Port P, To Client, Port Kernel,
.
Client : Kernel receives the reply and passes it to theuser.
8/19/2019 C3-Process [Compatibility Mode]
48/49
Remote Method Invocation
Remote Method Invocation (RMI) is a Javamechanism similar to RPCs.
RMI allows a Java program on one JVM to invoke
a method on a remote object.
Remote Method Invocation
8/19/2019 C3-Process [Compatibility Mode]
49/49
Remote Method Invocation
Differences between RPC and RMI:
RPC support procedural programming – only remote
procedure or functions may be called; RMI is object based
and enables invocation of methods on remote objects In RPC the parameters are ordinary data structures; with
RMI you can pass objects as parameters