Header Ads

Program to implement pipes for inter process communication

To create child process we use fork(). fork() returns :
<0 fail to create child (new) process
=0 for the child process
>0 i.e process ID of the child process to the parent process. When >0 parent process will execute.

pipe() is used for passing information from one process to another. pipe() is unidirectional, therefore, for two-way communication between processes, two pipes can be set up, one for each direction.


Program to send a message from parent process to child process.

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
      int fd[2],n;
      char buffer[100];
      pid_t p;
      pipe(fd);
      p=fork();
      if(p>0)  //parent
      {
                  close(fd[0]);
                  printf("Passing value to child\n");
                  write(fd[1],"hello\n",6);
                  wait();
                
      }
      else // child
      {
                  close(fd[1]);               
                  n=read(fd[0],buffer,100);
                  write(1,buffer,n);
      }
}


// C program to demonstrate use of fork() and pipe()
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include<sys/wait.h>

int main()
{
    // We use two pipes
    // First pipe to send input string from the parent
    // Second pipe to send a concatenated string from the child

    int fd1[2];  // Used to store two ends of first pipe
    int fd2[2];  // Used to store two ends of second pipe

    char fixed_str[] = "lpuallprogram.in";
    char input_str[100];
    pid_t p;

    if (pipe(fd1)==-1)
    {
        fprintf(stderr, "Pipe Failed" );
        return 1;
    }
    if (pipe(fd2)==-1)
    {
        fprintf(stderr, "Pipe Failed" );
        return 1;
    }

    scanf("%s", input_str);
    p = fork();

    if (p < 0)
    {
        fprintf(stderr, "fork Failed" );
        return 1;
    }

    // Parent process
    else if (p > 0)
    {
        char concat_str[100];

        close(fd1[0]);  // Close reading end of first pipe

        // Write input string and close writing end of first
        // pipe.
        write(fd1[1], input_str, strlen(input_str)+1);
        close(fd1[1]);

        // Wait for the child to send a string
        wait(NULL);

        close(fd2[1]); // Close writing end of second pipe

        // Read string from child, print it and close
        // reading end.
        read(fd2[0], concat_str, 100);
        printf("Concatenated string %s\n", concat_str);
        close(fd2[0]);
    }

    // child process
    else
    {
        close(fd1[1]);  // Close writing end of first pipe

        // Read a string using first pipe
        char concat_str[100];
        read(fd1[0], concat_str, 100);

        // Concatenate a fixed string with it
        int k = strlen(concat_str);
        int i;
        for (i=0; i<strlen(fixed_str); i++)
            concat_str[k++] = fixed_str[i];

        concat_str[k] = '\0';   // string ends with '\0'

        // Close both reading ends
        close(fd1[0]);
        close(fd2[0]);

        // Write concatenated string and close writing end
        write(fd2[1], concat_str, strlen(concat_str)+1);
        close(fd2[1]);

        exit(0);
    }
}
Powered by Blogger.