Sobes.tech
Junior — Middle

How is information transmitted between running processes in a system?

sobes.tech AI

Answer from AI

Inter-process communication (IPC) in systems can be carried out using several methods:

  • Files: processes read and write data from shared files.
  • Pipes: one-way or two-way data transfer, often used in Unix systems.
  • Sockets: allow data exchange over a network or locally via Unix domain sockets.
  • Shared memory: a memory area allocated accessible to multiple processes.
  • Semaphores and mutexes: used for synchronizing access to shared resources.
  • Message queues: processes exchange messages through system queues.

In Python, IPC can be implemented using the multiprocessing module, which supports queues, pipes, and shared memory. An example of data transfer via a queue:

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
    p.join()