Inter-Process Communication (IPC)
Definition: Inter-Process Communication (IPC) refers to the mechanisms that allow processes to communicate and share data with each other.
Pipes and FIFOs
Pipes:
- A pipe is a one-way communication channel that allows the output of one process to be connected to the input of another process.
# Example: Command1's output is passed as input to Command2 command1 | command2
FIFOs (Named Pipes):
- A FIFO (First-In-First-Out) is a named pipe that allows unrelated processes to communicate. It is similar to a regular pipe but can be accessed by multiple processes.
# Create a FIFO
mkfifo my_fifo
# Example: Command1 writes to FIFO, Command2 reads from FIFO
command1 > my_fifo & command2 < my_fifo
Signals and Signal Handling
Signals:
- Signals are software interrupts sent to a process to communicate an event or request a specific action.
# Example: Send SIGTERM signal to a process with PID <PID>
kill -15 <PID>
Signal Handling:
- Processes can define custom actions to be taken when a specific signal is received.
#include <stdio.h>
#include <signal.h>
void signal_handler(int signum) {
printf("Received signal %d\n", signum);
}
int main() {
signal(SIGINT, signal_handler); // Handle SIGINT (Ctrl+C) signal
while (1) {
// Code to execute
}
return 0;
}
Shared Memory
Shared Memory:
- Shared memory allows multiple processes to access and modify the same portion of memory. It is a very efficient method of IPC.
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main() {
key_t key = ftok("shmfile",65);
int shmid = shmget(key,1024,0666|IPC_CREAT);
char *str = (char*) shmat(shmid,(void*)0,0);
printf("Write Data : ");
gets(str);
shmdt(str);
return 0;
}
Message Queues
Message Queues:
- Message queues allow processes to communicate by sending and receiving messages.
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct msg_buffer {
long msg_type;
char msg_text[100];
};
int main() {
key_t key;
int msgid;
struct msg_buffer message;
// Create unique key
key = ftok("progfile", 65);
// Create a message queue
msgid = msgget(key, 0666 | IPC_CREAT);
message.msg_type = 1;
printf("Write Data : ");
gets(message.msg_text);
// Send message
msgsnd(msgid, &message, sizeof(message), 0);
return 0;
}
Sockets
Sockets:
- Sockets provide communication between processes over a network. They can be used for IPC between processes on the same machine or across a network.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
int main() {
int sock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(12345);
server.sin_addr.s_addr = inet_addr("127.0.0.1");
connect(sock, (struct sockaddr *)&server, sizeof(server));
char message[100];
strcpy(message, "Hello, server!");
send(sock, message, sizeof(message), 0);
close(sock);
return 0;
}
These examples provide practical demonstrations of different inter-process communication mechanisms. Each example demonstrates a method for processes to exchange information, either through pipes, FIFOs, signals, shared memory, message queues, or sockets.