Junior — Middle
What methods are used for inter-process communication in operating systems?
sobes.tech AI
Answer from AI
The main methods used for inter-process communication (IPC) in operating systems are:
- Pipes — allow data to be transmitted as a byte stream between processes, usually related.
- Named Pipes (FIFO) — similar to pipes but have a name in the filesystem and can be used between unrelated processes.
- Sockets — provide data exchange between processes, both on the same computer and over a network.
- Shared Memory — a memory area allocated for quick data exchange accessible by multiple processes.
- Semaphores — used for synchronizing access to shared resources.
- Message Queues — allow processes to exchange structured messages through a queue.
In Python, IPC can be implemented using the multiprocessing module, which provides high-level objects for data exchange such as queues (Queue), pipes (Pipe), and support for shared memory via Value and Array.
Example of using a queue for data exchange between processes in Python:
from multiprocessing import Process, Queue
def worker(q):
q.put('Hello from the child process')
if __name__ == '__main__':
q = Queue()
p = Process(target=worker, args=(q,))
p.start()
print(q.get()) # Receive message from the child process
p.join()