UNIX processes using C/C++
  • 🍴The fork system call
  • ⌚The wait system call
  • 🆔Process ID
  • 🔌Signals in UNIX
    • 🗄️Signal Handlers
  • 🥃Executing system commands
  • 📁Sharing information between processes
    • 🕚Bidirectional pipe
  • 🖥️Applications
    • 💻Console Application 1
    • 💻Console Application 2
  • ⚽Named pipe FIFO
Powered by GitBook
On this page
  1. Sharing information between processes

Bidirectional pipe

In this section we shall write a program for communicating between parent and child process using a bidirectional pipe

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

#define MAX_SIZE 1024

void GETS(char* str){
    fgets(str, MAX_SIZE, stdin);
    str[strlen(str)-1] = 0;
}

int main(){
    // declaring the file descriptors 
    int fd1[2], fd2[2];
    char message[MAX_SIZE], buffer[MAX_SIZE];
    
    if(pipe(fd1) == -1){
        perror("Pipe-1 creation failed\n");
        exit(0);
    }
    if(pipe(fd2) == -1){
        perror("Pipe-2 creation failed\n");
        exit(0);
    }
    int pid = fork();
    if(pid == 0){
        close(fd1[1]);
        close(fd2[0]);
        while(1){
            memset(buffer, 0, sizeof(buffer));
            read(fd1[0], buffer, MAX_SIZE);

            if(strlen(buffer) != 0)
                printf("\nReceived data from parent : %s\n",buffer);
            else
                printf("\nNo data received from parent\n");
            if(!strcmp(buffer,"exit")) break;

            fflush(stdin);
            printf("Write data for parent : ");

            GETS(message);
            write(fd2[1], message, MAX_SIZE);

            if(!strcmp(message,"exit")) break;
        }
    }else{
        close(fd1[0]);
        close(fd2[1]);
        while(1){
            memset(message, 0, sizeof(message));
            fflush(stdin);
            printf("Write data for child : ");

            GETS(message);
            write(fd1[1], message, MAX_SIZE);
            if(!strcmp(message,"exit")) break;

            memset(buffer, 0, sizeof(buffer));
            read(fd2[0], buffer, MAX_SIZE);

            if(strlen(buffer) != 0)
                printf("\nReceived data from child : %s\n",buffer);
            else
                printf("\nNo data received from child\n");
            if(!strcmp(buffer,"exit")) break;            
        }
    }
    return 0;
}

Output

Write data for child : Hello

Received data from parent : Hello
Write data for parent : Hii

Received data from child : Hii
Write data for child : I am Dark Mortal (daddy process)

Received data from parent : I am Dark Mortal (daddy process)
Write data for parent : I am Shadow Monarch (child process)

Received data from child : I am Shadow Monarch (child process)
Write data for child : I am stronger than you

Received data from parent : I am stronger than you
Write data for parent : 

No data received from child
Write data for child : Yeah I thought so

Received data from parent : Yeah I thought so
Write data for parent : Pls die

Received data from child : Pls die
Write data for child : exit

Received data from parent : exit
PreviousSharing information between processesNextApplications

Last updated 2 years ago

📁
🕚