Loading...
Loading...

C Interprocess Communication (IPC) Tutorial

Interprocess communication (IPC) allows processes to exchange data in a secure manner. This tutorial explains various IPC mechanisms in C, including pipes, message queues, and shared memory.

1. Introduction to IPC

IPC enables processes to communicate with each other and share data. In C, IPC can be achieved using several methods, such as pipes, message queues, and shared memory.

2. Using Pipes for IPC

Pipes allow data to be passed between processes. A pipe is unidirectional, and it provides communication from a write end to a read end.


#include <stdio.h>
#include <unistd.h>

int main() {
    int pipefd[2];
    char message[] = "Hello from parent!";
    char buffer[128];

    pipe(pipefd);
    write(pipefd[1], message, sizeof(message));
    read(pipefd[0], buffer, sizeof(buffer));
    printf("Received message: %s\n", buffer);
    return 0;
}
                

This code demonstrates the basic use of a pipe. The parent process writes to the pipe, and the child process reads from it.

3. Using Message Queues

Message queues allow processes to send and receive messages in a FIFO (First In, First Out) order.


#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>

struct msgbuf {
    long mtype;
    char mtext[100];
};

int main() {
    key_t key = ftok("msgfile", 65);
    int msgid = msgget(key, 0666 | IPC_CREAT);
    
    struct msgbuf message;
    message.mtype = 1;
    snprintf(message.mtext, sizeof(message.mtext), "Hello from message queue!");
    
    msgsnd(msgid, &message, sizeof(message), 0);
    msgrcv(msgid, &message, sizeof(message), 1, 0);
    
    printf("Received message: %s\n", message.mtext);
    msgctl(msgid, IPC_RMID, NULL);
    return 0;
}
                

Here, we create a message queue and send a message through it. The message is received using `msgrcv`, and the queue is removed with `msgctl` after usage.

4. Using Shared Memory

Shared memory allows multiple processes to access the same memory space for fast data exchange.


#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 *data = (char*) shmat(shmid, (void*)0, 0);
    snprintf(data, 1024, "Hello from shared memory!");
    
    printf("Data in shared memory: %s\n", data);
    shmdt(data);
    shmctl(shmid, IPC_RMID, NULL);
    return 0;
}
                

This example shows how to use shared memory. A segment of memory is created using `shmget`, attached to the process using `shmat`, and the data is accessed directly.

5. Conclusion

IPC is an essential mechanism for communication between processes. Depending on the use case, you can choose pipes, message queues, or shared memory for efficient communication and data sharing between processes in C.

0 Interaction
2.3K Views
Views
33 Likes
×
×
🍪 CookieConsent@Ptutorials:~

Welcome to Ptutorials

Note: We aim to make learning easier by sharing top-quality tutorials.

We kindly ask that you refrain from posting interactions unrelated to web development, such as political, sports, or other non-web-related content. Please be respectful and interact with other members in a friendly manner. By participating in discussions and providing valuable answers, you can earn points and level up your profile.

$ Allow cookies on this site ? (y/n)

top-home