Operating Systems Lecture 6: Inter-process communication
Inter-Process Communication Mechanisms
Overview of Inter-Process Communication (IPC)
- The lecture introduces the concept of inter-process communication (IPC), emphasizing that processes do not share memory and have separate memory images.
- IPC mechanisms allow processes to communicate by sharing information or bytes, with shared memory being the first discussed mechanism.
Shared Memory
- Shared memory enables two processes to access the same region of memory, allowing one process to write data while another reads it.
- Implementing shared memory requires coordination between processes to prevent simultaneous writes and manage read/write operations effectively.
Signals as a Communication Method
- Signals are defined messages sent to processes, some having fixed meanings like termination signals.
- When a process receives a signal, a signal handler executes; default actions can be overridden for custom behavior upon receiving specific signals.
- Certain signals cannot be overridden to prevent indefinite execution of a process, ensuring system stability.
Sockets for Process Communication
- Sockets facilitate communication between processes on the same or different machines using TCP or UDP protocols.
- Each process opens a socket and connects them for sending and receiving information; system calls remain consistent regardless of machine location.
Pipes: A Half-Duplex Communication Method
- Pipes provide half-duplex communication where data flows in one direction only.
Understanding File Descriptors and Inter-Process Communication
What is a File Descriptor?
- A file descriptor serves as a handle for reading or writing to files, sockets, or pipes.
- The pipe system call generates two file descriptors: one for writing and another for reading, facilitating half-duplex communication.
Parent and Child Process Communication
- Pipes are primarily used for communication between parent and child processes; the parent creates a pipe before forking a child process.
- Both the parent and child share access to the same pipe's file descriptors after the fork operation. The parent can write while the child reads from it.
- To avoid both ends of the pipe being in one program, different ends are assigned to different processes post-forking. This allows effective inter-process communication.
Named vs Unnamed Pipes
- Named pipes allow two distinct processes to communicate by accessing endpoints of the same named pipe, unlike unnamed pipes which are limited to single processes or their children.
- When using named pipes, both processes can obtain file descriptor handles to read from and write into them effectively.
Message Queues as an Alternative
- Message queues function like mailboxes where one process can send messages that another process retrieves later; this provides an additional method of inter-process communication.
- The operating system buffers messages sent to a mailbox until they are received by another process, allowing asynchronous communication between them.
Blocking vs Non-blocking Communication
- Communication through pipes can be either blocking or non-blocking; if a pipe is full during a write operation, it may block until space is available or return an error message instead.