Inter-process Communication (IPC)

When programs run, they are called processes. Sometimes, they need to talk to each other.

This communication is called Inter-process Communication (IPC).

What is IPC?

IPC means one process sending data or messages to another process.

It helps processes work together, share information, and do tasks faster.

πŸ§ƒ Example: Imagine two kids playing a game. One rolls a dice, the other moves the piece. They must talk to play. That’s IPC!

Why is IPC Important?

Types of IPC

There are many ways for processes to talk. Let's look at the most common ones.

1. Pipes

A pipe lets one process send data to another in a straight line.

Think of it like a water pipe: one end puts water (data), the other takes it out.

πŸ”§ Example: In Linux: ls | grep "file". One process sends a list to another that filters it.

2. Named Pipes (FIFOs)

Like pipes, but with a name. So, different programs can find and use them.

They stay in the system like a file.

3. Message Queues

A place where processes drop messages to be picked up by others.

It works like a mailbox. One sends a letter, another picks it up later.

4. Shared Memory

Two or more processes use the same area of memory.

This is very fast, but needs care to avoid problems.

πŸ• Example: Think of two people sharing a whiteboard to write ideas.

5. Sockets

Sockets let processes talk over a network.

Used in online games, chats, or web apps.

πŸ’¬ Example: Chat apps use sockets to send messages between phones.

Comparison Table

IPC Method Speed Used For
Pipes Medium Simple communication
Named Pipes Medium Different unrelated processes
Message Queues Medium Asynchronous messages
Shared Memory Very Fast Large data exchange
Sockets Slower Remote/network communication

Real Life Analogy

🏒 Imagine a busy office. Workers (processes) need to share files, notes, and ideas. They may use emails (message queues), shared folders (shared memory), or phone calls (sockets). Each method helps them work together better.

Problems with IPC

Synchronization

When processes share data, timing is important.

Synchronization means making sure things happen in the right order.

πŸ›ŽοΈ Example: If two people write on the same notebook at once, it gets messy. They need to take turns.

Summary

πŸ“š Key Terms

πŸ› οΈ Try It Yourself

If you use Linux, open two terminals. In one, run:
mkfifo mypipe
Then in one terminal:
echo "Hello from A" > mypipe
In the other terminal:
cat mypipe
You just used a named pipe!

Great job! You now understand how processes talk using IPC. πŸŽ‰