Opsys_08_ipc
Communication Between Processes - Lecture 7
Today's Program
The IPC model and its characteristics
IPC mechanisms
The Unix message queue
Unix shared memory
Sample programs
Inter Process Communication (IPC)
The independent process model does not exist in reality; there is always some relationship between processes, often competitive for resources.
To leverage cooperation, we can use IPC to facilitate communication between processes.
IPC mechanisms discussed so far include signaling for synchronization through minimal data transfer.
Parent-child processes can communicate through environmental variables (small amounts of information).
Communication can also happen via files (larger data volumes but slower).
Key question: How can we transfer information quickly and efficiently (large amounts of data with good performance)?
General Model of IPC
Required method for communication:
Send(to whom, message)
Receive(print, message)
Consider the following 4 key questions regarding IPC:
Does the sending process wait for the receiving process to receive the message?
Should the receiving process wait on the issued receive() call?
Does the sender specify the recipient of the message?
Does the receiver determine the sender's identity?
Synchronous vs. Asynchronous Communication
Possible answers to the first two questions about sending and receiving:
Blocked sending: the sender awaits acknowledgment that the receiver is ready.
Non-blocking sending (asynchrony): the sender continues without waiting.
Blocking receiving: the receiver waits for a message.
Non-blocking receiving (asynchrony): the receiver either retrieves a message or continues processing without waiting.
If both sending and receiving processes are blocked, communication is synchronous; if either (or both) is non-blocking, it is asynchronous.
Naming in IPC
Possible answers for another pair of questions regarding to/from:
Explicit naming: Sending is addressed to a specific process (targeted sending).
Implicit naming: Broadcasting messages where the sender does not specify a recipient.
Explicit identification of sender/receiver is useful for targeted communication, while implicit naming allows for broader message dissemination.
Practical Communication Models
Useful versions of communication methods:
Synchronous and explicit names (explicit naming) facilitate effective synchronization.
Broadcasting with blocked sending is impractical due to implementation challenges.
Blocked receiving with implicit names is useful in certain scenarios.
Non-blocking receiving with implicit names is beneficial but can be challenging to manage; leads to the indirect communication model.
Communication Characteristics
Number and Direction:
Participants in communication can vary in number; explicit names indicate situation between two processes.
Implicit names allow for one-to-many or many-to-one communications.
Typically, communication mechanisms are unidirectional (asymmetric), but they can also be multidirectional (symmetric).
Buffering:
Communication links can have zero, limited, or infinite buffering capacity.
Message length may be constrained by the method used:
Data sending vs. link sending (link-sending involves an indirect pathway).
Primitive Mechanisms:
Channel: A primitive IPC mechanism where a message is removed from the channel upon receipt; can be direct (between specified processes) or indirect (through an intermediary).
Buffered channels can be FIFO (First In First Out) or FILO (First In Last Out).
Pool: Another primitive where retrieved messages remain for multiple accesses typically indirect and can also be buffered, usually symmetrical in nature.
IPC Mechanism Models
Various IPC model types:
Unbuffered, asymmetric channel
Buffered, FIFO, asymmetric channel
Buffered, FILO, asymmetric channel
Unbuffered symmetrical channel
Transmission Types
Direct Transfer:
Requires processes to be aware of each other, typically explicit naming; common between two processes.
Indirect Transmission:
Involves an intermediary such as a message line or mailbox; participants need to be aware of this intermediary, which allows for implicit knowledge concerning individual process names and explicit knowledge of mediation.
Indirect Communication Structures
Components of indirect communication include:
Message Queue / Mailbox: Form independent objects, requiring knowledge of the intermediary.
Generic system calls for IPC include:
create(msg-queue)destroy(msg-queue)associate(msg-queue)send(msg-queue, message)receive(msg-queue, message)
Binding and Access Control
Two aspects of IPC binding:
Can be linked to a process: termination of the owner ceases the intermediary.
Access Relationship: use relationship allowing multiple accesses; binding relationships can be transferred, and access conditions defined.
Access to the OS may also be established, with potential for varied access levels.
Attachment vs. Access
Differentiation between:
Bonding properties (existence of the mediator) and ownership (dictating access rights).
Ownership relates to capabilities such as sending/receiving messages.
Enumerated IPC Mechanisms
Signaling: Asynchronous, indirect, fast, little information.
Environment communication: Asynchronous, indirect, bufferless, fast, little information.
File communication: Asynchronous, indirect, slower transfer of large information through FIFO channels.
Message queue communication.
Shared memory communication.
Semaphores as IPC mechanisms.
BSD socket communication.
Remote Procedure Call (RPC).
Unix Message Queue System
Characteristics:
Fast, medium transfer amount; uses an indirect FIFO channel; OS-bound, multidirectional; theoretically infinite buffer capacity; variable message lengths.
Essential system calls for message queues include:
msgget()- to create or identify a message queuemsgctl()- control and query message queue detailsmsgsnd()- to send messagesmsgrcv()- to receive messages from the queue
Identifications:
External: key
Internal: variable
Managing the Message Queue
A message queue exists independently of processes when created.
Ownership and access managed through file ownership and access rights.
Use
ipcscommand to retrieve IPC characteristics.
IPC Status with ipcs Command
Example command output:
ipcsIPC statusShows message queues, shared memory, and semaphore attributes.
System Calls: msgget
Function Prototype:
```c
int msgget(key_t key, int msgflg);
- `key` is used for external identification, and `msgflg` manages protections upon creation and access settings.
- The return value is the internal descriptor of the message queue or an error code.
## System Calls: `msgctl`
- **Function Prototype**:
c
int msgctl(int id, int cmd, struct msqid_ds *buf);
- Parameters:
- `id`: internal descriptor of the message queue.
- `cmd`: command for various operations including status query, setting attributes, and queue deletion.
- `buf`: pointer to the status description structure.
- Return value: 0 on success, -1 on error.
## System Calls: `msgsnd`
- **Function Prototype**:
c
int msgsnd(int id, struct msgbuf *msgp, size_t size, int msgflg);
- Describes how to send messages to the queue with details on structure and types of messages.
## System Calls: `msgrcv`
- **Function Prototype**:
c
int msgrcv(int id, struct msgbuf *msgp, size_t size, long msgtyp, int msgflg);
- Allows for detailed retrieval of messages based on type.
## Unix Shared Memory Usage
- Quick, indirect, zer-buffered pool of communication.
- Common system calls include:
- `shmget()`: creation and identification
- `shmat()`: attachment to process address range
- `shmdt()`: disconnection from the address range
- `shmctl()`: control and query shared memory characteristics.
## System Calls: `shmget`
- **Function Prototype**:
c
int shmget(key_t key, int size, int msgflg);
- Used for creating and identifying shared memory segments with options for protection and access settings.
## System Calls: `shmat`
- **Function Prototype**:
c
void *shmat(int id, const void *addr, int flg);
- Concerned with mapping shared memory segments to application address spaces.
## System Calls: `shmdt`
- **Function Prototype**:
c
int shmdt(const void *addr);
- Disconnects previously attached shared memory segments.
## System Calls: `shmctl`
- **Function Prototype**:
c
int shmctl(int id, int cmd, struct shmid_ds *buf);
```
Performs various controls and queries concerning shared memory attributes.
Shared Memory Segment Attributes
Attributes Struct:
struct shmid_dsIncludes access permissions, sizes, and timestamps for the last connection and disconnection.
Final Exam Item Related to IPC
Topics for review include basic communication concepts: blocking, synchronization, addressing, plurality, symmetry, indirectness, buffer capacity; primitive mechanisms; examples including signals, environment variables, communication via files, etc.; shared memory; message queues.