文章目录
OS——Process(Part Two)
Process Termination
Batch job issues
Halt instruction
User logs off
Quit an application
Error and fault conditions
Process executes last statement and asks the operating system to delete it (via exit).
– Output data from child to parent (via wait).
– Process’ resources are deallocated by operating system.
Parent may terminate execution of its children processes (via abort) for a variety of reasons:
– Child has exceeded its usage of allocated resources.
– Task assigned to child is no longer required.
– Parent is exiting.
Interprocess Communication
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
Two fundamental models
– Shared-memory
– Message passing
Message passing is
– useful when smaller numbers of data need to be exchanged, because no conflicts need to be avoided.
– Easier to implement for intercomputer communication.
Shared memory allows maximum speed and convenience of communication.
– It can be done at memory speeds when within a computer.
Shared-memory systems: Producer-Consumer Problem
we must have available a buffer of items that can be filled by the producer and emptied by the consumer.
Two types of buffer used
– unbounded-buffer places no practical limit on the size of the buffer.
– bounded-buffer assumes that there is a fixed buffer size.
//Shared data
#define BUFFER_SIZE 10
Typedef struct { . . . } item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
// in points to the next free position in the buffer;
// out points to the full position in the buffer.
// the buffer is empty when in==out;
// the buffer is full when ((in+1) % buffer_size)==out.
// solution is correct, but can only use BUFFER_SIZE-1 elements
Message-passing systems: Interprocess Communication (IPC)
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 aspects (e.g., shared memory, hardware bus, network)
– logical aspects (e.g., logical properties)
naming
Direct Communication
disadvantages – Limited modularity of the resulting process definitions
Indirect Communication
1.Messages are sent and received from mailboxes (also referred to as ports).
– Each mailbox has a unique id.
– Processes can communicate only if they share a mailbox.
2.Primitives are defined as:
send(A, message) – send a message to mailbox A
receive(A, message) – receive a message from mailbox A
3.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.
4.If the mailbox is owned by a process
– owner can only receive messages through this mailbox
– user can only send messages to the mailbox
if the mailbox is owned by the operating system
– the mailbox is independent
– not attached to any particular process
5.OS provides mechanism allowing a process to do following operations
– create a new mailbox
– send and receive messages through mailbox
– delete a mailbox
Synchronization
Message passing may be either blocking or nonblocking.
Blocking is considered synchronous
– Blocking send: sending process is blocked until the message is received by the receiving process or by the mailbox.
– Blocking receive: receiver blocks until a message is available.
Non-blocking is considered asynchronous
– Nonblocking send: sending process sends the message and resumes operation.
– Nonblocking receive: receiver retrieves either a valid message or a null.
Buffering
Queue of messages attached to the link; implemented in one of three ways.
– Zero capacity – 0 messages
Sender must wait for receiver (rendezvous).
– Bounded capacity – finite length of n messages
Sender must wait if link full.
– Unbounded capacity – infinite length
Sender never waits.
Example of IPC systems (*)
POSIX shared memory
POSIX shared memory is organized using memorymapped files, which associate the region of shared memory with a file.
Process first creates shared memory segment
Message passing in the Mach operating system
Messages are sent to and received from mailboxes, called ports in Mach.
Even system calls are messages.
Each task gets two mailboxes at creation
– Kernel and Notify
Windows
LPC Communicates between two processes on the same machine.
a port object is used to establish and maintain a connection between two processes.
two types of ports:
– Connection ports, server processes published named objects, visible to all processes;
used to set up communication channels.
– Communication ports
Communication works
– Client opens a handle to the server’s connection port object
– Client sends a connection request
– Server creates a channel and return a handle to the client
– Client and server use the corresponding port handle to send messages or callbacks and to listen for replies
Communication in Client-Server systems
Sockets
A socket is defined as an endpoint for communication.
A socket is identified by an IP address concatenated with a port number.
The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8
Communication consists between a pair of sockets.
Remote Procedure Calls (RPC)
abstracts procedure calls between processes on networked systems.
The semantics of RPCs allow a client to invoke a procedure on a remote host as it would invoke a procedure locally. By providing a stub on the client side
– Stubs
The client-side stub locates port on the server and marshals the parameters.
The server-side stub receives this message, unpacks the marshaled parameters, and performs the procedure on the server. Return values are passed back to the client using the same technique.
Stub code is compiled from a specification written in the MIDL(Microsoft Interface Definition Language) (存根)
– used for defining the interfaces between client and server program
machine-independent representation of data, external data representation (XDR).
– Client, converting machine-dependent data into XDR
– server, XDR data is unmarshelled and converted into the machine-dependent representation.
the semantics of a call, exactly once
– “at most once”, attaching to each message a timestamp.(时间戳) the server must keep a history of all the timestamps of messages it has already processed
– “exactly once”, Server sends ACK message to client.
the communication between a server and a client
– binding information may be predetermined, fixed port address
– binding can be done dynamically by a rendezvous mechanism
Remote Method Invocation (RMI, Java)
-
Remote Method Invocation (RMI) is a Java mechanism similar to RPCs.
-
Differences between RPCs and RMI
RPCs support procedural programming whereby only remote procedures or functions may be called. RMI is object-based, it supports invocation of methods on remote objects.
The parameters to remote procedures are ordinary data structures in RPC; with RMI it is possible to pass objects as parameters to remote methods. -
RMI implements the remote object using stubs and skeletons, so remote methods are transparent to both the client and the server
– A stub is a proxy for the remote object, resides with the client
– The skeleton is responsible for unmarshalling the parameters and invoking the desired method on the server
Pipes
Ordinary pipes – cannot be accessed from outside the process that created it. Typically, a parent process creates a pipe and uses it to communicate with a child process that it created.
Require parent-child relationship between communicating processes
Named pipes – can be accessed without a parent-child relationship.
named pipes continue to exist after communicating processes have finished
Provided on both UNIX and Windows systems